分享
 
 
 

Using Fonts in ATL Controls

王朝vc·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

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.aspand 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.

The following class implements an IPropertyNotifySink interface and can be used for receiving notifications from a Font object. This class also holds a back pointer to the parent control object so that it can update the control when properties change. This class is added to the control's header file before the declaration of the control's class: class CNiceTextCtrl; //forward definition of parent class

class ATL_NO_VTABLE CFontNotifyImpl :

public CComObjectRootEx<CComSingleThreadModel>

public IPropertyNotifySink

{

public:

CFontNotifyImpl():m_pParent(NULL){}

BEGIN_COM_MAP(CFontNotifyImpl)

COM_INTERFACE_ENTRY(IPropertyNotifySink)

END_COM_MAP()

public:

STDMETHOD(OnChanged)(DISPID dispid);

STDMETHOD(OnRequestEdit)(DISPID dispid){return S_OK;}

DWORD m_FontNotifyCookie;

void SetParent(CNiceTextCtrl *pParent){m_pParent = pParent;}

CNiceTextCtrl *m_pParent;

};

To your control, add a protected member variable that holds a sink object: CComObject<CFontNotifyImpl> *m_pFontNotifySink;

Initialize the sink object in your control's constructor: CComObject<CFontNotifyImpl>::CreateInstance(&m_pFontNotifySink);

m_pFontNotifySink->SetParent(this);

Initialize the font object for the control and hook up the sink interface. A good place to do this is in SetClientSite. SetClientSite is called when a control first begins to interact with a container, soon after the control object is created on behalf of a container. To override SetClientSite. add it as a member of your control class by adding the following to your control's header file inside your class declaration: STDMETHOD(SetClientSite)(LPOLECLIENTSITE pSite);

And implement it in your object's .cpp file like this: STDMETHODIMP CNiceTextCtrl::IOleObject_SetClientSite

(LPOLECLIENTSITE pSite)

{

HRESULT hr = CComControlBase::IOleObject_SetClientSite(pSite);

// Check to see if the container has an ambient font. If it does,

// clone it so your user can change the font of the control

// without changing the ambient font for the container. If there is

// no ambient font, create your own font object when you hook up a

// client site.

if(!m_pFont && pSite)

{

FONTDESC fd = _fontdesc;

CComPtr<IFont> pAF;

CComPtr<IFont> pClone;

if(SUCCEEDED(GetAmbientFont(&pAF)))

{

//clone the font

if(SUCCEEDED(pAF->Clone(&pClone)))

pClone->QueryInterface(IID_IFontDisp, (void**)&m_pFont);

}

else

{

OleCreateFontIndirect(&fd,IID_IFontDisp,(void**)&m_pFont);

}

//also, hook up a notify sink

if(m_pFont&&m_pFontNotifySink)

{

//smart pointers will release themselves

CComQIPtr<IConnectionPointContainer,&IID_IConnectionPointContainer>

pCPC(m_pFont);

CComPtr<IConnectionPoint> pCP;

if(pCPC)

{

pCPC->FindConnectionPoint(IID_IPropertyNotifySink,&pCP);

if(pCP)

{

pCP->Advise((IUnknown*)m_pFontNotifySink,

&m_pFontNotifySink->m_FontNotifyCookie);

}

}

}

}

return hr;

}

Have the sink interface update the control when font properties change: STDMETHODIMP CFontNotifyImpl::OnChanged(DISPID dispid)

{

ATLTRACE(_T("OnChanged sink: %x\n"),this);

m_pParent->FireViewChange();

return S_OK;

}

Additional query words: font ATL stock property

Keywords : kbfile kbprg kbsample kbusage kbATL200 kbATL210 kbCtrl kbFont kbATL300 kbGrpMFCATL

Issue type : kbinfo

Technology :

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有