在 HwndHost 父级富编辑控件中键入内容时,WPF 快捷方式会激活

发布于 2024-10-30 20:18:31 字数 1777 浏览 6 评论 0原文

我正在编写一个 WPF 应用程序,它封装了旧版 MFC 富文本编辑器。我已将富文本编辑器包装在 HwndHost 中。 HwndHost 控件位于 WPF TabControl 旁边。

运行应用程序的屏幕截图,位于 http://www.kempy.co.uk/code/Test。除了

我已经设法解决的聚焦问题和导航问题(上/下/左/右/制表符/输入)之外,我仍然有一个令人震惊的问题:

每当在富文本控件中键入一个字符时这是面板上的快捷键,激活该快捷键后,richedit 控件永远不会获取该字符。附加了一个简单的测试项目来演示 http://www.kempy.co 上的行为。 uk/code/Test.zip。在富编辑控件中按 P 或 C 将使面板聚焦或单击按钮,即使富文本控件具有键盘焦点也是如此。

仅供参考,这里是允许富编辑控件处理 Tab、Enter、Up、Down、Left、Right 的代码

#undef TranslateAccelerator
virtual bool TranslateAccelerator (System::Windows::Interop::MSG% msg, ModifierKeys modifiers) = IKeyboardInputSink::TranslateAccelerator
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());

    if (msg.message == WM_KEYDOWN)
    {
        // we want tabs when the rich edit is focused, send rich edit control a tab WM_CHAR 
        if (msg.wParam == (IntPtr)VK_TAB)
        {
            if (GetFocus() == m_pRichEdit->GetSafeHwnd())
            {
                m_pRichEdit->SendMessage(WM_CHAR, '\t', 0);
                return true;
            }
            else
            {
                return false;
            }
        }
        else if (
            msg.wParam == (IntPtr)VK_UP
            || msg.wParam == (IntPtr)VK_DOWN
            || msg.wParam == (IntPtr)VK_LEFT
            || msg.wParam == (IntPtr)VK_RIGHT
            || msg.wParam == (IntPtr)VK_RETURN)
        {
            // need cursor keys and enter/return, send KEYDOWN messages to rich edit control
            m_pRichEdit->SendMessage(msg.message, msg.wParam.ToInt32(), msg.lParam.ToInt32());
            return true;
        }

    }
    return false;
}

I am writing a WPF app that wraps up a legacy MFC richtext editor. I have wrapped the richtext editor in a HwndHost. The HwndHost control sits alongside a WPF TabControl.

Screenshot of running app at http://www.kempy.co.uk/code/Test.png

Apart from the focusing issues and navigation issues (up/down/left/right/tab/enter) which I have managed to resolve, I still have one showstopping issue:

Whenever a character is typed in the rich text control that is a shortcut key on the panel, the shortcut is activated and the richedit control never gets that character. A simple test project is attached to demonstrate the behaviour at http://www.kempy.co.uk/code/Test.zip. Pressing P or C in the rich edit control will focus the panel or click the button, even though the rich text control has keyboard focus.

FYI, here is the code that allows the rich edit control to handle Tab, Enter, Up, Down, Left, Right

#undef TranslateAccelerator
virtual bool TranslateAccelerator (System::Windows::Interop::MSG% msg, ModifierKeys modifiers) = IKeyboardInputSink::TranslateAccelerator
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());

    if (msg.message == WM_KEYDOWN)
    {
        // we want tabs when the rich edit is focused, send rich edit control a tab WM_CHAR 
        if (msg.wParam == (IntPtr)VK_TAB)
        {
            if (GetFocus() == m_pRichEdit->GetSafeHwnd())
            {
                m_pRichEdit->SendMessage(WM_CHAR, '\t', 0);
                return true;
            }
            else
            {
                return false;
            }
        }
        else if (
            msg.wParam == (IntPtr)VK_UP
            || msg.wParam == (IntPtr)VK_DOWN
            || msg.wParam == (IntPtr)VK_LEFT
            || msg.wParam == (IntPtr)VK_RIGHT
            || msg.wParam == (IntPtr)VK_RETURN)
        {
            // need cursor keys and enter/return, send KEYDOWN messages to rich edit control
            m_pRichEdit->SendMessage(msg.message, msg.wParam.ToInt32(), msg.lParam.ToInt32());
            return true;
        }

    }
    return false;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

み格子的夏天 2024-11-06 20:18:31

您需要重写 OnMnemonic 吗?

Dr Dobbs 关于互操作的文章...

http://drdobbs.com/windows/197003872?pgno=3

Do you need to override OnMnemonic?

Dr Dobs article about interop...

http://drdobbs.com/windows/197003872?pgno=3

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文