【CSplitterWnd技巧三】能拖动的带背景颜色的分割条
作者:enloo
想实现这个功能,曾经困扰了我一段时间,后来发现原来是犯了一些小错误。
功能:
1,带背景颜色
2,拖动拖动条,能拖动窗口
效果如下:
程序:
==================================================
//MySplitter.h
#pragma once
#include "stdafx.h"
class CMySplitter : public CSplitterWnd
{
public:
CMySplitter();
DECLARE_DYNCREATE(CMySplitter)
public:
~CMySplitter();
protected:
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnMouseMove(UINT,CPoint);
BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
HBRUSH m_hbr; //背景刷子
CString m_clsName; //窗口类名
DECLARE_MESSAGE_MAP()
};
==================================================
//MySplitter.cpp
#include "MySplitter.h"
IMPLEMENT_DYNCREATE(CMySplitter, CSplitterWnd)
CMySplitter::CMySplitter()
{
m_cxSplitter = 4; //must >=4 ,拖动splitter时拖动条的宽度
m_cySplitter = 4;
m_cxBorderShare = -4; //按下鼠标时splitter拖动条的偏移量
m_cyBorderShare = -4;
m_cxSplitterGap= 16;
m_cySplitterGap= 16; //splitter拖动条的宽度
m_hbr = ::CreateSolidBrush(RGB(98,98,98));
m_clsName = "";
}
CMySplitter::~CMySplitter()
{
}
BEGIN_MESSAGE_MAP(CMySplitter, CSplitterWnd)
ON_WM_LBUTTONDOWN()
ON_WM_MOUSEMOVE()
ON_WM_RBUTTONDOWN()
END_MESSAGE_MAP()
void CMySplitter::OnLButtonDown(UINT nFlags, CPoint point)
{
//拖动splitter拖动条,能够拖动窗口
::PostMessage(AfxGetMainWnd()->GetSafeHwnd(),WM_NCLBUTTONDOWN,HTCAPTION,MAKELPARAM(point.x,point.y));
CWnd::OnLButtonDown(nFlags,point);
}
void CMySplitter::OnMouseMove(UINT nFlags, CPoint point)
{
//跳过调用CSplitterWnd::OnMouseMove,因为他会引起光标的改变
CWnd::OnMouseMove(nFlags, point);
}
BOOL CMySplitter::PreCreateWindow(CREATESTRUCT& cs)
{
//窗口类名必须在类的域类有效,否则::CreateWindowEx得到的是无效的类名。
//将会导致创建文档失败的错误。
m_clsName = AfxRegisterWndClass(0, ::LoadCursor (NULL,
IDC_ARROW), ::CreateSolidBrush(RGB(255,95,17)));
cs.lpszClass = (const char* )m_clsName;
return CSplitterWnd::PreCreateWindow(cs);
}
==================================================
说明:
1,要实现用拖动条拖动窗口,不能简单的用技巧2介绍的OnNcHitTest返回HTCAPTION来解决。因为这样只能拖动分割窗口,不能拖动主窗口。这里用在左键按下的时候 ::PostMessage(AfxGetMainWnd()->GetSafeHwnd(),WM_NCLBUTTONDOWN,HTCAPTION,MAKELPARAM(point.x,point.y)),通过给主窗口发送带HTCAPTION的WM_NCLBUTTONDOWN消息来实现。
2,默认的分割窗口的类名是AfxMDIFrame42d,它和MDI程序中的主窗口差不多,没有背景刷。在这里注册了一个新的窗口类,并指定了背景刷。要注意的是,窗口类名不能在PreCreateWindow中申明,应该放在类的申明中,作为类的成员函数。