Detect keyboard hotkey inside edit control of CComboBox

半城伤御伤魂 提交于 2019-12-11 04:06:39

问题


I have this code:

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

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.

I changed the controls from CEdit to CComboBox. They are editable type. I adjusted EncodeText to use GetEditSel and SetEditSel.

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.

Visual Example

In this CEdit control I can select text:

Then I use one of the hot keys, eg: CTRL + B and it still works:

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?


回答1:


Not sure I like the WM_KEYDOWN hack. If you have real hot keys, I suggest you handle them correctly:

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



回答2:


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

As you can see, it includes a PreTranslateMessage and it works:

If there is a better way then I welcome your comments or answer.

Update

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.



来源:https://stackoverflow.com/questions/46817844/detect-keyboard-hotkey-inside-edit-control-of-ccombobox

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!