Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2007-03-28
5
Medium Priority
?
480 Views
Last Modified: 2013-11-20
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!
0
Comment
Question by:DavidHands
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 9

Expert Comment

by:Priyesh
ID: 18812985
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.
0
 
LVL 4

Expert Comment

by:bdunz19
ID: 18813892
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
0
 

Author Comment

by:DavidHands
ID: 18817760
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.
0
 
LVL 4

Accepted Solution

by:
bdunz19 earned 750 total points
ID: 18821560
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,
Brandon
0
 
LVL 9

Assisted Solution

by:Priyesh
Priyesh earned 750 total points
ID: 18821770
David,
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.
Priyesh
0

Featured Post

Building an interactive eFuture classroom

Watch and learn how ATEN provided a total control system solution including seamless switching matrix switch, HDBaseT extenders, PDU, lighting control to build an interactive eFuture classroom.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An introduction to the wonderful sport of Scam Baiting.  Learn how to help fight scammers by beating them at their own game. This great pass time helps the world, while providing an endless source of entertainment. Enjoy!
IF you are either unfamiliar with rootkits, or want to know more about them, read on ....
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question