VC和button控制只有两个事件,一个是单击事件,一个事双击事件。在这个方面VB就方便多了。但是我们有其他办法解决。首先我们先学一些基础知识。
1...关于PreTranslateMessage
PreTranslateMessage是消息在送给TranslateMessage函数之前被调用的,绝大多数本窗口的消息都要通过这里,比较常用,当你需要在MFC之前处理某些消息时,常常要在这里添加代码.@H_502_18@
2...关于MSG结构体@H_502_18@typedef struct tagMSG { // msg @H_502_18@ HWND hwnd; @H_502_18@ UINT message; @H_502_18@ WPARAM wParam; @H_502_18@ LPARAM lParam; @H_502_18@ DWORD time; @H_502_18@ POINT pt; @H_502_18@} MSG;
Members@H_502_18@hwnd @H_502_18@Handle to the window whose window procedure receives the message. @H_502_18@message @H_502_18@Specifies the message identifier. Applications can only use the low word; the high word is reserved by the system. @H_502_18@wParam @H_502_18@Specifies additional information about the message. The exact meaning depends on the value of the message member. @H_502_18@lParam @H_502_18@Specifies additional information about the message. The exact meaning depends on the value of the message member. @H_502_18@time @H_502_18@Specifies the time at which the message was posted. @H_502_18@pt @H_502_18@Specifies the cursor position,in screen coordinates,when the message was posted.
3...ID--HANDLE--HWND三者之间的互相转换@H_502_18@id->句柄、、、、、hWnd = ::GetDlgItem(hParentWnd,id);@H_502_18@id->指针、、、、、CWnd::GetDlgItem();@H_502_18@句柄->id、、、、、id = GetWindowLong(hWnd,GWL_ID);@H_502_18@句柄->指针、、、、CWnd *pWnd=CWnd::FromHandle(hWnd);@H_502_18@指针->ID、、、、、id = GetWindowLong(pWnd->GetSafeHwnd,GWL_ID);@H_502_18@指针->句柄、、、、hWnd=cWnd.GetSafeHandle() or mywnd->m_hWnd;
例程:
方法1:
BOOL AcameraCT::PreTranslateMessage(MSG* pMsg) @H_502_18@{@H_502_18@int buID;@H_502_18@buID= GetWindowLong(pMsg->hwnd,GWL_ID);//由窗口句柄获得ID号,GetWindowLong为获得窗口的ID号。@H_502_18@if(pMsg->message==WM_LBUTTONDOWN) @H_502_18@{ @H_502_18@if(buID==IDC_BUTTON_CT1) //按下 @H_502_18@{@H_502_18@//在这里添加单击按下事件的程序@H_502_18@} @H_502_18@}@H_502_18@if(pMsg->message==WM_LBUTTONUP) @H_502_18@{ @H_502_18@if(buID==IDC_BUTTON_CT1)@H_502_18@{@H_502_18@//在这里添加单击松开事件的程序@H_502_18@} @H_502_18@}@H_502_18@return CDialog::PreTranslateMessage(pMsg);@H_502_18@}
方法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);}