Article ID: 166472
Article Last Modified on 8/5/2004
119591 How to Obtain Microsoft Support Files from Online Services
[
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.
static const FONTDESC _fontdesc =
{sizeof(FONTDESC), OLESTR("times new roman"), FONTSIZE( 14 ),
FW_BOLD, ANSI_CHARSET, TRUE, FALSE, FALSE };
PROP_ENTRY("Font", DISPID_FONT, CLSID_MSStockFont)
NOTE: You also need to #include <ATLCTL.H>
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);
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);
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;
}
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: kbinfo kbdownload kbfont kbprogramming kbfile kbsample kbctrl kbusage KB166472