Using Fonts in ATL Controls[2]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 vcmfc 的 blog

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

    本文关键:Using Fonts in ATL Controls
     

    本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

    go top