对话框上的按钮本身只能添加单击双击时间,不能响应鼠标按下与弹起消息,可以通过两种方法实现:
1.重载CButton类,将该类子类化
在工程中添加一个新类CMyButton,基类为CButton。
在对话框MyDlg中为IDC_BUTTON添加变量,在变量类型里选择CMyButton,变量名自定义,如m_myButton。添加函数OnDown与OnUp函数响应按钮按下与弹起消息
在Class name中选择CMyButton,然后添加WM_LBUTTONUP,WM_LBUTTONDOWN消息映射函数。添加代码如下:
void CMyButton::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
((CMyDlg*)GetParent())->OnUp(this->GetDlgCtrlID());
CButton::OnLButtonUp(nFlags, point);
}
void CMyButton::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
((CMyDlg*)GetParent())->OnDown(this->GetDlgCtrlID());
CButton::OnLButtonDown(nFlags, point);
}
然后MyDlg中实现函数
void CMyDlg::OnDown( UINT nID )
{
Switch(nID)
case IDC_BUTTON:
break;
}
void CMyDlg::OnUp( UINT nID )
{
...
}
2.重载Dialog的PreTranslateMessage函数
BOOL CTestDlgDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if(pMsg-> message == WM_LBUTTONDOWN)
{
if(WindowFromPoint(pMsg-> pt) == GetDlgItem(IDC_BUTTON1))
{
}
}
else if(pMsg-> message == WM_LBUTTONUP)
{
if(WindowFromPoint(pMsg-> pt) == GetDlgItem(IDC_BUTTON1))
{
//AfxMessageBox( "Hello ");
}
}
return CDialog::PreTranslateMessage(pMsg);
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2012-01-16 Asp.net中服务端控件事件是如何触发的