// 从CDockablePane派生出CScrollPane
class CScrollPane : public CDockablePane
{
DECLARE_DYNAMIC(CScrollPane)
public:
CScrollPane();
virtual ~CScrollPane();
BOOL SetView(CRuntimeClass* pViewClass);
CFormView* GetView() { return m_pView; }
private:
CFormView* m_pView;
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnSize(UINT nType, int cx, int cy);
};
// ScrollPane.cpp : 实现文件
//
#include "stdafx.h"
#include "MyPane.h"
#include "ScrollPane.h"
// CScrollPane
IMPLEMENT_DYNAMIC(CScrollPane, CDockablePane)
CScrollPane::CScrollPane()
{
m_pView = NULL;
}
CScrollPane::~CScrollPane()
{
}
BOOL CScrollPane::SetView(CRuntimeClass* pViewClass)
{
if (pViewClass == NULL) return FALSE;
m_pView = (CFormView*)pViewClass->CreateObject();
if (m_pView == NULL) return FALSE;
return TRUE;
}
BEGIN_MESSAGE_MAP(CScrollPane, CDockablePane)
ON_WM_CREATE()
ON_WM_SIZE()
END_MESSAGE_MAP()
// CScrollPane 消息处理程序
int CScrollPane::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDockablePane::OnCreate(lpCreateStruct) == -1)
return -1;
if (m_pView)
{
RECT rect;
GetClientRect(&rect);
if (!((CWnd*)m_pView)->Create(NULL, NULL, WS_CHILD | WS_CHILD, rect, this, 0x01))
{
AfxMessageBox(TEXT("CScrollPane Can't create view !"));
return -1;
}
}
return 0;
}
void CScrollPane::OnSize(UINT nType, int cx, int cy)
{
CDockablePane::OnSize(nType, cx, cy);
if (m_pView && ::IsWindow(m_pView->m_hWnd))
{
CRect rect;
GetClientRect(&rect);
m_pView->MoveWindow(rect);
m_pView->ShowWindow(SW_SHOW);
}
}
注意:
BOOL SetView(CRuntimeClass* pViewClass);
这里添加的是CFormView, CFormView要重载OnMouseActive函数, 否则CDockablePane处于浮动状态的时候, 点击CFormView会导致程序崩溃。
// CMyFormView 消息处理程序
int CMyFormView::OnMouseActivate(CWnd* pDesktopWnd, UINT nHitTest, UINT message)
{
CFrameWnd* pParentFrame = GetParentFrame();
if (pParentFrame == pDesktopWnd)
return CFormView::OnMouseActivate(pDesktopWnd, nHitTest, message);
else
return CWnd::OnMouseActivate(pDesktopWnd, nHitTest, message);
}