Alt键和选项卡在从WPF应用程序打开的Windows窗体中不起作用

Fra*_*sca 5 c# wpf winforms alt-key

我有很多旧的Windows窗体应用程序,最终将移植到WPF(它是一个大型应用程序,所以它不能在一个sprint中完成),我已经通过在WPF中创建一个主菜单开始了这个过程.Windows窗体应用程序是从此菜单打开的单独窗口.

Windows窗体应用程序正在打开和工作,没有任何问题,除了我使用快捷方式和Tab密钥的问题.Tab键没有将焦点移动到下一个控件,Alt触发&Search按钮的键不再起作用.

我究竟做错了什么?

Gar*_*one 6

我发现的一个部分解决方案是从您的 WPF 构造函数中调用它: System.Windows.Forms.Integration.WindowsFormsHost.EnableWindowsFormsInterop(); (您需要引用 dll WindowsFormsIntegration.dll)

我说部分是因为并非所有按键都按预期工作。例如,对于简单的表单似乎可以正常工作。

请参阅:http :
//msdn.microsoft.com/en-us/library/system.windows.forms.integration.windowsformshost.enablewindowsformsinterop(v=vs.100).aspx


Fra*_*sca 4

我最终设法通过将 winform 托管在 WPF 表单内的 WindowsFormsHost 控件内来解决该问题。

public partial class MyWindow : Window
{
    public MyWindow()
    {
        InitializeComponent();

        Form winform = new Form();
        // to embed a winform using windowsFormsHost, you need to explicitly
        // tell the form it is not the top level control or you will get
        // a runtime error.
        winform.TopLevel = false;

        // hide border because it will already have the WPF window border
        winform.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        this.windowsFormsHost.Child = winform;
    }

}
Run Code Online (Sandbox Code Playgroud)

请注意,如果您有关闭表单的按钮,您可能还需要连接 winform 关闭事件。