现在的位置: 首页 > 自动控制 > 工业·编程 > 正文

利用PreTranslateMessage响应按钮控件的按下与松开

2012-08-12 06:54 工业·编程 ⁄ 共 1850字 ⁄ 字号 暂无评论

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

给我留言

留言无头像?