How to programmatically create a WPF window in a WinForm application

前端 未结 2 1667
萌比男神i
萌比男神i 2020-12-10 13:34

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

2条回答
  •  無奈伤痛
    2020-12-10 13:57

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

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

    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.

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

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

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

提交回复
热议问题