VC下劃分窗口並固定

 

前幾天爲了程序的需要,連夜連晚的學習了VC下劃分窗口並固定的方法。過程是痛苦的,結果是可喜了。現將此方法總結一下,自己也可以再熟悉下。

程序所採用平臺爲Microsoft Visual C++6.0,將要得到如下效果

 

首先建立MFC AppWizard(exe),取名字爲CView,下一步,選擇單文檔“Single document”,其他默認,點擊“完成”。

點擊ResourceView,在Dialog下插入兩個個Dialog窗口用於稍後的分割後的窗口,ID號分別改爲“IDD_FORMVIEW1”和“IDD_FORMVIEW2”。記得及時保存。 這裏有個注意的地方,在建立好窗口後,在屬性裏要將其Styles改成Child,不然程序不能運行的。這也是我當初沒注意到的地方,浪費了不少時間。

單擊ClassView標籤,添加2個類,當然也可以從ClassWizard中添加,名字叫FormView1,類選擇CFormView,Dialog ID選擇IDD_FORMVIEW1,另一個類叫FormView2,ID選擇IDD_FORMVIEW2。

在CMainFrame中添加兩個成員變量,類型都爲CSplitterWnd,變量名分別爲m_splitter和m_splitter1,然後添加一個虛函數OnCreateClient,點擊編輯代碼,將如下代碼更改

BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
    //創建一個靜態分欄窗口,分爲一行二列
    if(m_splitter.CreateStatic(this,1,2)==NULL)
        return FALSE;

    //將CCSplitterWndView連接到0行0列窗格上
    m_splitter.CreateView(0,0,RUNTIME_CLASS(CCViewView),CSize(600,600), pContext);

    if(m_splitter1.CreateStatic(&m_splitter,2,1,WS_CHILD|WS_VISIBLE,
        m_splitter.IdFromRowCol(0, 1))==NULL)
        return FALSE; //將第0行1列再分開2行1列
    //將FormView1類連接到第二個分欄對象的0行0列
    m_splitter1.CreateView(0,0,RUNTIME_CLASS(FormView1),CSize(200,400),pContext);
    //將FormView2類連接到第二個分欄對象的1行0列
    m_splitter1.CreateView(1,0,RUNTIME_CLASS(FormView2),CSize(200,200),pContext);
    return TRUE;

}

添加頭文件

#include "CViewDoc.h"
#include "CViewView.h"
#include "FormView1.h"
#include "FormView2.h"

OK,編輯運行。

但上述程序只完成了劃分,沒實現固定。我來介紹固定窗口的方法。

方法有兩種,但都是重載了窗口類,通過重載類來使窗口固定。

第一種方法:

插入->新建類->generic class  

name: MySplitter

Derived Form :CSplitterWnd  as public

OK生成。

這時需要在CMainFrame.h 中 添加頭文件 #include "MySplitte.h"

然後修改當初我們用CSplitterWnd定義的m_splitter, m_splitter1爲 MySplitter m_splitter, m_splitter1

這是很重要的一步。

修改 MySplitter.h

class MySplitter :: public CSplitterWnd
{
public:
 MySplitter1();
 virtual ~MySplitter();
 afx_msg UINT OnNcHitTest(CPoint point); //聲明固定窗口函數

 DECLARE_MESSAGE_MAP() 

};

在 MySplitter.cpp 中添加如下代碼

BEGIN_MESSAGE_MAP(MySplitter, CSplitterWnd)
 //{{AFX_MSG_MAP(MySplitter)
 ON_WM_NCHITTEST()    //聲明固定窗口消息
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()

 

UINT MySplitter::OnNcHitTest(CPoint point)   //固定窗口的實現 
{
 // TODO: Add your message handler code here and/or call default
 return HTNOWHERE;
}

----- OK,編譯運行。

第二種方法:

運行類嚮導--add class--new

name: MySplitter

Base class : generic CWnd                      OK,建立類。

將MySplitter.h 和 MySplitter.cpp中所有 CWnd 替換成CSplitterWnd如

BEGIN_MESSAGE_MAP(MySplitter2, CWnd)

BEGIN_MESSAGE_MAP(MySplitter2, CSplitterWnd)

class MySplitter2 : public CWnd

class MySplitter2 : public CSplitterWnd

這時需要在CMainFrame.h 中 添加頭文件 #include "MySplitte.h"

然後修改當初我們用CSplitterWnd定義的m_splitter, m_splitter1爲 MySplitter m_splitter, m_splitter1

這是很重要的一步。

然在在類嚮導裏添加WM_NCHITTEXT消息.編輯函數內容

UINT MySplitter::OnNcHitTest(CPoint point)   //直接實現窗口的固定,而聲明全由類嚮導完成
{
 // TODO: Add your message handler code here and/or call default
 return HTNOWHERE;
}

----- OK,編譯運行。

------------------------------------------------------------------------------------------------------------------------------------------------------------------------

PS:

以上方法對高手來說,肯定簡單,但像我這樣的新手,其實當初是很希望都能得到這樣一份資料啊。所以將其總結如上,並且感謝唐同學對我的幫助哈。

過兩天我還會再寫些我寫程序時總結的小技巧。

 

 

發佈了19 篇原創文章 · 獲贊 14 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章