在 MFC 中创建 child window 的真正方法是什么?

What is the true way to create a child window in MFC?

我已经为具有 document/view 体系结构的 SDI MFC 应用程序创建了项目。

现在,我想把我的主要 window 和棋盘分开(我想写一个棋盘游戏)。我想把板子放在 child window 上,以获得进一步的轻量级能力,使它们居中取决于 parent(main) window.

的大小

实际上我已经这样做了,但现在我遇到了问题 - 我还想等待鼠标的消息。

class CGameView {
  // ...
  CGameView()
  {
    childWnd = new CWnd;
  }

  ~CGameView()
  {
    delete childWnd;
  }

  void CGameView::OnSize(UINT nType, int cx, int cy) {
    CGameDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    if(!pDoc)
      return;
    // I bind the height of child window with height of parent
    RECT rcClient;
    GetClientRect(&rcClient);
    rcClient.left = 0;
    rcClient.right = pDoc->getWidth();
    childWnd->MoveWindow(&rcClient);

    childWnd->CenterWindow();

    return;
  }

  int CGameView::OnCreate(LPCREATESTRUCT lpCreateStruct)
  {
    if (CView::OnCreate(lpCreateStruct) == -1)
      return -1;

    CGameDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    if(!pDoc)
      return -1;

    childWnd->Create(NULL, NULL, WS_CHILD | WS_VISIBLE,
       CRect(0, 0, pDoc->getWidth(), pDoc->getHeight()), this, 0);

    return 0;
  }

  void CGameView::OnDraw(CDC* pDC)
  {
    CGameDoc* pDoc = GetDocument();

    ASSERT_VALID(pDoc);
    if (!pDoc)
        return;

    CRect rcClient;
    GetClientRect(&rcClient);
    CClientDC cdc(childWnd);
  // The pOffScreen contains the bitmap that I just copy to specified CDC
    pOffScreen->print(&cdc, &rcClient);

  }
}

?我非常关心我 child window 方式的正确性。 ?现在,我不知道如何通过我的 child window 漂亮地收听鼠标消息?

一般来说,不应该直接创建CWnd类型的对象。

相反,创建 CWnd 的子类并创建该类型的对象。然后你可以使用 class.

的成员函数来管理鼠标、绘画和任何其他事件
class CBoardWnd : public CWnd
{
    DECLARE_MESSAGE_MAP()
    //...
};
BEGIN_MESSAGE_MAP(CBoardWnd, CWnd)
    ON_WM_MOUSEMOVE(...)
    //...
END_MESSAGE_MAP()