利用PreTranslateMessage,响应按钮控件的按下(WM_LBUTTONDOWN)和松开(WM_LBUTTONUP)

    VC和button控制只有两个事件,一个是单击事件,一个事双击事件。在这个方面VB就方便多了。但是我们有其他办法解决。首先我们先学一些基础知识。

 

1...关于PreTranslateMessage

PreTranslateMessage是消息在送给TranslateMessage函数之前被调用的,绝大多数本窗口的消息都要通过这里,比较常用,当你需要在MFC之前处理某些消息时,常常要在这里添加代码.

2...关于MSG结构体
typedef struct tagMSG {     // msg
    HWND   hwnd;    
    UINT   message;
    WPARAM wParam;
    LPARAM lParam;
    DWORD  time;
    POINT  pt;
} MSG;

Members
hwnd
Handle to the window whose window procedure receives the message.
message
Specifies the message identifier. Applications can only use the low word; the high word is reserved by the system.
wParam
Specifies additional information about the message. The exact meaning depends on the value of the message member.
lParam
Specifies additional information about the message. The exact meaning depends on the value of the message member.
time
Specifies the time at which the message was posted.
pt
Specifies the cursor position, in screen coordinates, when the message was posted.

3...ID--HANDLE--HWND三者之间的互相转换
id->句柄、、、、、hWnd = ::GetDlgItem(hParentWnd,id);
id->指针、、、、、CWnd::GetDlgItem();
句柄->id、、、、、id = GetWindowLong(hWnd,GWL_ID);
句柄->指针、、、、CWnd *pWnd=CWnd::FromHandle(hWnd);
指针->ID、、、、、id = GetWindowLong(pWnd->GetSafeHwnd,GWL_ID);
指针->句柄、、、、hWnd=cWnd.GetSafeHandle() or mywnd->m_hWnd;

 

例程:

方法1:

BOOL AcameraCT::PreTranslateMessage(MSG* pMsg)
{
  int buID;
 buID= GetWindowLong(pMsg->hwnd,GWL_ID);//由窗口句柄获得ID号,GetWindowLong为获得窗口的ID号。
  if(pMsg->message==WM_LBUTTONDOWN) 
 {     
  if(buID==IDC_BUTTON_CT1) //按下
  {  
   //在这里添加单击按下事件的程序
  } 
 }
 if(pMsg->message==WM_LBUTTONUP) 
 { 
  if(buID==IDC_BUTTON_CT1)
  {  
   //在这里添加单击松开事件的程序
  } 
 }
 return CDialog::PreTranslateMessage(pMsg);
}

 

方法2:

BOOL AcameraCT::PreTranslateMessage(MSG* pMsg)
{
  int buID;
 CWnd* pWnd=WindowFromPoint(pMsg->pt); //获得指定点句柄
 buID=pWnd->GetDlgCtrlID();//获得该句柄的ID号。
  if(pMsg->message==WM_LBUTTONDOWN) 
 {     
  if(buID==IDC_BUTTON_CT1) //按下
  {  
   //在这里添加单击按下事件的程序
  } 
 }
 if(pMsg->message==WM_LBUTTONUP) 
 { 
  if(buID==IDC_BUTTON_CT1)
  {  
   //在这里添加单击松开事件的程序
  } 
 }
 return CDialog::PreTranslateMessage(pMsg);
}

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值