本文主要介绍:在MFC中创建停靠窗口。
一、创建停靠窗口类
创建后的ObjectWnd.h和ObjectWnd.cpp文件如下:
#pragma once #include "afxdockablepane.h" class CObjectWnd : public CDockablePane { public: CObjectWnd(void); ~CObjectWnd(void); DECLARE_MESSAGE_MAP() afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct); afx_msg void OnSize(UINT nType, int cx, int cy); };
#include "stdafx.h" #include "ObjectWnd.h" CObjectWnd::CObjectWnd(void) { } CObjectWnd::~CObjectWnd(void) { } BEGIN_MESSAGE_MAP(CObjectWnd, CDockablePane) ON_WM_CREATE() ON_WM_SIZE() END_MESSAGE_MAP() int CObjectWnd::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CDockablePane::OnCreate(lpCreateStruct) == -1) return -1; return 0; } void CObjectWnd::OnSize(UINT nType, int cx, int cy) { CDockablePane::OnSize(nType, cx, cy); }二、MainFrm中设置停靠窗口
1. MainFrm.h中添加头文件ObjectWnd.h,并定义CObjectWnd类的对象
CObjectWnd m_wndObject;2. MainFrm.cpp中添加响应程序
BOOL CMainFrame::CreateDockingWindows() 函数中添加:
CString strObjectView("面向对象"); if (!m_wndObject.Create(strObjectView, this, CRect(0, 0, 200, 200), TRUE, 1001, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | CBRS_LEFT | CBRS_FLOAT_MULTI)) { TRACE0("未能创建“面向对象”窗口\n"); return FALSE; // 未能创建 }int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) 函数中添加
m_wndObject.EnableDocking(CBRS_ALIGN_ANY); //使可停靠与浮动 DockPane(&m_wndObject);