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);
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 : kbfile kbprg kbsample kbusage kbatl200 kbatl210 kbctrl kbfont kbatl300 kbgrpmfcatl
issue type : kbinfo