在CComboBox的编辑控件中检测键盘热键 [英] Detect keyboard hotkey inside edit control of CComboBox
问题描述
我有此代码:
BOOL CChristianLifeMinistryStudentMaterialDlg::PreTranslateMessage(MSG* pMsg)
{
BOOL bNoDispatch, bDealtWith;
bDealtWith = FALSE;
if (IsCTRLpressed() &&
pMsg->message == WM_KEYDOWN && pMsg->wParam == _TINT(_T('I')))
{
if (EncodeText(pMsg->hwnd, _T("i")))
{
// Eat it.
bNoDispatch = TRUE;
bDealtWith = TRUE;
}
}
else if (IsCTRLpressed() &&
pMsg->message == WM_KEYDOWN && pMsg->wParam == _TINT(_T('B')))
{
if (EncodeText(pMsg->hwnd, _T("b")))
{
// Eat it.
bNoDispatch = TRUE;
bDealtWith = TRUE;
}
}
if (!bDealtWith)
bNoDispatch = CDialogEx::PreTranslateMessage(pMsg);
return bNoDispatch;
}
最初,我的对话框中有3个CEdit
控件.当您按下此键时,它会对编辑控件中的选择执行上述操作.
Originally, I had 3 CEdit
controls on my dialog. When you used this key press it performed an action as above on the selection in the edit controls.
我将控件从CEdit
更改为CComboBox
.它们是可编辑的类型.我将EncodeText
调整为使用GetEditSel
和SetEditSel
.
I changed the controls from CEdit
to CComboBox
. They are editable type. I adjusted EncodeText
to use GetEditSel
and SetEditSel
.
现在唯一的问题是当我在组合框中编辑文本时.我选择了一些文本,然后按CTRL + I,但是什么也没有发生.我的对话框的PTM未被拦截.
Only problem is now when I am editing text in the combo box. I select some of the text and press CTRL + I and nothing happens. The PTM of my dialog is not getting intercepted.
在此CEdit
控件中,我可以选择文本:
In this CEdit
control I can select text:
然后我使用其中一个热键,例如: CTRL + B ,它仍然有效:
Then I use one of the hot keys, eg: CTRL + B and it still works:
但是,当我在可编辑的CComboBox
中选择一些文本并使用相同的热键时:
But, when I select some text in the editable CComboBox
and use the same hot key:
在这种情况下,它不起作用.
In this case it is not working.
我认为这是因为从技术上讲,我位于组合的嵌入式编辑"控件中.既然我正在组合框中使用选定的文本,如何仍然检测热键?
I have assumed it is because technically I am inside a embedded "Edit" control of the combo. How do I still detect the hot keys now that I am using selected text inside a combo?
推荐答案
我最终创建了一个新类CEncodedCombBox
,它是从CComboBox
派生的,像这样:
I ended up creating a new class CEncodedCombBox
, derived from CComboBox
, like this:
// EncodedComboBox.cpp : implementation file
//
#include "stdafx.h"
#include "Meeting Schedule Assistant.h"
#include "EncodedComboBox.h"
// CEncodedComboBox
IMPLEMENT_DYNAMIC(CEncodedComboBox, CComboBox)
CEncodedComboBox::CEncodedComboBox()
{
}
CEncodedComboBox::~CEncodedComboBox()
{
}
BEGIN_MESSAGE_MAP(CEncodedComboBox, CComboBox)
END_MESSAGE_MAP()
// CEncodedComboBox message handlers
BOOL CEncodedComboBox::PreTranslateMessage(MSG* pMsg)
{
BOOL bNoDispatch, bDealtWith;
DWORD dwSel = GetEditSel();
CString strCode = _T(""), strText;
GetWindowText(strText);
bDealtWith = FALSE;
if (IsCTRLpressed() &&
pMsg->message == WM_KEYDOWN && pMsg->wParam == _TINT(_T('I')))
{
strCode = _T("i");
bNoDispatch = TRUE;
bDealtWith = TRUE;
}
else if (IsCTRLpressed() &&
pMsg->message == WM_KEYDOWN && pMsg->wParam == _TINT(_T('B')))
{
strCode = _T("b");
bNoDispatch = TRUE;
bDealtWith = TRUE;
}
if (bDealtWith)
{
CMeetingScheduleAssistantApp::EncodeText(strText, strCode, LOWORD(dwSel), HIWORD(dwSel));
SetWindowText(strText);
SetEditSel(HIWORD(dwSel) + 7, HIWORD(dwSel) + 7);
}
if (!bDealtWith)
bNoDispatch = CComboBox::PreTranslateMessage(pMsg);
return bNoDispatch;
}
如您所见,它包含一个PreTranslateMessage
并且有效:
As you can see, it includes a PreTranslateMessage
and it works:
如果有更好的方法,那么我欢迎您的评论或回答.
If there is a better way then I welcome your comments or answer.
我必须针对编辑控件句柄进行测试,而不必为自己的CDialog
工作而使用组合框句柄:
I had to test against the edit control handle and not to combo box handle for my own CDialog
to work:
if (::GetParent(hWnd) == m_cbMaterialAssignment1.GetSafeHwnd())
不再需要派生的组合类.
No derived combo class needed any more.
这篇关于在CComboBox的编辑控件中检测键盘热键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!