How to create, draw and/or print formatted text.

I'm programming for windows using mfc VC++ 2003 and need a suggestion on ways to solve this problem...

My windows mfc app creates a small amount of formatted text (bold, italics and different size fonts).  Later the text needs to be drawn or printed, but the lower level library that does the drawing & printing, gets a CDC* and a char* and that is it (I'm exaggerating a bit but the point is windows/mfc api and controls are not available at the lower level).  Currently all the drawing is done via CDC->DrawText

I was thinking I could use CRichEditCtrl to create RTF text (assuming that's what it does) and then later hand code the drawing of RTF text, but I'd rather not rewrite how to draw from RTF.  Is this approach reasonable?  Are there libraries that do draw/print RTF already?  Is there a better way?

Who is Participating?
bdunz19Connect With a Mentor Commented:
Hi again,

Well that makes more sense and now I see your problem. Let me warn you, as you are about to embark on a very difficult journey! I spent 2 months writing something over last summer that works just like you are saying; it was an html/css printer DLL. Basically the DLL I wrote was fed standard html with cascade style sheets as the formating data, and the DLL then parsed through it to popback a final CDC that would then be sent to a printer. Something like this is not by any means easy. The DLL I wrote only supports about half of the css standard format possibilities (this was because of a lack of time).

The other problem I see you having is that if you can't modify the "low level" library that you are calling, how will your library possibly know how to correctly parse the text or data you are sending it to create the final visual formating of the text? You might in the end have to replace this library with on of your own...

Anyway, those are my thoughts, let me know what you think or how you might be able to simplify this problem, as most people don't have 2 months to blow away writing something like this.

Good luck,
You said the lower level library does not get access to windows/mfc api. Then how does it know about a CDC and if it's HDC, then still the manipulations are through windows api right? I am not understanding what the restriction is then.
Hi DavidHands,
If your library accepts a CDC or a HDC all you need to do is declare an CFont or HFONT and select it into the CDC/HDC before passing it to the library.

// In your class def:
CFont m_font;

// In your class constructor or really anywhere:

// Then before passing your CDC to the library:
CFont *pOldFont = dc.SelectObject(&m_font); // Select the font into the CDC
dc.SetTextColor(RGB(255,0,0)); // You could also change the text color of the CDC
dc.SetBkMode(TRANSPARENT); // You could also chose to make the background of the text transparent
LibraryCall(&dc); // This is where you pass your CDC* to the library

Well, good luck!
DavidHandsAuthor Commented:
Hi Brandon,

Your example code is actually very close to what I currently do.  What is different for me now is I do not know the formatting until runtime.  For example, suppose the user wants the app to draw "Hello world" in Arial 12pt and bold the "Hello", but next time time they want to draw "Hello world" in Bookman 10pt and italicize "Hello".

So now I need a way to store and retrieve the formatting information along with the text so I can correctly set up the CDC prior to drawing.  This is what made me think of using RTF because it's contains the text and formatting in an ASCII format (right?), but I've never used RTF.  I'm not sure what options are available I just know I don't want to reinvent the wheel if I don't need to.

PriyeshConnect With a Mentor Commented:
If you can store the formatted text as RTF, you should be able to stream it into a RichEditCtrl and print from it.

Look at CRichEditCtrl and the StreamIn function.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.