I. 窗体类消息处理
1. 在WndProc中调用窗体类的方法:
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
a. 将WndProc声明为窗体类中的静态函数;
b. 在创建窗体时,先用SetWindowLong( HWND hWnd, int nIndex, LONG dwNewLong )在窗体中保存窗体实例的指针;
c. 在WndProc中用LONG GetWindowLong(HWND hWnd, int nIndex) 取得窗体类指针。
class COwner { ... public: BOOL CreateWindow(HWND hWndParent, int nCmdShow); static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); ... } BOOL COwner::CreateWindow(HWND hWndParent, int nCmdShow) { m_hWnd = CreateWindow(szWindowClass, szTitle, WS_POPUP, m_rect.left, m_rect.top, Width(), Height(), hWndParent, NULL, m_hInstance, this); if (!m_hWnd) { return FALSE; } ::SetWindowLong( m_hWnd, GWL_USERDATA, (LONG)this); ShowWindow(m_hWnd, nCmdShow); return TRUE; } LRESULT CALLBACK COwner::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { COwner *pOwner = (COwner *)GetWindowLong(hWnd, GWL_USERDATA); //Call function of COwner from pOwner to access the owner ... }