using fonts in atl controls with nicetext.exe |
the information in this article applies to:
- microsoft active template library, versions 2.0, 2.1, 3.0
summary
active template library (atl) programmers creating activex controls that display text may find it beneficial to allow the user of the control to select the font that the text is drawn in. atl provides some functionality for a stock font property, or you may choose to use a custom font property. in atl activex controls, the steps to implement a custom or stock font property are practically the same. the more information section of this article describes the steps to take to add a stock font property to your atl activex control. the nicetext sample shows how to implement an atl control that uses a stock font property.
nicetext.exe is a self-extracting executable file that contains the source files for the nicetext control.
the following files are available for download from the microsoft download center. click the file names below to download the files:
nicetext.exe
for more information about how to download files from the microsoft download center, please visit the download center at the following web address
http://www.microsoft.com/downloads/search.asp and then click how to use the microsoft download center.
more information
use these steps to create a control with a stock font property:
- open an existing atl in-proc server project or create a new one.
- using the atl object wizard, insert a new atl control into your project. give the control a name, then select the stock properties tab. from the stock properties tab, select the font property and insert it into your control. completing this step adds an interface definition such as the following to your projects interface definition language file (.idl file). the interface definition in your .idl file looks similar to the following:
[
object,
uuid(e882d672-878e-11d0-b00c-000000000000),
dual,
helpstring("inicetextctrl interface"),
pointer_default(unique)
]
interface inicetextctrl : idispatch
{
[propputref, id(dispid_font)]
hresult font([in]ifontdisp* pfont);
[propput, id(dispid_font)]
hresult font([in]ifontdisp* pfont);
[propget, id(dispid_font)]
hresult font([out, retval]ifontdisp** ppfont);
}; in order for your control's font property to work correctly in most control containers, you need to move this interface definition from outside the library block in your .idl file to inside the library block. you also need to remove the #import for ucidl.idl from the .idl file. your .idl file now has a library section that looks like the following: [
uuid(e882d665-878e-11d0-b00c-000000000000),
version(1.0),
helpstring("nicetext 1.0 type library")
]
library nicetextlib
{
importlib("stdole32.tlb");
importlib("stdole2.tlb");
[ object,
uuid(e882d672-878e-11d0-b00c-000000000000),
dual,
helpstring("inicetextctrl interface"),
pointer_default(unique)
]
interface inicetextctrl : idispatch
{
[propputref, id(dispid_font)]
hresult font([in]ifontdisp* pfont);
[propput, id(dispid_font)]
hresult font([in]ifontdisp* pfont);
[propget, id(dispid_font)]
hresult font([out, retval]ifontdisp** ppfont);
};
[
uuid(e882d673-878e-11d0-b00c-000000000000),
helpstring("nicetextctrl class") ]
coclass nicetextctrl
{
[default] interface inicetextctrl;
};
}; this step also adds a member variable named m_pfont to your control's implementation class to hold the font property. the get, put, and putref functions for the stock font property are implemented by atl in the cstockpropimpl class inside atlctl.h. if you are implementing a non- stock font property, you have to add the get, put, or putref functions manually if you are using visual c++ 4.2b. if you are using visual c++ 5.0, you can use the interface browser to add the property, which adds the property get and put functions for you.
- add a static variable of type fontdesc to your control's .cpp file. the fontdesc structure holds the default description of your font property. you can create and initialize the variable in the same step as follows:
static const fontdesc _fontdesc =
{sizeof(fontdesc), olestr("times new roman"), fontsize( 14 ),
fw_bold, ansi_charset, true, false, false };
- add an entry to your control's property map for the stock font such as the following:
prop_entry("font", dispid_font, clsid_msstockfont) note: you also need to #include <atlctl.h>
- change your control's drawing code so it uses your font property to draw text. here is an example of drawing code you could add to an atl control's ondraw member function:
hfont hstockfont=null,holdfont=null;
ccomqiptr<ifont,&iid_ifont> pfont(m_pfont);
tchar msg[50];
wsprintf(msg,_t("current thread is %x"),getcurrentthreadid());
if(pfont)
pfont->get_hfont(&hstockfont);
if(hstockfont)
holdfont = (hfont)selectobject(di.hdcdraw,hstockfont);
drawtext(di.hdcdraw, msg, -1, &rc, dt_center | dt_vcenter |
dt_singleline);
if(holdfont)
selectobject(di.hdcdraw,holdfont);
- it is recommended that you implement a sink object to connect to the font object's property notification source so that your control updates its rendering with the correct font properties when a client changes one of your font's properties. com-based fonts support an outgoing interface based on the ipropertynotifysink interface to notify font users when properties of the font, such as name or weight, are about to change and after they have been changed. to receive notification of these changes, add a class to your project that implements the ipropertynotifysink interface. then, an instance of this class can be created by the control and a pointer to it passed to the font object's connection point. your control then receives notifications from the font object if its properties change, which allows you to update your control's text accordingly.
本文关键:Using Fonts in ATL Controls
本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)
|