WEB开发网
开发学院软件开发VC think window procedure 阅读

think window procedure

 2010-06-15 20:40:13 来源:WEB开发网   
核心提示:2.2 往底层一点,我们可以说CWnd::WindowProc()是现在的window procedure,think window procedure(2),它是一个template method,被你"programming by difference"的消息,哈哈,我看到了,会被它交给CWnd

2.2 往底层一点,我们可以说CWnd::WindowProc()是现在的window procedure,它是一个template method,被你"programming by difference"的消息,会被它交给CWnd::OnWndMsg()处理,缺省的,会被它交给CWnd::DefWindowProc()处理。当然,上面说的没有考虑多态的情况,其实CWnd::OnWndMsg()和CWnd::DefWindowProc()都是虚函数。我们也注意到CWnd::DefWindowProc()中调用了::DefWindowProc(),也就是Win32 API的DefWindowProc()。

class CWnd : public CCmdTarget
{
...
protected:
  // for processing Windows messages
  virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);
  virtual BOOL OnWndMsg(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pResult);
...
};
///template method
LRESULT CWnd::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
  LRESULT lResult = 0;
  if (!OnWndMsg(message, wParam, lParam, &lResult))
    lResult = DefWindowProc(message, wParam, lParam);
  return lResult;
}
//primitive method
LRESULT CWnd::DefWindowProc(UINT nMsg, WPARAM wParam, LPARAM lParam)
{
  if (m_pfnSuper != NULL)
    return ::CallWindowProc(m_pfnSuper, m_hWnd, nMsg, wParam, lParam);
  ...
}

2.3 往更底层,来看看MFC预注册的那些类,window procedure是谁。注意,Pre-Registers Window Classes没有什么神秘的,因为Window Classes就是一个struct,而当你想用某个Pre-Registers Window Classes时,无非是传一个parameter过去,某段程序一判断,给wc结构赋值,调用AfxRegisterClass( & wc),OK。哈哈,我看到了,用的还是Win32 API的::DefWindowProc()。

BOOL CWnd::CreateEx(DWORD dwExStyle, LPCTSTR lpszClassName,
  LPCTSTR lpszWindowName, DWORD dwStyle,
  int x, int y, int nWidth, int nHeight,
  HWND hWndParent, HMENU nIDorHMenu, LPVOID lpParam)
{
  CREATESTRUCT cs;
  cs.lpszClass = lpszClassName;
  ...

  PreCreateWindow(cs); //########pass a cs with lpszClass null
  ...
}
BOOL CWnd::PreCreateWindow(CREATESTRUCT& cs) //########pass a cs with lpszClass NULL
{
  if (cs.lpszClass == NULL) //########pass a cs with lpszClass NULL
  {
    // make sure the default window class is registered
    VERIFY(AfxDeferRegisterClass(AFX_WND_REG));//########pass a para AFX_WND_REG
    // no WNDCLASS provided - use child window default
    ASSERT(cs.style & WS_CHILD);
    cs.lpszClass = _afxWnd;
  }
  return TRUE;
}
#define AfxDeferRegisterClass(fClass) AfxEndDeferRegisterClass(fClass)
BOOL AFXAPI AfxEndDeferRegisterClass(LONG fToRegister)//########pass a para AFX_WND_REG
{
  ...
  // common initialization
  WNDCLASS wndcls;
  memset( & wndcls, 0, sizeof(WNDCLASS));
  wndcls.lpfnWndProc = DefWindowProc; //########## here,Win32 API ::DefWindowProc()
  wndcls.hInstance = AfxGetInstanceHandle();
  wndcls.hCursor = afxData.hcurArrow;
  ...
  if (fToRegister & AFX_WND_REG) //########pass a para AFX_WND_REG
  {
    wndcls.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
    wndcls.lpszClassName = _afxWnd; //########pass a para _afxWnd
    AfxRegisterClass( & wndcls);
    ...
  }
  ...
}
const TCHAR _afxWnd[] = AFX_WND;
#define AFX_WND    AFX_WNDCLASS("Wnd")
#define AFX_WNDCLASS(s) \
  _T("Afx") _T(s) _T("42") _STATIC_SUFFIX _UNICODE_SUFFIX _DEBUG_SUFFIX

Tags:think window procedure

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接