【CSplitterWnd技巧二】使分割窗口不能改变大小
作者:周焱
有时候,需要保持分割窗口中每个小窗口的尺寸或者比例,不允许用户拖动分割条。我考虑了有两种方法可以实现。效果如下,鼠标在分割条上,没有出现可以改变大小的箭头。
方法1:
================================================
//CMySplitter.h
#pragma once
#include "stdafx.h"
class CMySplitter:public CSplitterWnd
{
DECLARE_DYNAMIC(CMySplitter)
public:
DECLARE_MESSAGE_MAP()
protected:
//重载进行非客户区鼠标测试函数
afx_msg UINT OnNcHitTest(CPoint point);
};
================================================
//CMySplitter.cpp
#include "Cmysplitter.h"
IMPLEMENT_DYNAMIC(CMySplitter,CSplitterWnd)
BEGIN_MESSAGE_MAP(CMySplitter,CSplitterWnd)
ON_WM_NCHITTEST()
END_MESSAGE_MAP()
UINT CMySplitter::OnNcHitTest(CPoint point)
{
//返回HTNOWHERE...
return HTNOWHERE;
}
================================================
方法2:
================================================
//CMySplitter.h
#pragma once
#include "stdafx.h"
class CMySplitter : public CSplitterWnd
{
DECLARE_DYNCREATE(CMySplitter)
protected:
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()
};
================================================
//CMySplitter.cpp
#include "CMySplitter.h"
IMPLEMENT_DYNCREATE(CMySplitter, CSplitterWnd)
BEGIN_MESSAGE_MAP(CMySplitter, CSplitterWnd)
ON_WM_LBUTTONDOWN()
ON_WM_SETCURSOR()
ON_WM_MOUSEMOVE()
END_MESSAGE_MAP()
void CMySplitter::OnLButtonDown(UINT nFlags, CPoint point)
{
// 直接返回
return;
}
BOOL CMySplitter::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
// 当光标进入分割窗口时,不允许改变样子
return FALSE;
}
void CMySplitter::OnMouseMove(UINT nFlags, CPoint point)
{
//跳过调用CSplitterWnd::OnMouseMove,因为他会引起光标的改变
CWnd::OnMouseMove(nFlags, point);
}
================================================
虽然上面两种方法,一个繁一个易,但是从不同的角度分析和解决了问题。其实,面对一个想法,可能有多种处理方案。如果我们认真思考,会发现有很多方法能够达到我们的要求。