1. 程式人生 > >基於對話方塊的MFC專案實現視窗分割

基於對話方塊的MFC專案實現視窗分割

1. 新建一個MFC對話方塊程式MySplitter。 再插入兩個Dialog資源 ,這裡一定要選擇IDD_FORMVIEW類別的對話方塊,對這兩個對話方塊分別新建CMyFormView0 和CMyFormView1,基類別選CDialog,一定要選擇CFormView。

2.主對話方塊類MySplitterDlg.h中新增兩個成員變數

    CFrameWnd*      m_pMyFrame;    // 分隔視窗  
    CSplitterWnd    m_cSplitter;     // 左右分隔  

3. CMySplitterDlg中增加WM_CREATE的訊息響應,編輯OnCreate()
int CMySplitterDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CDialog::OnCreate(lpCreateStruct) == -1)
        return -1;
    // 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,_T(""), 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;
}


4.在CMySplitterDlg::OnInitDialog()中顯示Frame

int CMySplitterDlg::OnInitDialog()
{
CDialog::OnInitDialog();

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


return TRUE;
}