窗口分割的方法在网上很容易找到,分割后出现的灰色默认分割条却很不美观,如图1所未.经过自己的查询和尝试,找到了改变它颜色的方法,效果如图2.
图1
图2
实现方法:
一.添加普通类.
我以CRichMineSplitter为例,继承CSplitterWnd,添加消息映射.(红色为自己添加和修改的地方)
头文件
class CRichMineSplitter : public CSplitterWnd
{
public:
CRichMineSplitter();
virtual ~CRichMineSplitter();
DECLARE_DYNCREATE(CRichMineSplitter)
protected:
//添加消息响应函数
DECLARE_MESSAGE_MAP()
};
源文件顶部
IMPLEMENT_DYNCREATE(CRichMineSplitter, CSplitterWnd)
BEGIN_MESSAGE_MAP(CRichMineSplitter, CSplitterWnd)
//添加响应消息
END_MESSAGE_MAP()
二.实现分割条不可移动,如果不需要,可跳过.
1.添加
ON_WM_LBUTTONDOWN()
ON_WM_SETCURSOR()
ON_WM_MOUSEMOVE()
三个消息的响应,代码修改如下
头文件:
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()
源文件
BEGIN_MESSAGE_MAP(CRichMineSplitter, CSplitterWnd)
ON_WM_LBUTTONDOWN()
ON_WM_SETCURSOR()
ON_WM_MOUSEMOVE()
END_MESSAGE_MAP()
2.实现消息响应,代码如下
void CRichMineSplitter::OnLButtonDown(UINT nFlags, CPoint point)
{
// 直接返回,不处理
return;
}
BOOL CRichMineSplitter::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
// 当光标进入分割窗口时,不允许改变样子,不处理
return FALSE;
}
void CRichMineSplitter::OnMouseMove(UINT nFlags, CPoint point)
{
//将CSplitter类的处理改为由CWnd处理
//CSplitterWnd::OnMouseMove(nFlags, point);
CWnd::OnMouseMove(nFlags, point);
}
这样分割条就固定了.
三.改变分割条的颜色
1.构造函数中,将分割条周围的空隙设为0,不然不好控制,并定义合适的分割条大小.
CRichMineSplitter::CRichMineSplitter()
{
m_cxSplitter=m_cySplitter=6; // size of splitter bar
m_cxBorderShare=m_cyBorderShare=0; // space on either side of splitter
m_cxSplitterGap=m_cySplitterGap=6; // amount of space between panes//间隙
m_cxBorder=m_cyBorder=0; // borders in client area 外框
}
2.添加ON_WM_ERASEBKGND()消息响应,取消重刷新背景
重载OnDrawSplitter(...)函数,刷分割条
头文件
protected:
............
afx_msg void OnDrawSplitter( CDC* pDC, ESplitType nType, const CRect& rect );
afx_msg BOOL OnEraseBkgnd( CDC* pDC );
DECLARE_MESSAGE_MAP()
源文件
BEGIN_MESSAGE_MAP(CRichMineSplitter, CSplitterWnd)
..........
ON_WM_ERASEBKGND()
END_MESSAGE_MAP()
函数代码:
BOOL CRichMineSplitter::OnEraseBkgnd( CDC* pDC )
{
//不刷新背景
return TRUE;
}
void CRichMineSplitter::OnDrawSplitter( CDC* pDC, ESplitType nType, const CRect& rect )
{
//蓝色填充
if(nType== splitBar && pDC!=NULL)//判断画分割条时
{
CRect rcD=rect,rcL=rect;
//根据横纵向矩阵大小关系,将分割分为两部分刷两种颜色,
//rcD 深色.rcL 浅色
if(rect.right-rect.left < 10)//纵向
{
rcL.right=(rcL.left+rcL.right)/2;//
rcD.left=rcL.right;
}
else//横向
{
rcL.bottom=(rcL.top+rcL.bottom)/2;
rcD.top=rcL.bottom;
}
CBrush brushD(RGB(0,0,230));//深色画刷
CBrush brushL(RGB(60,150,255));/浅色画刷
//用指定画刷填充对应矩形区域
pDC->FillRect(&rcD,&brushD);
pDC->FillRect(&rcL,&brushL);
}
}
五.获得分割条大小
网上给了的方法可谓是千奇百怪,实际根本没有那么麻烦. void CRichMineSplitter::OnDrawSplitter( CDC* pDC, ESplitType nType, const CRect& rect )的最后一个参数rect就是当前正在绘制的分割条的大小.可以通过对它参数大小的判断,判断出是哪一个分割条.
六.大功造成.
转帖:http://hi.baidu.com/jlusuoya/blog/item/19950cdc47309e53cdbf1a39.html
===========================================================================
CSplitterWnd技巧一】改变分割窗口拖动条的大小
作者:周焱
大家都知道默认的CSplitterWnd的拖动条很宽,看起来占地方,也不美观。怎么改变它呢?
研究CSplitterWnd的构造函数发现,拖动条的宽度是可以设置的。下面是我写的一个简单的CMySplitter类。他的效果如下:
下面是CMySplitter的代码:
=========================================================
//MySplitter.h
#pragma once
#include "stdafx.h"
class CMySplitter : public CSplitterWnd
{
public:
CMySplitter();
DECLARE_DYNCREATE(CMySplitter)
public:
~CMySplitter();
protected:
DECLARE_MESSAGE_MAP()
};
=========================================================
//MySplitter.cpp
#include "MySplitter.h"
IMPLEMENT_DYNCREATE(CMySplitter, CSplitterWnd)
//在构造函数中改变分割条的宽度和属性!
CMySplitter::CMySplitter()
{
m_cxSplitter = 4; //must >=4 ,拖动时拖动条的宽度
m_cySplitter = 4;
m_cxBorderShare = 0; //按下鼠标时拖动条的偏移量
m_cyBorderShare = 0;
m_cxSplitterGap= 1; //splitter拖动条的宽度
m_cySplitterGap= 1;
}
CMySplitter::~CMySplitter()
{
}
BEGIN_MESSAGE_MAP(CMySplitter, CSplitterWnd)
END_MESSAGE_MAP()
=========================================================
使用时,在主窗口中包含头文件MySplitter.h,将CSplitterWnd改成CMySplitter就可以了。
转帖: http://www.moon-soft.com/doc/19172.htm