现在的位置: 首页 > 综合 > 正文

VC++界面处理中怎样设置默认按钮

2018年07月17日 ⁄ 综合 ⁄ 共 2190字 ⁄ 字号 评论关闭
重载PreTranslateMessage函数,截获回车键的消息,当截获到的时候,触发你的函数。 
  BOOL   CxxxDlg::PreTranslateMessage(MSG*   pMsg)   
  { 
    if(pMsg->message==WM_KEYDOWN   &&   pMsg->wParam==VK_ENTER)   
    {   
      YourButtonFunc()   //你的按钮函数
                       //这样,在你的程序中,按回车,就会执行该函数 
      }   
    return   CDialog::PreTranslateMessage(pMsg);  
  }
一下是例子:
BOOL CBrowsBar::PreTranslateMessage(MSG* pMsg)
{
    // TODO: 在此添加专用代码和/或调用基类
    if ((pMsg->message == WM_KEYDOWN) && (pMsg->wParam == VK_ESCAPE))
    {
        return FALSE;
    }
  
    if ((pMsg->message == WM_LBUTTONDOWN) || (pMsg->message == WM_LBUTTONUP) || (pMsg->message == WM_MOUSEMOVE))
    {
        m_pToolTipCtrl.RelayEvent(pMsg);
    }
    if ((pMsg->message == WM_KEYDOWN) && (pMsg->wParam == VK_RETURN))
    {
        int nControlID = GetFocus()->GetDlgCtrlID();
        if (IDC_EDIT_SEARCH == nControlID)
        {
            OnBnClickedButtonSearch();
          
        }
    }
 if ((pMsg->message == WM_KEYDOWN) && (pMsg->wParam == VK_DOWN))
 {
  int nControlID = GetFocus()->GetDlgCtrlID();
  if (IDC_EDIT_COMBO == nControlID)
  {
   m_comboURL.SetFocus();
   m_comboURL.SetCurSel(0);
  
  }
 }
 if (pMsg->message == WM_MOUSEWHEEL)
 {
  CRect rect;
  ::GetWindowRect(GetDlgItem(IDD_VSEEBROWSBAR_COMBO)->GetSafeHwnd(),&rect);
  rect.bottom += 800;
  CPoint pt1;
  GetCursorPos(&pt1);
  if(rect.PtInRect(pt1)) 
  {
   ::SendMessage(GetDlgItem(IDD_VSEEBROWSBAR_COMBO)->GetSafeHwnd(), WM_MOUSEWHEEL, pMsg->wParam, pMsg->lParam);
  } 
 }
 if (pMsg->message == WM_KEYDOWN)
 {
  int nControlID = GetFocus()->GetDlgCtrlID();
  if (IDC_EDIT_COMBO == nControlID)
  {
   m_acComboURL.SetAutoComp(TRUE);
  
  }
  //OnKeyDown(pMsg->wParam, HIWORD(pMsg->lParam), LOWORD(pMsg->lParam));
 }

    return CDialog::PreTranslateMessage(pMsg);
}

转载:http://blog.sina.com.cn/s/blog_4b27e9200100mv69.html

MFCpicture

在一个自定义的Dialog中加入了picture控件,想要获取鼠标在该控件上的点击位置,遇到一些困难,最终解决了。方法如下:  
     其实挺简单的,首先用自定义的Dialog类重载CDialog的PreTranslateMessage函数,并在其中用到了Dialog的OnLButtonDown函数(其实不用也行,我只是想把操作封在这个函数里)这样就可以通过此函数传递点击位置。 BOOL PrintDialog::PreTranslateMessage(MSG* pMsg) { 
// TODO: Add your specialized code here and/or call the base class  
if(pMsg->message == WM_LBUTTONDOWN && 
GetDlgItem(IDC_PACKPIC)->GetSafeHwnd() == pMsg->hwnd)  
   OnLButtonDown(MK_LBUTTON, pMsg->pt);   //在此传递点击部位在对话框中的坐标  
return CDialog::PreTranslateMessage(pMsg); }  
接下来在Dialog中的OnLButtonDown函数中判段是否点在picture控件内(lRect是控件的区域) 
if((point.x>=lRect.left && point.x<=lRect.right) && (point.y>=lRect.top && point.y<=lRect.bottom)) { 
               //  通过对point的处理,获得实际在picture控件中的点击位置(坐标),完成。  
point.x-=lRect.left;  
point.y-=lRect.top; }

抱歉!评论已关闭.