來(lái)北航做畢業(yè)設(shè)計(jì)已經(jīng)有半個(gè)月了,由于實(shí)驗(yàn)室中,項(xiàng)目組用的是VC,昨天剛好遇到一個(gè)細(xì)節(jié)問(wèn)題:大家都知道,VC中可以動(dòng)態(tài)或者靜態(tài)的分割窗口(關(guān)于這點(diǎn)許多地方說(shuō)的已經(jīng)很清楚了,這里不做討論)但是實(shí)際上,很多時(shí)候我們想要的只是靜態(tài)的分割出窗口,并不想讓別人移動(dòng)改變這個(gè)比例,或者是需要限定某個(gè)分割出的窗口的范圍(比如是小到多少之后就不能再變小了),關(guān)于這個(gè)問(wèn)題許多書(shū)上都沒(méi)有解釋?zhuān)ǎ校樱耗鞘怯X(jué)得太簡(jiǎn)單了,直接忽略么?orz...)本人研究了一下, 關(guān)于鎖定分割大致有兩種方法,而如何限制移動(dòng)范圍也可已在此基礎(chǔ)上加,特此總結(jié)如下:
鎖定的話,其中一種方法是直接截獲一個(gè)消息,我們知道,其實(shí) CsplitterWnd 是從CWnd 派生出來(lái)的,所以其實(shí)很容易截獲Window的消息,在這里我們應(yīng)該關(guān)注的其實(shí)僅僅只有一個(gè)消息WM_NCHITTEST ,其作用大致就是當(dāng)你的鼠標(biāo)在這個(gè)劃分出的區(qū)域中移動(dòng)時(shí)為CWnd捕捉你的鼠標(biāo)輸入的,所以,換言之,如果我們截獲了這個(gè)消息,直接返回為HTNOWHERE(參考MSDN),那么當(dāng)你的鼠標(biāo)停留在Splitter上時(shí)就不會(huì)出現(xiàn)任何反應(yīng),從而鎖定的目的就達(dá)到了
另一種是重載:OnLButtonDown(UINT nFlags, CPoint point),OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 以及OnMouseMove(UINT nFlags, CPoint point)幾個(gè)函數(shù),前兩個(gè)函數(shù)的重載也可以達(dá)到鎖定的效果,第三個(gè)函數(shù)也就是OnMouseMove()的重載則是為了限定移動(dòng)的范圍(這種情況可能需要的更多一些)下面貼上這兩種方法的代碼:
兩種方法有一個(gè)共同點(diǎn)就是需要自己從CSplitterWnd類(lèi)中派生一個(gè)自己的類(lèi)就叫 MySplitterWnd 好了,先采用第一種方法,來(lái)鎖定,MySplitterWnd.h中代碼如下:
#if !defined(AFX_MYSPLITTERWND_H__A2E77A4F_BB34_4622_8178_ED5FB394208E__INCLUDED_)
#define AFX_MYSPLITTERWND_H__A2E77A4F_BB34_4622_8178_ED5FB394208E__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// MySplitterWnd.h : header file
//


/**//////////////////////////////////////////////////////////////////////////////
// MySplitterWnd frame with splitter

#ifndef __AFXEXT_H__
#include <afxext.h>
#endif

class MySplitterWnd : public CSplitterWnd


{
DECLARE_DYNCREATE(MySplitterWnd)
protected:
// protected constructor used by dynamic creation

// Attributes
protected:
CSplitterWnd m_wndSplitter;
public:

MySplitterWnd();

public:

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(MySplitterWnd)
protected:
virtual BOOL OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext);
afx_msg UINT OnNcHitTest(CPoint point); //新加的

//}}AFX_VIRTUAL

// Implementation
public:
virtual ~MySplitterWnd();

// Generated message map functions
//{{AFX_MSG(MySplitterWnd)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};


/**//////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_MYSPLITTERWND_H__A2E77A4F_BB34_4622_8178_ED5FB394208E__INCLUDED_)

