MFC-SpinButton响应鼠标抬起事件

SpinButton鼠标抬起的响应需要将CSpinButtonCtrl控件先子类化,然后再子类中响应WM_LBUTTONUP事件,在该事件的响应函数中发送消息给父窗口,这里博主发送的是自定义消息,然后再父窗口中接受该消息进行处理,即可响应Spin控件的鼠标抬起了。具体代码如下:

1. 子类化CSpinButtonCtrl为CMySpin

#pragma once
#include "afxcmn.h"
class CMySpin :
	public CSpinButtonCtrl
{
public:
	CMySpin();
	~CMySpin();
	DECLARE_MESSAGE_MAP()
	afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
};

2. 在子类中天健LButtonUp事件的响应函数,给父窗口发送消息 

#include "stdafx.h"
#include "MySpin.h"


CMySpin::CMySpin()
{
}


CMySpin::~CMySpin()
{
}
BEGIN_MESSAGE_MAP(CMySpin, CSpinButtonCtrl)
	ON_WM_LBUTTONUP()
END_MESSAGE_MAP()


void CMySpin::OnLButtonUp(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	CWnd * pDlg = (CWnd*)this->GetParent();
	pDlg->SendMessage(WM_USER+8, 0, 0);
	CSpinButtonCtrl::OnLButtonUp(nFlags, point);
}

这里我们发送的是自定义消息WM_USER+8

3. 在父窗口中接受消息并处理

头文件中 

afx_msg LRESULT OnSpinRelease(WPARAM, LPARAM);

CPP文件中

BEGIN_MESSAGE_MAP(CRobotControlDlg, CDialogEx)
	...
	ON_MESSAGE(WM_USER+8, OnSpinRelease)
END_MESSAGE_MAP()

...

LRESULT CRobotControlDlg::OnSpinRelease(WPARAM w, LPARAM l)
{
	// TODO: Add your control notification handler code here
	return 0;
}

然后就可以响应该事件了

 

 

发表评论