CSplitterWnd窗口分割之——静态分割(一)

        静态分割窗口在多文档和单文档里面非常简单,唯一要说的技术单文档窗口分割直接在MainFrm中,而多文档的窗口分割操作是要在ChildFrm中处理。

         我今天想说的是在对话框窗口中如何使用静态分割。

以下三个步骤在对话框中创建分割窗:

1. In the OnCreate function or your CDialog, register a new WindowClass by calling "AfxRegisterWndClass".

    在你的CDialog类的OnCreate函数中使用AfxRegisterWndClass注册一个新的窗口类。

2. Create a new CFrameWnd by using the "new" operator and initialize it.

    使用new操作符创建一个CFrameWnd,并对其进行初始化。

3. Create your splitter by using the new CFrameWnd you just created as the parent.

    在步骤2中创建的CFrameWnd上创建分割窗。


1. 新建一个MFC对话框程序DialogSplitter。 再插入两个Dialog资源 ,这里一定要选择IDD_FORMVIEW类别的对话框(选择资源视图项目资源的Dialog文件夹右键->添加资源->点击Dialog前面加号->选择IDD_FORMVIEW即可),分别为两个对话框新建类CMyFormView0 和CMyFormView1,基类别选CDialog,一定要选择CFormView。

2. CDialogSplitterDlg中增加

public:
    CFrameWnd* m_pMyFrame;
    CSplitterWnd m_cSplitter;

WM_CREATE的消息响应,编辑OnCreate()

int CDialogSplitterDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CDialog::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  Add your specialized creation code here

	// 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));

	// Create the frame window with "this" as the parent
	m_pMyFrame = new CFrameWnd;
	m_pMyFrame->Create(strMyClass,L"", WS_CHILD,   CRect(0,0,300,300), this);
	m_pMyFrame->ShowWindow(SW_SHOW);

	// and finally, create the splitter with the frame as the parent
	m_cSplitter.CreateStatic(m_pMyFrame,1, 2); //在Frame里切分视图窗口为1×2,就是一行两列
	m_cSplitter.CreateView(0,0, RUNTIME_CLASS(CMyFormView0),   CSize(100,100), NULL);//第一行一列
	m_cSplitter.CreateView(0,1, RUNTIME_CLASS(CMyFormView1), CSize(100,100), NULL);//第一行二列

	return 0;
}

3. 在CDialogSplitterDlg::OnInitDialog()中显示Frame

BOOL CDialogSplitterDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here

	CRect cRect;
	GetWindowRect(&cRect);
	ScreenToClient(&cRect);
	m_pMyFrame->MoveWindow(&cRect);
	m_pMyFrame->ShowWindow(SW_SHOW);

	return TRUE;  // return TRUE  unless you set the focus to a control
}
        这里谈谈我的理解,在对话框中使用SplitterWnd实际上还是在CFrameWnd,为什么这么说呢,因为在Dialog的OnCreate中我们实际上是创建了一个CFrameWnd类型的窗口作为Dialog的子窗口,是从Dialog的0,0点开始的一个300*300大小的一个子窗口,然后在这个子窗口上做的窗口分割,注释掉OnInitDialog中添加的代码就可以看到真相啦,该段代码实际上就是获取Dialog窗口大小,把CFrameWnd大小变成Dialog的大小,使其填充整个Dialog(CRect(0,0,300,300)随便改不影响最终结果)。还要补充说明一点窗口分割的SIZE,我们分割成一行两列,第一列和第二列的大小都是CSize(100,100),也就是100*100,;实际结果是因为只有一行,所以分割后的两个视图窗口高度是一样的,即为父窗口的高度,而宽度第一列的宽度被设置为100,而第二列的宽度则自动被设置为:父窗口宽度-100,即就是剩余窗口宽度。

       动态分割原理差不多,如有需要自行修改。


你可能感兴趣的:(MFC)