其中關(guān)鍵的語(yǔ)句就是 加一句 afx_msg UINT OnNcHitTest(CPoint point)的定義,然后再在MySplitterWnd.cpp具體實(shí)現(xiàn)這個(gè)函數(shù),如下
// MySplitterWnd.cpp : implementation file
//

#include "stdafx.h"
#include "GL.h"
#include "MySplitterWnd.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


/**//////////////////////////////////////////////////////////////////////////////
// MySplitterWnd

IMPLEMENT_DYNCREATE(MySplitterWnd, CSplitterWnd)

MySplitterWnd::MySplitterWnd()


{
}

MySplitterWnd::~MySplitterWnd()


{
}


BOOL MySplitterWnd::OnCreateClient(LPCREATESTRUCT /**//*lpcs*/, CCreateContext* pContext)


{
return m_wndSplitter.Create(this,
2, 2, // TODO: adjust the number of rows, columns
CSize(10, 10), // TODO: adjust the minimum pane size
pContext);
}


afx_msg UINT MySplitterWnd::OnNcHitTest(CPoint /**//*point*/)


{
return HTNOWHERE;
}



/**//*BEGIN_MESSAGE_MAP(MySplitterWnd, CSplitterWnd)
//{{AFX_MSG_MAP(MySplitterWnd)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()*/


BEGIN_MESSAGE_MAP(MySplitterWnd, CSplitterWnd)
ON_WM_NCHITTEST()
END_MESSAGE_MAP()



/**//////////////////////////////////////////////////////////////////////////////
// MySplitterWnd message handlers

可以看到,具體實(shí)現(xiàn)就一句話,return HTNOWHERE。現(xiàn)在就OK了,我的表述已經(jīng)盡可能清楚了,如果實(shí)在不理解也沒(méi)關(guān)系,你們可以直接將上面的.h和.cpp直接復(fù)制過(guò)去,直接加入到你的工程中,到時(shí)候用的時(shí)候直接 定義用 MySplitterWnd這個(gè)類(lèi)定義 自己的變量,如 MySplitterWnd XXX(自己隨便取名)
下面介紹另一種方法,也是同上面一樣需要自己建立一個(gè)類(lèi),還是叫MySplitterWnd好了,基類(lèi)還是CSplitterWnd
然后 在MySplitterWnd.h中添加如下代碼(位置 和添加OnNcHitTest(...)函數(shù)一樣)
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
在MySplitterWnd.cpp中添加三個(gè)函數(shù)的具體實(shí)現(xiàn)代碼如下:
void MySplitterWnd::OnLButtonDown(UINT nFlags, CPoint point)
{
// 直接返回,不處理
return;
}
BOOL MySplitterWnd::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
// 當(dāng)光標(biāo)進(jìn)入分割窗口時(shí),不允許改變樣子,不處理
return FALSE;
}
void MySplitterWnd::OnMouseMove(UINT nFlags, CPoint point)
{
//將CSplitter類(lèi)的處理改為由CWnd處理
//CSplitterWnd::OnMouseMove(nFlags, point);
if(point.x<250||point.x>500) CWnd::OnMouseMove(nFlags, point);
else CSplitterWnd::OnMouseMove(nFlags, point);
}
好了,其實(shí)到了這里還沒(méi)有完,剛說(shuō)到鎖定的問(wèn)題,大家可以發(fā)現(xiàn)如果三個(gè)函數(shù)這樣子寫(xiě)你的分隔條直接不能動(dòng)了,和剛才的截取消息的鎖定效果一摸一樣,但是關(guān)于第三個(gè)函數(shù)OnMouseMove()的實(shí)際作用其實(shí)是限定范圍,if(point.x<250||point.x>500) CWnd::OnMouseMove(nFlags, point);
else CSplitterWnd::OnMouseMove(nFlags, point); 這兩句的意思是說(shuō)將移動(dòng)范圍鎖定在250和500之間(這個(gè)數(shù)值大家可以隨便設(shè)置),為了要使得這個(gè)函數(shù)生效,大家可以把前面兩個(gè)也就是OnLButtonDown()和OnSetCursor()屏蔽掉,只留下第三個(gè)OnMouseMove()就可以達(dá)到限制范圍的效果了,當(dāng)然只屏蔽掉OnLButtonDown()函數(shù)也是可以的,因?yàn)镺nSetCursor()函數(shù)只是改變光標(biāo)樣子用的,(鎖定的另一個(gè)方法其實(shí)就是只用前面兩個(gè),既屏蔽掉OnMouseMove(),這個(gè)函數(shù),大家可以自己試試,分別屏蔽看會(huì)出現(xiàn)什么效果)
MySplitterWnd.h和MySplitterWnd.cpp分別如下:
MySplitterWnd.h:
#if !defined(AFX_MYSPLITTERWND_H__7AA280DC_82F5_4FF5_95A6_036C8A20B50D__INCLUDED_)
#define AFX_MYSPLITTERWND_H__7AA280DC_82F5_4FF5_95A6_036C8A20B50D__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// MySplitterWnd.h : header file
//


