加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

ActiveX控件的MFC设计之旅-第14步 .

发布时间:2020-12-16 22:49:07 所属栏目:大数据 来源:网络整理
导读:在VB设计ActiveX控件时,UserControl可以访问容器提供的扩展对象Extender,比如VB,就提供了Visible,Tag,Name等等标准的扩展属性和ToolTipText等等其它扩展属性。 那么在用MFC设计ActiveX控件时,是否也能利用到这个扩展属性呢? 能,COleControl提供了一个
在VB设计ActiveX控件时,UserControl可以访问容器提供的扩展对象Extender,比如VB,就提供了Visible,Tag,Name等等标准的扩展属性和ToolTipText等等其它扩展属性。
那么在用MFC设计ActiveX控件时,是否也能利用到这个扩展属性呢?
能,COleControl提供了一个函数LPDISPATCH GetExtendedControl()用来获得扩展对象的IDispatch接口。

注意: 容器并不一定要提供扩展对象,因此,GetExtendedControl也并不一定能返回有效的IDispatch接口,必须首先判断一下是否为NULL。 .继续用tppb作例子,我们测试三个扩展属性,Visible,Tag和Name 1.添加三个属性BOOl MyVisible,BSTR Tag,BSTR Name,都用get/set methods类型 2.通过获得扩展对象并访问扩展对象的属性来实现我们的这三个属性 BOOL CTppbCtrl::GetMyVisible() { // TODO: Add your property handler here BOOL b; DISPID dwDispID; LPDISPATCH lpdisp = GetExtendedControl(); if(lpdisp && GetDispID(lpdisp,"Visible",dwDispID)){ COleDispatchDriver PropDispDriver; PropDispDriver.AttachDispatch(lpdisp,FALSE); PropDispDriver.GetProperty(dwDispID,VT_BOOL,&b); PropDispDriver.DetachDispatch(); } if(lpdisp){ lpdisp->Release(); } return b; return TRUE; } void CTppbCtrl::SetMyVisible(BOOL bNewValue) { // TODO: Add your property handler here DISPID dwDispID; LPDISPATCH lpdisp = GetExtendedControl(); if(lpdisp && GetDispID(lpdisp,FALSE); PropDispDriver.SetProperty(dwDispID,bNewValue); PropDispDriver.DetachDispatch(); } if(lpdisp){ lpdisp->Release(); } SetModifiedFlag(); } BSTR CTppbCtrl::GetMyTag() { CString strResult; // TODO: Add your property handler here DISPID dwDispID; LPDISPATCH lpdisp = GetExtendedControl(); if(lpdisp && GetDispID(lpdisp,"Tag",VT_BSTR,&strResult); PropDispDriver.DetachDispatch(); } if(lpdisp){ lpdisp->Release(); } return strResult.AllocSysString(); } void CTppbCtrl::SetMyTag(LPCTSTR lpszNewValue) { // TODO: Add your property handler here DISPID dwDispID; LPDISPATCH lpdisp = GetExtendedControl(); if(lpdisp && GetDispID(lpdisp,FALSE); CString str = lpszNewValue; PropDispDriver.SetProperty(dwDispID,str); PropDispDriver.DetachDispatch(); } if(lpdisp){ lpdisp->Release(); } SetModifiedFlag(); } BSTR CTppbCtrl::GetMyName() { CString strResult; // TODO: Add your property handler here DISPID dwDispID; LPDISPATCH lpdisp = GetExtendedControl(); if(lpdisp && GetDispID(lpdisp,"Name",&strResult); PropDispDriver.DetachDispatch(); } if(lpdisp){ lpdisp->Release(); } return strResult.AllocSysString(); } void CTppbCtrl::SetMyName(LPCTSTR lpszNewValue) { // TODO: Add your property handler here DISPID dwDispID; LPDISPATCH lpdisp = GetExtendedControl(); if(lpdisp && GetDispID(lpdisp,str); PropDispDriver.DetachDispatch(); } if(lpdisp){ lpdisp->Release(); } SetModifiedFlag(); } BOOL CTppbCtrl::GetDispID(LPDISPATCH pdisp,LPCTSTR lpszName,long& dwDispID) { USES_CONVERSION; LPCOLESTR lpOleStr = T2COLE(lpszName); return SUCCEEDED(pdisp->GetIDsOfNames(IID_NULL,(LPOLESTR*)&lpOleStr,1,(long*)&dwDispID)); } 3.在VB中添加控件测试可以发现,改变Visible,Tag和Name属性,MyVisible,MyTag和MyName也随之改变。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读