Solved

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

Posted on 2007-03-28
5
466 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
  • 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 250 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 250 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

Do email signature updates give you a headache?

Do you feel like all of your time is spent managing email signatures? Too busy to visit every user’s desk to make updates? Want high-quality HTML signatures on all devices, including on mobiles and Macs? Then, let Exclaimer solve all your email signature problems today!

Join & Write a Comment

Storage devices are generally used to save the data or sometime transfer the data from one computer system to another system. However, sometimes user accidentally erased their important data from the Storage devices. Users have to know how data reco…
Our Group Policy work started with Small Business Server in 2000. Microsoft gave us an excellent OU and GPO model in subsequent SBS editions that utilized WMI filters, OU linking, and VBS scripts. These are some of experiences plus our spending a lo…
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now