/**//////////////////////////////////////////////////////////////////////////////
// MySplitterWnd frame with splitter

#ifndef __AFXEXT_H__
#include <afxext.h>
#endif

class MySplitterWnd : public CSplitterWnd


{

public:

MySplitterWnd();
// Operations
public:

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(MySplitterWnd)


//}}AFX_VIRTUAL

// Implementation
public:
virtual ~MySplitterWnd();

DECLARE_DYNCREATE(MySplitterWnd)

afx_msg void OnLButtonDown(UINT nFlags, CPoint point); //新加的
afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message); //新加的
afx_msg void OnMouseMove(UINT nFlags, CPoint point); //新加的
DECLARE_MESSAGE_MAP() //后加的


};


/**//////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_MYSPLITTERWND_H__7AA280DC_82F5_4FF5_95A6_036C8A20B50D__INCLUDED_)

MySplitterWnd.cpp:
// MySplitterWnd.cpp : implementation file
//

#include "stdafx.h"
#include "GL.h"
#include "MySplitterWnd.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


/**//////////////////////////////////////////////////////////////////////////////
// MySplitterWnd


IMPLEMENT_DYNCREATE(MySplitterWnd, CSplitterWnd)


BEGIN_MESSAGE_MAP(MySplitterWnd, CSplitterWnd)
//ON_WM_LBUTTONDOWN()
//ON_WM_SETCURSOR()
ON_WM_MOUSEMOVE()

END_MESSAGE_MAP()




MySplitterWnd::MySplitterWnd()


{
}

MySplitterWnd::~MySplitterWnd()


{
}


/**//*void MySplitterWnd::OnLButtonDown(UINT nFlags, CPoint point)
{
// 直接返回,不處理
return;
}

BOOL MySplitterWnd::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
// 當(dāng)光標(biāo)進(jìn)入分割窗口時(shí),不允許改變樣子,不處理
return FALSE;
}*/

void MySplitterWnd::OnMouseMove(UINT nFlags, CPoint point)


{
//將CSplitter類(lèi)的處理改為由CWnd處理
//CSplitterWnd::OnMouseMove(nFlags, point);

if(point.x<250||point.x>500) CWnd::OnMouseMove(nFlags, point);
else CSplitterWnd::OnMouseMove(nFlags, point);

}





/**//*
BEGIN_MESSAGE_MAP(MySplitterWnd, CSplitterWnd)
//{{AFX_MSG_MAP(MySplitterWnd)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()*/


/**//////////////////////////////////////////////////////////////////////////////
// MySplitterWnd message handlers

說(shuō)的可能有些不清楚,不清楚的也沒(méi)關(guān)系,可以直接將上面的MySplitterWnd.h和MySplitterWnd.cpp導(dǎo)入到自己的工程中,直接用就行了,將你要鎖定或者限制移動(dòng)范圍的分割區(qū)域用MySplitterWnd定義,不做限制,鎖定的區(qū)域用CSplitterWnd類(lèi)定義,這樣子就可以做到有選擇的限制鎖定某幾個(gè)分割區(qū)域的效果.