如何在 WinForm 应用程序中以编程方式创建 WPF 窗口 [英] How to programmatically create a WPF window in a WinForm application

查看:33
本文介绍了如何在 WinForm 应用程序中以编程方式创建 WPF 窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的 WinForm 应用程序,现在无法移植到 WPF.但是,我需要一个窗口,该窗口具有一些在 WinForm 中无法实现的棘手的透明行为(是的,尝试了分层窗口,但这是行不通的).

I have an existing WinForm app which is too much to port to WPF right now. However, I need a window with some tricky transparency behavior that I cannot achieve in a WinForm (yes, tried Layerd Windows but it's a no-go).

WPF 提供了我需要的透明行为,非常漂亮和简单.

WPF allows the transparency behavior I need beautifully and simply.

我当然用谷歌搜索,但只能找到如何在 WinForm 中创建 WPF 控件的提示,但这不是我需要的.我需要一个单独的 WPF 窗口,它完全独立于我的其他表单.

I googled of course, but can only find hints how to create a WPF control within a WinForm but that is NOT what I need. I need a separate WPF window that is completely independant of my other Forms.

WPF 窗口将是一个相当简单的全屏和无边框覆盖窗口,我将在其中绘制一些简单的绘图,每个绘图都有不同的透明度.

The WPF window will be a rather simple full-screen and borderless overlay window where I will do some simple drawings, each with different transparencies.

如何在 WinForm 应用程序中创建 WPF 窗口?

How can I create a WPF window within a WinForm application?

推荐答案

向项目添加必要的 WPF 引用,创建 WPF Window-instance,调用 EnableModelessKeyboardInterop 并显示窗口.

Add the necessary WPF references to your project, create a WPF Window-instance, call EnableModelessKeyboardInterop and show the window.

EnableModelessKeyboardInterop 的调用确保您的 WPF 窗口将从您的 Windows 窗体应用程序获取键盘输入.

The call to EnableModelessKeyboardInterop makes sure, that your WPF window will get keyboard inputs from your Windows Forms app.

请注意,如果您从 WPF 窗口中打开一个新窗口,键盘输入将不会路由到这个新窗口.您还必须为这些新创建的窗口调用 EnableModelessKeyboardInterop.

Take care, if you open a new Window from within your WPF window, the keyboard input will not be routed to this new window. You have to call also for these newly created windows EnableModelessKeyboardInterop.

对于您的其他要求,请使用 Window.TopmostWindow.AllowsTransparency.不要忘记将 WindowStyle 设置为None,否则不支持透明.

Fore your other requirements, use Window.Topmost and Window.AllowsTransparency. Don't forget to set the WindowStyle to None, otherwise, transparency is not supported.

更新
应添加以下参考以在 Windows 窗体应用程序中使用 WPF:

Update
The following references should be added to use WPF in your windows forms application:

  • PresentationCore
  • 演示框架
  • 系统.Xaml
  • WindowsBase
  • WindowsForms 集成

这篇关于如何在 WinForm 应用程序中以编程方式创建 WPF 窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