8

我有一个编辑控件

HWND hInput = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", 
    WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_MULTILINE | ES_AUTOVSCROLL, 
    0, 0, 100, 100, hwnd, (HMENU)IDC_MAIN_INPUT, GetModuleHandle(NULL), NULL);

和一个按钮:

HWND hSendButton = CreateWindowEx(WS_EX_CLIENTEDGE,"BUTTON","Send!",
        BS_DEFPUSHBUTTON | WS_VISIBLE | WS_CHILD,
        0,0,0,0,hwnd,(HMENU)IDC_MAIN_SENDBUTTON,GetModuleHandle(NULL),NULL);

有没有办法查看在编辑控件中键入时按下 Enter 键并发送消息,如按下按钮?

我想也许

SendMessage(hwnd,WM_COMMAND,(WPARAM)IDC_MAIN_SENDBUTTON,LPARAM(0));

会完成发送消息的工作,但我仍然坚持捕获 Enter 键。

先感谢您。

4

2 回答 2

14

您需要子类化编辑控件并处理 WM_KEYDOWN 消息。如果它是您想要的密钥,请发送消息,否则,让默认的编辑控制程序完成它的工作。

代码如下:

WNDPROC oldEditProc;

LRESULT CALLBACK subEditProc(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
   switch (msg)
   {
    case WM_KEYDOWN:
         switch (wParam)
         {
          case VK_RETURN:
          //Do your stuff
              break;  //or return 0; if you don't want to pass it further to def proc
          //If not your key, skip to default:
         }
    default:
         return CallWindowProc(oldEditProc, wnd, msg, wParam, lParam);
   }
   return 0;
}

void somecreateeditproc()
{
  HWND hInput = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", 
    WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_MULTILINE | ES_AUTOVSCROLL, 
    0, 0, 100, 100, hwnd, (HMENU)IDC_MAIN_INPUT, GetModuleHandle(NULL), NULL);

  oldEditProc = (WNDPROC)SetWindowLongPtr(hInput, GWLP_WNDPROC, (LONG_PTR)subEditProc);
}
于 2013-03-29T21:04:25.793 回答
1

我在对话框中这样做:

BOOL CDialogObject::PreTranslateMessage(MSG* pMsg)
{
    if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN)
    {
        SendMessage(WM_COMMAND, (WPARAM)IDC_BUTTON3,LPARAM(0));
    }
    return CDialog::PreTranslateMessage(pMsg);
}
于 2018-01-24T14:24:40.537 回答