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

本文介绍了如何在MFC环境下设计ActiveX控件,并利用COleControl的GetExtendedControl函数获取扩展对象IDispatch接口,进而访问容器提供的属性如Visible、Tag和Name等。通过实现get/set方法,实现在MFC中通过代理属性操作容器对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 在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, "Visible", dwDispID)){
        COleDispatchDriver PropDispDriver;
        PropDispDriver.AttachDispatch(lpdisp, FALSE);
        PropDispDriver.SetProperty(dwDispID, VT_BOOL, 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", dwDispID)){
        COleDispatchDriver PropDispDriver;
        PropDispDriver.AttachDispatch(lpdisp, FALSE);
        PropDispDriver.GetProperty(dwDispID, 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, "Tag", dwDispID)){
        COleDispatchDriver PropDispDriver;
        PropDispDriver.AttachDispatch(lpdisp, FALSE);
        CString str = lpszNewValue;
        PropDispDriver.SetProperty(dwDispID, VT_BSTR, 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", dwDispID)){
        COleDispatchDriver PropDispDriver;
        PropDispDriver.AttachDispatch(lpdisp, FALSE);
        PropDispDriver.GetProperty(dwDispID, VT_BSTR, &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, "Name", dwDispID)){
        COleDispatchDriver PropDispDriver;
        PropDispDriver.AttachDispatch(lpdisp, FALSE);
        CString str = lpszNewValue;
        PropDispDriver.SetProperty(dwDispID, VT_BSTR, 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, 0, (long*)&dwDispID));
}

3.在VB中添加控件测试可以发现,改变Visible,Tag和Name属性,MyVisible,MyTag和MyName也随之改变。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值