简体   繁体   English

如何禁用CComboBox mfc键盘导航?

[英]How to disable CComboBox mfc keyboard navigation?

How i can disable CComboBox mfc keyboard navigation, i need when i press key on keyboard with open dropdown list, item must not selecting. 我怎么能禁用CComboBox mfc键盘导航,我需要当我按下键盘打开下拉列表时,项目必须不选择。 Thanks! 谢谢!

A simple solution without subclassing the combobox is to set its first child window (which is the CEdit box) to readonly, like this : 没有子类化组合框的简单解决方案是将其第一个子窗口(即CEdit框)设置为只读,如下所示:

GetDlgItem(IDC_MY_COMBO)->GetWindow(GW_CHILD)->SendMessage(EM_SETREADONLY, 1, 0); GetDlgItem(IDC_MY_COMBO) - > GetWindow(GW_CHILD) - > SendMessage(EM_SETREADONLY,1,0);

If you really just mean: "how do I disable the control from being changed?", then just call the EnableWindow method on the CComboBox. 如果你真的只是说:“如何禁止控制被更改?”,然后只需在CComboBox上调用EnableWindow方法。

But if you really mean you just want to block keyboard messages from hitting the control, then use window subclassing to swallow keyboard messages. 但是如果你真的想要阻止键盘消息阻止控制,那么使用窗口子类来吞下键盘消息。 (Don't confuse the term "window subclassing" with C++ classes - not the same thing). (不要将术语“window subclassing”与C ++类混淆 - 不一样)。 Basically, we're just going to intercept all WM_CHAR and WM_KEYDOWN messages associated with the combo box and let all the other messages pass. 基本上,我们只是拦截与组合框关联的所有WM_CHAR和WM_KEYDOWN消息,并让所有其他消息通过。

Do this: 做这个:

WNDPROC g_prevFunc = NULL;

LRESULT MyWindowHook(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    if ((uMsg == WM_CHAR) || (uMsg == WM_KEYDOWN) || (uMsg == WM_KEYUP))
    {
        return 0; // swallow message
    }

    return ::CallWindowProcW(g_prevFunc, hWnd, uMsg, wParam, lParam);
}


void MySubclassWindow(HWND hwnd)
{
    g_prevFunc = (WNDPROC)::SetWindowLongW(hwnd, GWL_WNDPROC, (LONG_PTR)MyWindowHook);
}

// wherever your code gets initialized
CYourWindow::OnInit()
{
   // whatever other initialization you got going on...

  // I'm assuming your CComboBox is named something like m_combobox.

  ::MySubclassWindow(m_combobox.m_hWnd);

}

Double check to make sure this doesn't break tab key navigation. 仔细检查以确保这不会破坏标签键导航。 I just tried and it seems to work fine. 我刚试过,似乎工作正常。 You may not need to swallow WM_CHAR, just might need to swallow WM_KEYUP and WM_KEYDOWN. 您可能不需要吞下WM_CHAR,可能需要吞下WM_KEYUP和WM_KEYDOWN。 Some experimentation on your part is likely needed. 您可能需要进行一些实验。

There's also an MFC method on the CWnd class called SubclassWindow. 在CWnd类上还有一个名为SubclassWindow的MFC方法。 So if you want to go pure MFC, you can look into this as well. 因此,如果您想要使用纯MFC,您也可以查看它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM