ATL问题集- #9 如何做一个简单的控件容器?

类别:VC语言 点击:0 评论:0 推荐:
 

/*此文是我将网上的一些文章,自已遇到的问题进行整理,有些是翻译,所有只供学习讨论,如有版权还属于原作者——作者:黄森堂*/

#9 如何做一个简单的控件容器?

MFC控件向导支持简单框架控件,ATL 2.1不支持,以下代码演示在ATL 3.0(VC6环境)中实现简单的ISimpleFrameSite的容器框架.

1.定义两个宏(主要是为了方便)

#define RELEASE_OBJECT( ptr )if (ptr) { IUnknown *pUnk = (ptr); (ptr) = NULL; pUnk->Release(); } #define QUICK_RELEASE(ptr) if (ptr) ((IUnknown *)ptr)->Release();

2.在控件类中加入成员变量:

ISimpleFrameSite* m_pSimpleFrameSite;

3.在控件类的构造函数中加入:

m_pSimpleFrameSite = NULL;

4.在控件类的析构函数中加入:

QUICK_RELEASE(m_pSimpleFrameSite);

5.覆盖IOleObject::SetClientSite:

STDMETHOD(SetClientSite)(IOleClientSite *pClientSite) { HRESULT hr = IOleObjectImpl<你的控件类>::SetClientSite(pClientSite); RELEASE_OBJECT(m_pSimpleFrameSite); if( pClientSite != NULL ) pClientSite->QueryInterface( IID_ISimpleFrameSite, (void **)&m_pSimpleFrameSite); return hr; }

6.在控件类中加入成员变量:

WNDPROC m_fnOldWindowProc;

7.覆盖Create函数:

HWND Create( HWND hWndParent, RECT& rcPos, LPCTSTR szWindowName = NULL, DWORD dwStyle = WS_CHILD | WS_VISIBLE, DWORD dwExStyle = 0, UINT nID = 0 ) { HWND hWnd = CWindowImpl<你的控件类>::Create( hWndParent, rcPos, szWindowName, dwStyle, dwExStyle, nID); if (hWnd) { ::SetProp(hWnd, "ABC", static_cast<HANDLE> ((你的控件类*) this)); m_fnOldWindowProc = (WNDPROC) ::SetWindowLong( hWnd, GWL_WNDPROC, (LONG) SimpleFrameWindowProc); } return hWnd; }

8.在你的控件类中加入定义:

static LRESULT CALLBACK SimpleFrameWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );

9.加入实理代码:

LRESULT CALLBACK 你的控件类::SimpleFrameWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) { 你的控件类* pThis = static_cast<你的控件类*> (::GetProp(hWnd, "ABC")); WNDPROC fnOldWindowProc = pThis->m_fnOldWindowProc; LRESULT lResult; BOOL bProcess = TRUE; DWORD dwCookie; HRESULT hr = E_FAIL; if(pThis->m_pSimpleFrameSite) { hr = pThis->m_pSimpleFrameSite->PreMessageFilter(hWnd, uMsg, wParam, lParam, &lResult, &dwCookie); bProcess = (hr != S_FALSE); } if (bProcess) lResult = fnOldWindowProc(hWnd, uMsg, wParam, lParam); if(pThis->m_pSimpleFrameSite && bProcess) { pThis->m_pSimpleFrameSite->PostMessageFilter( hWnd, uMsg, wParam, lParam, &lResult, dwCookie); } return lResult; }

10.在.RGS文件中的MiscStatus中新增加OR 0x10000

'MiscStatus' = s '0' { '1' = s '131473' }

to:

'MiscStatus' = s '0' { '1' = s '197009' }

 

本文地址:http://com.8s8s.com/it/it3542.htm