0

我正在尝试在笔记编辑控件中实现 ctrl+A:m_editNoteTypeView”,它是类 NoteDialog 中的 cEdit 实例。我的笔记编辑如下所示。

NoteDialog::initDialog()
{
    m_editNoteTypeView.CreateEx(::GetWindowLong(m_editSubject.m_hWnd, GWL_EXSTYLE), "edit", "", dwStyle | ES_READONLY, CRect(0, 0, 0, 0), this, 0);
    m_editNoteTypeView.SetSel(0,-1,TRUE);
}

NoteDialog 类派生自另一个名为 Sdialog 的类,该类最终派生自CDialog.

我已经PreTranslateMessage(MSG* pMsg)在 SDialog 中定义了,但控制不会转到 PreTranslateMessage ,因此当我在笔记编辑上打字时,我无法检查我在 keboard 上按了哪个键。

bool Sdialog::PreTranslateMessage(MSG* pMsg)
{
    if (GetFocus() == this) 
    {
        if (pMsg->message == WM_CHAR)
        {
            if ((LOWORD(pMsg->wParam) & VK_CONTROL) == VK_CONTROL)
            {
                //SetSel(0, -1);
            }
        }
    }

    return CDialog::PreTranslateMessage(pMsg);
}
4

1 回答 1

1

return类型 for PreTranslateMessageshould be BOOL,否则在 MFC 中会出现编译错误。

if (pMsg->message == WM_CHAR)
    if ((LOWORD(pMsg->wParam) & VK_CONTROL) == VK_CONTROL)
        {...}

WM_CHAR消息不携带VK_CONTROLwParam

拦截Ctrl+ A

  • 等待WM_KEYDOWN消息
  • 检查A密钥
  • 检查Ctrl按键是否被按下

因此:

BOOL Sdialog::PreTranslateMessage(MSG* pMsg)
{
    static int i = 0;
    CString s;

    if(pMsg->message == WM_KEYDOWN)
    {
        if(GetKeyState(VK_CONTROL) & 0x8000)
        {
            if(pMsg->wParam == 'A')
            {
                ...
            }
        }
    }

    return CDialog::PreTranslateMessage(pMsg);
}

此外,您可以添加这些帮助宏:

// Handy functions
#define IsCTRLpressed()  ( GetKeyState(VK_CONTROL) & 0x8000 )
#define IsSHIFTpressed()  ( GetKeyState(VK_SHIFT) & 0x8000 )
//15 = sizeof(SHORT) * 8 - 1
//0000 0000 0000 0001 = 1
//1000 0000 0000 0000 = 15 << 1

那么它可以很简单:

if (IsCTRLpressed() &&
    pMsg->message == WM_KEYDOWN && pMsg->wParam == _TINT(_T('A')))
{
    // Do whatever

    // Eat it.
    bNoDispatch = TRUE;
    bDealtWith = TRUE;
}

请记住,您必须决定是否仍要运行基本实现。如果您实际上自己处理消息并处理它,那么您应该 return TRUE。否则,让基类处理它。

例子:

BOOL Sdialog::PreTranslateMessage(MSG* pMsg)
{
    BOOL    bNoDispatch, bDealtWith;

    bDealtWith = FALSE;

    if (IsCTRLpressed() &&
        pMsg->message == WM_KEYDOWN && pMsg->wParam == _TINT(_T('A')))
    {
        // Deal with it

        // Eat it.
        bNoDispatch = TRUE;
        bDealtWith = TRUE;
    }

    if (!bDealtWith)
        bNoDispatch = CDialogEx::PreTranslateMessage(pMsg);

    return bNoDispatch;
}
于 2017-11-18T17:58:06.550 回答