功能说明
在一个主的对话框上,把对话框分割成左右2个窗口。
创建子窗口
1)子窗口的属性
- 子窗口的类,一定要继承CFormView
//例
class CSubView02 : public CFormView
{
//....
}
分割主窗口
1)定义FRAME对象,和分割窗口用的CSplitterWnd对象
CFrameWnd *m_pMyFrame;
CSplitterWnd m_cSplitter;
- 追加WM_CREATE和WM_DESTROY的消息
在OnCreate(…)里分割窗口
int CCVTDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: ここに特定な作成コードを追加してください。
// Because the CFRameWnd needs a window class, we will create a new one. I just copied the sample from MSDN Help.
// When using it in your project, you may keep CS_VREDRAW and CS_HREDRAW and then throw the other three parameters.
CString strMyClass = AfxRegisterWndClass(CS_VREDRAW | CS_HREDRAW,
::LoadCursor(NULL, IDC_ARROW), (HBRUSH) ::GetStockObject(WHITE_BRUSH),
::LoadIcon(NULL, IDI_APPLICATION));
CRect cRect;
GetWindowRect(&cRect);
// Create the frame window with "this" as the parent
m_pMyFrame = new CFrameWnd;
m_pMyFrame->Create(strMyClass, _T(""), WS_CHILD, CRect(0,0,1161,656), this);
m_pMyFrame->ShowWindow(SW_SHOW);
// and finally, create the splitter with the frame as the parent
m_cSplitter.CreateStatic(m_pMyFrame, 1, 2);
m_cSplitter.CreateView(0, 0, RUNTIME_CLASS(CSubView01), CSize(460, 650), NULL);
m_cSplitter.CreateView(0, 1, RUNTIME_CLASS(CSubView02), CSize(690, 650), NULL);
return 0;
}
在OnDestroy()里delete对象
void CCVTDlg::OnDestroy()
{
CDialog::OnDestroy();
// TODO: ここにメッセージ ハンドラー コードを追加します。
if (m_pMyFrame)
{
delete m_pMyFrame;
}
}
画面大小调整
在主窗口和子窗口都加上WM_SIZE的消息
1)主窗口里可以设置左右子窗口的大小
void CCVTDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
CRect cRect3(0, 0, cx, cy);
m_pMyFrame->MoveWindow(&cRect3);
if (nType != SIZE_MINIMIZED && m_bInitSplitter)
{
m_cSplitter.SetRowInfo(0, cy, 0);
m_cSplitter.SetColumnInfo(0, cx *2/ 5, 50);
m_cSplitter.SetColumnInfo(1, cx *3/ 5, 50);
//RecalcLayout()一定要
m_cSplitter.RecalcLayout();
m_bInitSplitter = false;
}
// TODO : ここにメッセージ ハンドラ コードを追加します。
Invalidate();
}
2)子窗口里可以调整画面上各个控件的位置
//子窗口画面滚动条显示不正常时
this->GetWindowRect(&rectMain);
SetScrollSizes(MM_HIMETRIC, CSize(rectMain.Width(), rectMain.Height()));
//控件的位置调整
AdjustDlgSize();