如何以编程方式在WinForm应用程序中创建WPF窗口

时间:2022-09-10 21:03:08

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).

我有一个现有的WinForm应用程序,现在移植到WPF太多了。但是,我需要一个窗口,其中有一些棘手的透明行为,我无法在WinForm中实现(是的,尝试了Layerd Windows,但它是不行的)。

WPF allows the transparency behavior I need beautifully and simply.

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.

我当然用Google搜索,但只能找到如何在WinForm中创建WPF控件的提示,但这不是我需要的。我需要一个单独的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.

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

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

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

2 个解决方案

#1


14  

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

向项目添加必要的WPF引用,创建WPF Window实例,调用EnableModelessKeyboardInterop并显示窗口。

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

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

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.

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

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

根据您的其他要求,使用Window.Topmost和Window.AllowsTransparency。不要忘记将WindowStyle设置为None,否则不支持透明度。

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

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

  • PresentationCore
  • PresentationCore
  • PresentationFramework
  • PresentationFramework
  • System.Xaml
  • System.Xaml
  • WindowsBase
  • WindowsBase
  • WindowsFormsIntegration
  • WindowsFormsIntegration程序

#2


6  

Here's the (tested) solution. This code can be used in both a WinForm or a WPF app. No XAML needed at all.

这是(经过测试的)解决方案。此代码可以在WinForm或WPF应用程序中使用。根本不需要XAML。

#region WPF
// include following references:
//   PresentationCore
//   PresentationFramework
//   WindowsBase

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
 #endregion


public class WPFWindow : Window
{

    private Canvas canvas = new Canvas();

    public WPFWindow()
    {
        this.AllowsTransparency = true;
        this.WindowStyle = WindowStyle.None;
        this.Background = Brushes.Black;
        this.Topmost = true;

        this.Width = 400;
        this.Height = 300;
        canvas.Width = this.Width;
        canvas.Height = this.Height;
        canvas.Background = Brushes.Black;
        this.Content = canvas;
    }
}

The window background is fully transparent. You can draw on the canvas and each element can have it's own transparency (which you can determine by setting the alpha channel of the Brush used to draw it). Simply invoke the window with something like

窗口背景完全透明。您可以在画布上绘制,每个元素都可以拥有自己的透明度(您可以通过设置用于绘制它的画笔的Alpha通道来确定)。只需用类似的东西调用窗口即可

WPFWindow w = new WPFWindow();
w.Show();

#1


14  

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

向项目添加必要的WPF引用,创建WPF Window实例,调用EnableModelessKeyboardInterop并显示窗口。

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

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

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.

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

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

根据您的其他要求,使用Window.Topmost和Window.AllowsTransparency。不要忘记将WindowStyle设置为None,否则不支持透明度。

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

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

  • PresentationCore
  • PresentationCore
  • PresentationFramework
  • PresentationFramework
  • System.Xaml
  • System.Xaml
  • WindowsBase
  • WindowsBase
  • WindowsFormsIntegration
  • WindowsFormsIntegration程序

#2


6  

Here's the (tested) solution. This code can be used in both a WinForm or a WPF app. No XAML needed at all.

这是(经过测试的)解决方案。此代码可以在WinForm或WPF应用程序中使用。根本不需要XAML。

#region WPF
// include following references:
//   PresentationCore
//   PresentationFramework
//   WindowsBase

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
 #endregion


public class WPFWindow : Window
{

    private Canvas canvas = new Canvas();

    public WPFWindow()
    {
        this.AllowsTransparency = true;
        this.WindowStyle = WindowStyle.None;
        this.Background = Brushes.Black;
        this.Topmost = true;

        this.Width = 400;
        this.Height = 300;
        canvas.Width = this.Width;
        canvas.Height = this.Height;
        canvas.Background = Brushes.Black;
        this.Content = canvas;
    }
}

The window background is fully transparent. You can draw on the canvas and each element can have it's own transparency (which you can determine by setting the alpha channel of the Brush used to draw it). Simply invoke the window with something like

窗口背景完全透明。您可以在画布上绘制,每个元素都可以拥有自己的透明度(您可以通过设置用于绘制它的画笔的Alpha通道来确定)。只需用类似的东西调用窗口即可

WPFWindow w = new WPFWindow();
w.Show();