Link to home
Start Free TrialLog in
Avatar of DavidHands
DavidHands

asked on

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?

Thanks!
Avatar of Priyesh
Priyesh

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:
m_font.CreateFontA(10, 0, 0, 0, FW_NORMAL, 0, 0, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH | FF_ROMAN, "MS Sans Serif");

// 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
dc.SelectObject(pOldFont);

Well, good luck!
Brandon
Avatar of DavidHands

ASKER

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.

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of bdunz19
bdunz19

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial