简体   繁体   中英

How do I prevent keyboard events in a WPF dialog box from bleeding through to the MFC application which invoked it?

We have a legacy MFC application which we are extending with new WPF views and dialog boxes. I am trying to implement F1 help in a WPF dialog box that gets invoked from the MFC main window.

Initially I added a KeyBinding to the WPF dialog for F1, and had it fire a command that runs HtmlHelp ; something like this:

<Window.Resources>
    <command:CommandReference x:Key="ShowF1Help" Command="{Binding ShowHelpCommand}"/>
</Window.Resources>

<Window.InputBindings>
    <KeyBinding x:Name="ShowHelp" Gesture="F1" Command="{StaticResource ShowF1Help}"/>
</Window.InputBindings>

This brought up Help, but unfortunately the keyboard event was also picked up by the MFC window; even though the WPF dialog was modally displayed on top of it, the MFC window still received F1 and so it launched HtmlHelp a second time, showing its own topic.

I searched for a way to mark the event as handled within the XAML/KeyBinding element but had no luck. So I tried to brute-force it and replaced that with a KeyDown handler in the code-behind, marking the event as handled, like this:

private void WindowKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.F1)
    {
        MyDialogVM.ShowContextSensitiveHelp();
        e.Handled = true;
    }
}

This didn't work; the event still winds up getting handled by the MFC window. I also tried PreviewKeyDown --same results.

I have a feeling that I'm overlooking something obvious, but it sure looks like marking a WPF event as handled only affects WPF code, and the MFC message pump has no knowledge that the managed code has seen or handled a given keyboard event.

Is there a way to prevent keyboard events that are handled in a WPF dialog box from also being seen by the MFC application that invoked the dialog?

Thanks in advance.

Couple of questions: 1) is the owner of the WPF dialog the MFC window? If you didn't do anything explicitly, it isn't by default. 2) is the main window disabled when the wpf dialog is up? If it isn't, probably because of issue 1.

You need something like:

var hlpr = new System.Windows.Interop.WindowInteropHelper( xaml_window );
hlpr.Owner = mfc_window_handle;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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