1、给控件类加上IPerPropertyBrowsingImpl接口
public IPerPropertyBrowsingImpl
在BEGIN_COM_MAP/END_COM_MAP()中间加上
COM_INTERFACE_ENTRY_IMPL(IPerPropertyBrowsing)
2、 在该类中添加下列函数
(A)STDMETHOD(MapPropertyToPage)(DISPID dispid, CLSID *pClsid);
(B)STDMETHOD(GetPredefinedStrings)(DISPID dispid, CALPOLESTR *pCaStringOut, CADWORD *pCaCookiesOut);
(C)STDMETHOD(GetPredefinedValue)(DISPID dispid, DWORD dwCookie, VARIANT *pVarOut);
(D) STDMETHOD(GetDisplayString)(DISPID dispid, BSTR *pBstr);
STDMETHODIMP CCameraCtrl::MapPropertyToPage(DISPID dispid, CLSID *pClsid)
{
switch (dispid)
{
case dispidDeviceSource:
return E_NOTIMPL; // 如果要自己实现下拉列表则返回E_NOIMPL
default:
break;
}
return IPerPropertyBrowsingImpl<CCameraCtrl>::MapPropertyToPage(dispid, pClsid);
}
STDMETHODIMP CCameraCtrl::GetPredefinedStrings(DISPID dispid, CALPOLESTR *pCaStringOut, CADWORD *pCaCookiesOut)
{
USES_CONVERSION;
HRESULT hr = S_OK;
if ((NULL == pCaStringOut) || (NULL == pCaCookiesOut))
{
return E_POINTER;
}
if (dispid == dispidDeviceSource)
{
ULONG ulElems = 3;
pCaStringOut->pElems = (LPOLESTR*)::CoTaskMemAlloc(sizeof(LPOLESTR) * ulElems);
if (NULL == pCaStringOut->pElems)
{
return E_OUTOFMEMORY;
}
pCaCookiesOut->pElems = (DWORD*)::CoTaskMemAlloc(sizeof(DWORD*) * ulElems);
if (NULL == pCaCookiesOut->pElems)
{
CoTaskMemFree(pCaStringOut->pElems);
return E_OUTOFMEMORY;
}
pCaStringOut->cElems = ulElems;
pCaCookiesOut->cElems = ulElems;
char *szName[3] = {"Device0", "Device1", "Device2"};
pCaStringOut->pElems[0] = ATLA2WHELPER((LPWSTR)::CoTaskMemAlloc((sizeof(szName[0]) + 1) * 2), szName[0], lstrlen(szName[0]) + 1);
pCaStringOut->pElems[1] = ATLA2WHELPER((LPWSTR)::CoTaskMemAlloc((sizeof(szName[0]) + 1) * 2), szName[1], lstrlen(szName[1]) + 1);
pCaStringOut->pElems[2] = ATLA2WHELPER((LPWSTR)::CoTaskMemAlloc((sizeof(szName[0]) + 1) * 2), szName[2], lstrlen(szName[2]) + 1);
pCaCookiesOut->pElems[0] = 0;
pCaCookiesOut->pElems[1] = 1;
pCaCookiesOut->pElems[2] = 2;
hr = S_OK;
}
return hr;
}
STDMETHODIMP CCameraCtrl::GetPredefinedValue(DISPID dispid, DWORD dwCookie, VARIANT *pVarOut)
{
BOOL bResult = FALSE;
switch (dispid)
{
case dispidDeviceSource:
VariantInit(pVarOut);
pVarOut->vt = VT_I4;
pVarOut->lVal = dwCookie;
bResult = TRUE;
break;
default:
break;
}
return bResult;
}
STDMETHODIMP CCameraCtrl::GetDisplayString(DISPID dispid, BSTR *pBstr)
{
USES_CONVERSION;
HRESULT hr = S_FALSE;
switch (dispid)
{
case dispidDeviceSource:
switch (m_lDeviceSource)
{
case 0:
*pBstr = ::SysAllocString(T2OLE("Device0"));
break;
case 1:
*pBstr = ::SysAllocString(T2OLE("Device1"));
break;
case 2:
*pBstr = ::SysAllocString(T2OLE("Device2"));
break;
default:
break;
}
hr = S_OK;
break;
default:
break;
}
return hr;
}