DoubleLi

qq: 517712484 wx: ldbgliet

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  4737 随笔 :: 2 文章 :: 542 评论 :: 1615万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

对话框上的按钮本身只能添加单击双击时间,不能响应鼠标按下与弹起消息,可以通过两种方法实现:

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);
}

 

 

from:http://blog.csdn.net/pandy1110/article/details/5953188

posted on   DoubleLi  阅读(3735)  评论(0编辑  收藏  举报
编辑推荐:
· 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中服务端控件事件是如何触发的
点击右上角即可分享
微信分享提示