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

本文介绍了如何在MFC的对话框窗口中创建静态分割窗口。主要步骤包括在OnCreate函数中注册新的窗口类,使用new操作符创建CFrameWnd并初始化,以及以这个CFrameWnd为父窗口创建CSplitterWnd。同时提到了在对话框程序中创建两个IDD_FORMVIEW类别的对话框作为子视图,并展示了相关代码示例。

        静态分割窗口在多文档和单文档里面非常简单,唯一要说的技术单文档窗口分割直接在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,即就是剩余窗口宽度。

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


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值