2

我有这个代码:

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;
}

最初,我CEdit的对话框中有 3 个控件。当您使用此键按下时,它会对编辑控件中的选择执行上述操作。

我将控件从 更改CEditCComboBox。它们是可编辑类型。我调整EncodeText为使用GetEditSeland SetEditSel

现在唯一的问题是当我在组合框中编辑文本时。我选择了一些文本并按 CTRL + I 并没有任何反应。我的对话的 PTM 没有被拦截。

视觉示例

在此CEdit控件中,我可以选择文本:

选择文本

然后我使用其中一个热键,例如:CTRL + B,它仍然有效:

CEdit 的结果

但是,当我在可编辑中选择一些文本CComboBox并使用相同的热键时:

在 CComboBox 上使用热键

在这种情况下,它不起作用。

我假设这是因为从技术上讲,我在组合的嵌入式“编辑”控件中。既然我在组合中使用选定的文本,我如何仍然检测热键?

4

2 回答 2

2

不确定我喜欢这个WM_KEYDOWN黑客。如果您有真正的热键,我建议您正确处理它们:

BEGIN_MESSAGE_MAP(CEncodedCombBox, CCombBox)
    ON_WM_HOTKEY()
END_MESSAGE_MAP()

void CEncodedCombBox::OnHotKey(UINT nHotKeyId, UINT nKey1, UINT nKey2)
{
    if (nHotKeyId == idForHotKey_I)
        HandleCode(_T("i"));
    else if (nHotKeyId == idForHotKey_B)
        HandleCode(_T("b"));
}

void CEncodedCombBox::HandleCode(CString strCode)
{
    DWORD dwSel = GetEditSel();

    CMeetingScheduleAssistantApp::EncodeText(strText, strCode, LOWORD(dwSel), HIWORD(dwSel));
    SetWindowText(strText);
    SetEditSel(LOWORD(dwSel), HIWORD(dwSel) + 7);
}
于 2017-10-19T08:50:27.087 回答
0

我最终创建了一个新类CEncodedCombBox,派生自CComboBox,如下所示:

// 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;
}

如您所见,它包含 aPreTranslateMessage并且可以正常工作:

热键现在可以使用

如果有更好的方法,那么我欢迎您的意见或回答。

更新

我必须针对编辑控件句柄进行测试,而不是针对我自己的组合框句柄进行测试CDialog

if (::GetParent(hWnd) == m_cbMaterialAssignment1.GetSafeHwnd())

不再需要派生的组合类。

于 2017-10-18T20:56:30.750 回答