一、自定义按钮RFMSStepButton :
//////////// //RFMSStepButton.h
#pragma once
class RFMSStepButton : public CButton
{
DECLARE_DYNAMIC(RFMSStepButton)
public :
RFMSStepButton();
virtual ~ RFMSStepButton();
protected :
DECLARE_MESSAGE_MAP()
public :
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
public :
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
public :
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
public :
bool m_bStartMove;
public :
afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);
public :
CPoint m_oldpoint;
};
/////////////// //RFMSStepButton.cpp
#include " stdafx.h "
#include " ButtonTest.h "
#include " RFMSStepButton.h "
#include " MyDialog.h "
// RFMSStepButton
IMPLEMENT_DYNAMIC(RFMSStepButton, CButton)
RFMSStepButton::RFMSStepButton()
: m_bStartMove( false )
, m_oldpoint( 0 )
{
}
RFMSStepButton:: ~ RFMSStepButton()
{
}
BEGIN_MESSAGE_MAP(RFMSStepButton, CButton)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDBLCLK()
END_MESSAGE_MAP()
// RFMSStepButton 消息处理程序
void RFMSStepButton::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
m_bStartMove = TRUE;
m_oldpoint = point;
ClientToScreen( & m_oldpoint);
CButton::OnLButtonDown(nFlags, point);
}
void RFMSStepButton::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
m_bStartMove = FALSE;
CButton::OnLButtonUp(nFlags, point);;
}
void RFMSStepButton::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
if (m_bStartMove)
{
/*
此方法优点,代码简单,但有个问题没解决,就是鼠标在其他地方按下左键,拖动到按钮上去后,按钮就又会移动了。
ReleaseCapture();
SendMessage(WM_NCLBUTTONDOWN, (WPARAM)HTCAPTION, 0);
*/
CRect rect;
GetClientRect( & rect);
ClientToScreen( & rect);
GetParent() -> ScreenToClient( & rect);
ClientToScreen( & point);
SetWindowPos(NULL,rect.left + point.x - m_oldpoint.x ,
rect.top + point.y - m_oldpoint.y , 0 , 0 , SWP_NOZORDER | SWP_NOSIZE);
m_oldpoint = point;
Invalidate();
}
CButton::OnMouseMove(nFlags, point);
}
void RFMSStepButton::OnLButtonDblClk(UINT nFlags, CPoint point)
{
// 重载按钮双击事件。
MyDialog dialog;
dialog.DoModal();
CButton::OnLButtonDblClk(nFlags, point);
}
/*
备注:响应函数要用WM_CLICK之类的,而不能用=BN_CLICKED=之类的。
*/
二、调用处及群响应:
// ButtonTestDlg.cpp : 实现文件
#define ID1 120
#define ID2 140
BEGIN_MESSAGE_MAP(CButtonTestDlg, CDialog)
ON_COMMAND_RANGE(ID1,ID2, OnMyButton) // 从ID1到ID2的所有控件都响应OnMyButton函数
END_MESSAGE_MAP()
BOOL CButtonTestDlg::OnInitDialog()
{
CDialog::OnInitDialog();
RFMSStepButton * button11 = new RFMSStepButton;
CRect rect1( 200 , 0 , 300 , 100 );
button11 -> Create(L " 124 " , WS_CHILD | WS_VISIBLE | WS_TABSTOP , rect1, this , 124 );
RFMSStepButton * button12 = new RFMSStepButton;
CRect rect3( 200 , 200 , 300 , 300 );
button12 -> Create(L " 125 " , WS_CHILD | WS_VISIBLE | WS_TABSTOP , rect3, this , 125 );
return TRUE;
}
void CButtonTestDlg::OnMyButton(UINT nID)
{
// MessageBox(L"this");
}