为什么在对话框里添加键盘响应函数(ONKEYDOWN)没有用??

解决方案 »

  1.   

    BOOL CTestDlg::PreTranslateMessage(MSG* pMsg) 
    {
     if(pMsg->message==WM_KEYDOWN) 
     { 
         AfxMessageBox("你按了键盘!");
      } 
     return CDialog::PreTranslateMessage(pMsg);}
      

  2.   

    通过不同的键来调用不同的函数呢?比如按方向键VK_LFET...
    怎样进行不同键的选择?
    直接响应WM_KEYDOWN时,可以通过nChar来进行条件分支,如:
    switch (nChar)
    {
       case (VK_LEFT):
          函数1;
       break;
         .
         .
    }
    你们那样重载怎样条件分支?
    给点代码好吗?小弟很菜的!!
      

  3.   

    BOOL CTestEscDlg::PreTranslateMessage(MSG* pMsg) 
    {
    // TODO: Add your specialized code here and/or call the base class
    if(pMsg->message == WM_KEYDOWN)
     switch(pMsg->wParam)
    {
    case VK_UP: 
    {
    //do somethin
    break;
    }
    case VK_DOWN:
    {
    break;
    }
    default:
    break;
    }

    return CDialog::PreTranslateMessage(pMsg);
    }
      

  4.   

    谢谢各位大侠,已经搞定了!!
    能具体给一下MSG* 中各参数的具体含义吗?
       hwnd---
       lParam---
       message---
       pt---
       time---
       wParam---
    谢谢!!
      

  5.   

    msdn 中有呀!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. 
      

  6.   

    对话框的setfocus不同于一般setfocus,本身可能已经设置过了.
    所以应该重载他,去掉里面的内容,再次用这个setfocus
    我只要50分,呵呵
    部分代码
    void CdlgDlg::OnSetFocus(CWnd* pOldWnd)
    {
    ::SetFocus(this->m_hWnd);//焦点 // TODO: 在此处添加消息处理程序代码
    }
    void CdlgDlg::OnBnClickedButton1()
    {
    this->SetFocus();//设置焦点
    }
    void CdlgDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
    {
    MessageBox("fan",0,MB_OK);//效果
    CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
    }