托管 WPF UserControls 时如何在 WinForms MDI 应用程序中获取默认的 Ctrl+Tab 功能

发布于 2024-09-04 03:50:29 字数 428 浏览 2 评论 0原文

我有一个基于 WinForms 的应用程序,其中包含传统的 MDI 实现,只不过我通过 ElementHost 控件托管基于 WPF 的 UserControls 作为每个 MDI 子项的主要内容。这是Microsoft 推荐的解决方案 用于使用 WPF 实现 MDI,尽管不幸的是存在各种副作用。其中之一是我在每个 MDI 子项之间进行选项卡切换的 Ctrl+Tab 功能消失了,因为 Tab 键似乎被 WPF 控件吞没了。

是否有一个简单的解决方案可以让 Ctrl+tab 键序列到达我的 WinForms MDI 父级,以便我可以获得内置的选项卡切换功能?

I have a WinForms based app with traditional MDI implementation within it except that I'm hosting WPF based UserControls via the ElementHost control as the main content for each of my MDI children. This is the solution recommended by Microsoft for achieving MDI with WPF although there are various side effects unfortunately. One of which is that my Ctrl+Tab functionality for tab switching between each MDI child is gone because the tab key seems to be swallowed up by the WPF controls.

Is there a simple solution to this that will let the Ctrl+tab key sequences reach my WinForms MDI parent so that I can get the built-in tab switching functionality?

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

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

发布评论

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

评论(1

甜警司 2024-09-11 03:50:29

在主机 WinForm 中,为托管 WPF 控件添加一个 PreviewKeyDown 处理程序,该处理程序捕获 Ctrl-(Shift)-Tab、激活下一个或上一个 MDI 子项并将事件标记为已处理:

TheHostedWpfControl.PreviewKeyDown += (s, e) =>
{
    if (e.Key == Key.Tab && ModifierKeys.HasFlag(Keys.Control))
    {
        ActivateNextMdiChild(ModifierKeys.HasFlag(Keys.Shift));
        e.Handled = true;
    }
};

以下是下一个/上一个 MDI 子项激活:

[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, int lParam);

private const int WM_MDINEXT = 0x224;

private void ActivateNextMdiChild(bool backward = false)
{
    if (MdiParent != null)
    {
        MdiClient mdiClient = MdiParent.Controls.OfType<MdiClient>().FirstOrDefault();
        if (mdiClient != null)
        {
            SendMessage(mdiClient.Handle, WM_MDINEXT, Handle, backward ? 1 : 0);
        }
    }
}

In the host WinForm add a PreviewKeyDown handler for the hosted WPF control that captures Ctrl-(Shift)-Tab, activates the next or previous MDI child and marks the event as handled:

TheHostedWpfControl.PreviewKeyDown += (s, e) =>
{
    if (e.Key == Key.Tab && ModifierKeys.HasFlag(Keys.Control))
    {
        ActivateNextMdiChild(ModifierKeys.HasFlag(Keys.Shift));
        e.Handled = true;
    }
};

And here is the next/prev MDI child activation:

[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, int lParam);

private const int WM_MDINEXT = 0x224;

private void ActivateNextMdiChild(bool backward = false)
{
    if (MdiParent != null)
    {
        MdiClient mdiClient = MdiParent.Controls.OfType<MdiClient>().FirstOrDefault();
        if (mdiClient != null)
        {
            SendMessage(mdiClient.Handle, WM_MDINEXT, Handle, backward ? 1 : 0);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文