ActiveX控制項的MFC設計之旅-第18步

來源:互聯網
上載者:User
在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也隨之改變。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.