[ATL/WTL]_[初级]_[关于CEdit不识别WM_KEYDOWN的问题]

来源:互联网 发布:linux yum网络安装 编辑:程序博客网 时间:2024/05/17 02:56

场景

  1. Win32 的Edit 控件如果没有Accelerator的话默认支持Ctrl+a, Ctrl+V等全选, 粘贴操作, 不需要自己添加支持.
  2. 如果需要自己添加回车响应搜索实现的话,常见的方式是在 PreTranslateMessage 里添加对CEdit的监听和操作;
BOOL PreTranslateMessage(MSG* pMsg){    if(GetFocus() != search_edit_)    {        return FALSE;    }    if(pMsg->wParam == VK_RETURN)    {        if(pMsg->message == WM_KEYDOWN)        {            BOOL bHandled = FALSE;            OnSearch(0,0,NULL,bHandled);            return bHandled;        }    }else if(pMsg->message == WM_KEYDOWN && (GetKeyState( VK_CONTROL) & 0xFF00 ) == 0xFF00)     {        // 全选        if( pMsg->wParam == 'A' || pMsg->wParam == 'a')        {            search_edit_.SetSel(0, -1);            return TRUE;        }        // 拷贝        if( pMsg->wParam == 'C' || pMsg->wParam == 'c')        {            search_edit_.Copy();            return TRUE;        }        // 剪切        if( pMsg->wParam == 'X' || pMsg->wParam == 'x')        {            search_edit_.Cut();            return TRUE;        }        // 粘贴        if( pMsg->wParam == 'V' || pMsg->wParam == 'v')        {            search_edit_.Paste();            std::cout << "paste" << std::endl;            return TRUE;        }        // 粘贴        if( pMsg->wParam == 'Z' || pMsg->wParam == 'z')        {            search_edit_.Undo();            return TRUE;        }    }    return FALSE;}

说明

  1. 但是有时候突然它就不响应Ctrl+V里, 很奇怪的现象, 不同的项目相同的代码有的却不响应Ctrl+V, 但却响应Ctrl+A.
  2. 使用Spy++来查看窗口的消息, 发现是有WM_KEYDOWN消息的.

图1

  1. 因为自定义的 CMessageFilter 子窗口类, 并不是 直接在 CMainFrame里添加过滤, 所以可能是CMainFrame里的 PreTranslateMessage 过滤了这层消息, 在以下代码调用之前是有WM_KEYDOWN消息的, 调用完之后这个消息丢失.
if(CFrameWindowImpl<CMainFrame>::PreTranslateMessage(pMsg))        return TRUE;

看 TranslateAccelerator 的说明很显然了, 是全局快捷键过滤了:

Processes accelerator keys for menu commands. The function translates a WM_KEYDOWN or WM_SYSKEYDOWN message to a WM_COMMAND or WM_SYSCOMMAND message (if there is an entry for the key in the specified accelerator table) and then sends the WM_COMMAND or WM_SYSCOMMAND message directly to the specified window procedure. TranslateAccelerator does not return until the window procedure has processed the message.
  1. 解决办法就是删除这个Accelerator, 在资源文件rc里删除 Accelerator 或者删除Ctrl+V的快捷键映射:
    图2

参考

win32 select all on edit ctrl
TranslateAccelerator function
WM_CHAR message

0 0