VC+MFC里按鈕只有單擊和雙擊消息,要實(shí)現做簡(jiǎn)單幾何右鍵單擊,得在PreTransLateMessage里面實(shí)現;
對話(huà)框的PreTranslateMessage里 實(shí)現 按鈕的左鍵單擊和右鍵單擊事件;
也在里面實(shí)現回車(chē)鍵響應和Ctrl+enter組合鍵響應。
- //在這里處理按鈕的左鍵單擊和右鍵單擊,編輯框回車(chē)和CTRL+ENTR消息
- BOOL CTestDlg::PreTranslateMessage(MSG* pMsg)
- {
- // TODO: Add your specialized code here and/or call the base class
-
- int My_ID=GetWindowLong(pMsg->hwnd, GWL_ID); //由消息句柄獲得發(fā)送消息的控件ID號
- if(My_ID==IDC_BUTTON1) //點(diǎn)擊按鈕
- {
- if(pMsg->message==WM_LBUTTONDOWN) //如果鼠標左鍵按下
- {
- MessageBox("左鍵點(diǎn)擊按鈕");
- }
-
- if(pMsg->message==WM_RBUTTONDOWN) //如果鼠標右鍵按下
- {
- MessageBox("右鍵點(diǎn)擊按鈕");
- }
- }
-
- //在此添加對話(huà)框里面的編輯框的Enter和Ctrl+enter響應消息
- if(My_ID==IDC_EDIT1)// 編輯框
- {
- if(pMsg->message==WM_KEYDOWN && pMsg-> wParam==VK_RETURN)
- {
- if( GetKeyState(VK_CONTROL)&0x80 )
- {
- PostMessage(WM_COMMAND,IDC_EDIT1,0);
-
- UpdateData(true);
- CString str;
- str.Format("%s\r\n",m_edit);
- m_edit.Format("%s",str);
- UpdateData(false);
-
- CDC *dc=GetDC();
- CSize sz=dc->GetTextExtent(m_edit);
- //下面設置光標跟在字符串的最后面
- ((CEdit*)GetDlgItem(IDC_EDIT1))->SetSel(DWORD(sz.cx),DWORD(sz.cx),TRUE);
-
- return true;
-
- }
- else
- {
- PostMessage(WM_COMMAND,IDC_EDIT1,0);
- MessageBox(" Enter");
- return true;
- }
-
-
- }
- }
-
-
- return CDialog::PreTranslateMessage(pMsg);
- }