MSN windowless RichEdit actually uses the COM interface to give an article.
Introduction to the windowless RichEdit
It is a little-known fact that the RichEdit from version 2.0 onwards comes with a COM interface to drive it. to use this interface, you don't need to create a RichEdit window, so it is perfect for applications that don't need the extra handling required by having just another window. the example given here extends strates this point by providing a class that can draw RTF text anywhere you 'd like.
The main com interfaces involved are itexthost, itextservices, and itextdocument. when you create a windowless RichEdit, you have to provide it with a class that implements itexthost. this class is usually quite simple, even more so in my example because I just use it for drawing and don't provide editing capabilities. itextservices is the main interface you use to communicate with RichEdit. it provides the functions for setting, getting, and drawing the text. moreover, it lets you send messages to the RichEdit that's hidden behind it. so you can still use all the standard messages such as em_setcharformat. itextdocument is the interface you can use to bypass the message passing. by using it, you can get an itextselection interface to the current selection-An itextrange interface for any range, for example. these in turn let you get itextfont objects and so on.
Itextdocument is not specific to the windowless RichEdit, though. when you have created a random wed RichEdit, you can use the em_getoleinterface message to retrieve a COM interface to the RichEdit, anyhow. this interface can be queried for itextdocument and you will be able to use all its functionality.
Implementing itexthost
This is where it gets a little complicated. the msdn documentation on itexthost is pretty much non-existant, so you have to work from the few indications you get. if you want to implement a fully editable itextservices object, you will have to fill in all the functions I have left blank in my example. this is because, for the sole purpose of drawing RTF text, you don't need to provide much information to itextservices, aside from the RTF text, the formatting rectangle, and the HDC. it is recommended that the class that implements itexthost be a com class because that's the way itextservices communicates with it. to make my example easier, I left out the com bits and provided myself the basic iunknown functions. it is interesting to note that just after creating the textservices object, it queries the texthost for some other interface. I have not been able to figure out what this is, though.
Using the example code
All the windowless RichEdit functionality is provided by the cformattedtextdraw class. it implements itexthost minimally and also the custom interface (or rather purely abstract class) iformattedtextdraw. the reason for using iformattedtextdraw Is That You shocould not call any functions on itexthost directly; Access shoshould always be through iformattedtextdraw instead of cformattedtextdraw. the usage of the class can be seen in the win32test. CPP file. it is based on the sample "Hello World" Win32 project that vc6 generates. here is the simplicity with which you use this class:
Iformattedtextdraw * g_formatter;
BSTR bstrtext;
Rect RT;
HDC;
G_formatter = new cformattedtextdraw;
G_formatter-> Create ();
// Fill in the Unicode text, the rectangle, and get an HDC
G_formatter-> put_rtftext (bstrtext );
G_formatter-> draw (HDC, & RT );
Notes
To use this code in your own projects, you will have to link with riched20.lib. moreover, you will most probably get a linker error related to _ iid_itextservices and _ iid_itexthost. if this is the case, open your version of textserv. H (you will find it in .. /Microsoft SDK/include directory) and comment out the two following lines:
// Extern_c const IID iid_itextservices;
// Extern_c const IID iid_itexthost;
Download: http://www.codeguru.com/code/legacy/richedit/RTFDraw_demo.zip
Http://www.codeguru.com/code/legacy/richedit/RTFDraw_src.zip
Bytes.