今天在一个MFC的GUI程序中实现了一个自定义的列表控件类(CListCtrl),在这个类里嵌入了一个CEdit类以便于编辑列表项,为了实现在编辑每个列表项时能支持快捷键,在派生的CEdit类加入下面这个函数:
- BOOL CCustomizedListCtrl::CListEditor::PreTranslateMessage(MSG* pMsg)
- {
- // 编辑框快捷键操作
- if(WM_KEYDOWN == pMsg->message)
- {
- if(::GetFocus() == m_hWnd && (GetKeyState( VK_CONTROL) & 0xFF00 ) == 0xFF00)
- {
- // 全选
- if( pMsg->wParam == 'A' || pMsg->wParam == 'a')
- {
- this->SetSel(0, -1);
- return true;
- }
- // 拷贝
- if( pMsg->wParam == 'C' || pMsg->wParam == 'c')
- {
- this->Copy();
- return true;
- }
- // 剪切
- if( pMsg->wParam == 'X' || pMsg->wParam == 'x')
- {
- this->Cut();
- return true;
- }
- // 粘贴
- if( pMsg->wParam == 'V' || pMsg->wParam == 'v')
- {
- this->Paste();
- return true;
- }
- // 粘贴
- if( pMsg->wParam == 'Z' || pMsg->wParam == 'z')
- {
- this->Undo();
- return true;
- }
- }
- }
- return CEdit::PreTranslateMessage(pMsg);
- }
一开始实现时,编辑列表项不能捕捉焦点,后在google代码搜索中搜关键字PreTranslateMessage,才知道没加一个判断条件,::GetFocus() == m_hWnd。