/*此文是我将网上的一些文章,自已遇到的问题进行整理,有些是翻译,所有只供学习讨论,如有版权还归原作者——作者:黄森堂*/
#4 如何注册控件?这是一个很常见的问题,最简单用Winodws自带的Regsvr32或其它工具等,其原理是利用控件的RegsiterServer函数与UnregsiterServer来实现注册与取消注册,以下是代码实现注册:
DWORD RegisterServer( char* szPath )
{
HINSTANCE hInstance = ::LoadLibrary( szPath );
if ( 0 == hInstance )
{
return ::GetLastError();
}
typedef void (FAR PASCAL *REGSERVER)(void);
REGSERVER RegServer = (REGSERVER) ::GetProcAddress( hInstance, _T( "DllRegisterServer" ));
if ( 0 == RegServer )
{
::FreeLibrary( hInstance );
return ::GetLastError();
}
RegServer();
::FreeLibrary( hInstance );
}
答:你只要重载IOleObject接口的SetExtent方法.
// NoteCtl.h : Declaration of the CNoteCtl
...
class ATL_NO_VTABLE CNoteCtl :
...
STDMETHOD(SetExtent)(DWORD dwDrawAspect, SIZEL *psizel)
{
ATLTRACE(_T("SetExtent sizing control to 1000x1000\n"));
psizel->cx = psizel->cy = 1000;
return IOleObjectImpl<CNoteCtl>::SetExtent(dwDrawAspect, psizel);
}
...
};
void CMyCtrl::SetNewSize (int cx, int cy)
{
SIZEL szlPixels, szlMetric;
szlPixels.cx = cx;
szlPixels.cy = cy;
AtlPixelToHiMetric(&szlPixels, &szlMetric);
// IOleObjectImpl
SetExtent(DVASPECT_CONTENT, &szlMetric);
// update control sizing...
m_rcPos.right= m_rcPos.left + cx;
m_rcPos.bottom= m_rcPos.top + cy;
if (m_spInPlaceSite != NULL) {
// needed for IE to accept the resizing
m_spInPlaceSite->OnPosRectChange(&m_rcPos);
}
SetFocus();
答:ATL提供了CComControlBase::GetAmbientUserMode()来取得其状态.
BOOL IsUserMode()
{
BOOL bUserMode = TRUE;
HRESULT hRet = GetAmbientUserMode(bUserMode);
if (FAILED(hRet) || bUserMode)
{
return TRUE;
}
return FALSE;
}
答:COleControl提供了两个方法来辅助实现:AmbientUserMode与GetNotSupported,AmbientUserMode()来取得当前容器的状态,是在运行时还是设计时;而GetNotSupported()能产生CTL_E_GETNOTSUPPORTED自动化异常.
HRESULT CNoteCtl::get_RuntimeOnly( long* pTest )
{
BOOL bUserMode;
GetAmbientUserMode( bUserMode );
if (! bUserMode )
return CTL_E_GETNOTSUPPORTED;
*pTest = 100;
return S_OK;
}
本文地址:http://com.8s8s.com/it/it3558.htm