如何在外部 Win32 应用程序的 window 上添加 WPF 覆盖?
How can I add a WPF overlay over an external Win32 application's window?
我正在尝试附加 WPF Window 作为外部应用程序(例如记事本)的子 window 以提供覆盖。在研究了我可以在 SO 和 MSDN 上找到的所有答案后,我已经在我的 WPF 应用程序运行时在记事本的一角创建了一个坚实的覆盖层。然而,
- 一旦记事本获得焦点,叠加层就会消失,
- 与在记事本上显示的叠加层一样,叠加层也单独显示为 window
- 记事本上的覆盖层不会接收任何 MouseMove 事件(但单独的 window 会。
这是演示该问题的最小示例:
Overlay.xaml
<Window x:Class="WindowControlTest.Overlay"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300"
Opacity="1"
Background="Azure"
MouseMove="Window_MouseMove"
GotFocus="Window_GotFocus"
Loaded="Window_Loaded"
Title="Overlay"
WindowStyle="None"
>
</Window>
Overlay.xaml.cs
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Interop;
namespace WindowControlTest
{
public partial class Overlay : Window
{
IntPtr m_ParentHwnd;
HwndSource m_HwndSource;
public Overlay(IntPtr parentHwnd)
{
InitializeComponent();
m_ParentHwnd = parentHwnd;
}
private void Window_MouseMove(object sender, MouseEventArgs e)
{
Console.WriteLine("Overlay.Window_MouseMove: " + e.GetPosition(this));
}
private void Window_GotFocus(object sender, RoutedEventArgs e)
{
Console.WriteLine("Overlay.Window_GotFocus");
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
HwndSourceParameters parameters = new HwndSourceParameters();
parameters.WindowStyle = (int) (WindowStyles.WS_VISIBLE | WindowStyles.WS_CHILD);
parameters.SetPosition(0, 0);
parameters.UsesPerPixelOpacity = true;
parameters.SetSize((int)Width, (int)Height);
parameters.ParentWindow = m_ParentHwnd;
m_HwndSource = new HwndSource(parameters);
m_HwndSource.CompositionTarget.BackgroundColor = Colors.Aqua;
m_HwndSource.RootVisual = (Visual)Content;
}
}
}
MainWindow.xaml
<Window x:Class="WindowControlTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
Loaded="OnLoaded"
>
<StackPanel>
<Label x:Name="stateLabel">Label</Label>
</StackPanel>
</Window>
MainWindow.xaml.cs - 查找并处理记事本并创建 Overlay
using System;
using System.Text;
using System.Windows;
namespace WindowControlTest
{
public partial class MainWindow : Window
{
private IntPtr m_TargetHwnd;
private Overlay m_Overlay;
public MainWindow()
{
InitializeComponent();
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
processWindows();
if (m_TargetHwnd != IntPtr.Zero)
{
m_Overlay = new Overlay(m_TargetHwnd);
m_Overlay.Show();
}
}
private void processWindows()
{
Win32.EnumWindows(delegate(IntPtr wnd, IntPtr param)
{
String text = GetWindowText(wnd);
Console.WriteLine("Window: " + text);
if (text.Contains("Notepad"))
{
m_TargetHwnd = wnd;
}
return true;
}, IntPtr.Zero);
}
public static string GetWindowText(IntPtr hWnd)
{
int size = Win32.GetWindowTextLength(hWnd);
if (size++ > 0)
{
var builder = new StringBuilder(size);
Win32.GetWindowText(hWnd, builder, builder.Capacity);
return builder.ToString();
}
return String.Empty;
}
}
}
(注意:许多 SO 问题解决了类似但不同的问题,例如 How to set Win32 window as owner of WPF window? assumes I am in control of the source code for the Win32 window, as do the examples 我可以在 MSDN 上找到。)
我正在尝试附加 WPF Window 作为外部应用程序(例如记事本)的子 window 以提供覆盖。在研究了我可以在 SO 和 MSDN 上找到的所有答案后,我已经在我的 WPF 应用程序运行时在记事本的一角创建了一个坚实的覆盖层。然而,
- 一旦记事本获得焦点,叠加层就会消失,
- 与在记事本上显示的叠加层一样,叠加层也单独显示为 window
- 记事本上的覆盖层不会接收任何 MouseMove 事件(但单独的 window 会。
这是演示该问题的最小示例:
Overlay.xaml
<Window x:Class="WindowControlTest.Overlay"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300"
Opacity="1"
Background="Azure"
MouseMove="Window_MouseMove"
GotFocus="Window_GotFocus"
Loaded="Window_Loaded"
Title="Overlay"
WindowStyle="None"
>
</Window>
Overlay.xaml.cs
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Interop;
namespace WindowControlTest
{
public partial class Overlay : Window
{
IntPtr m_ParentHwnd;
HwndSource m_HwndSource;
public Overlay(IntPtr parentHwnd)
{
InitializeComponent();
m_ParentHwnd = parentHwnd;
}
private void Window_MouseMove(object sender, MouseEventArgs e)
{
Console.WriteLine("Overlay.Window_MouseMove: " + e.GetPosition(this));
}
private void Window_GotFocus(object sender, RoutedEventArgs e)
{
Console.WriteLine("Overlay.Window_GotFocus");
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
HwndSourceParameters parameters = new HwndSourceParameters();
parameters.WindowStyle = (int) (WindowStyles.WS_VISIBLE | WindowStyles.WS_CHILD);
parameters.SetPosition(0, 0);
parameters.UsesPerPixelOpacity = true;
parameters.SetSize((int)Width, (int)Height);
parameters.ParentWindow = m_ParentHwnd;
m_HwndSource = new HwndSource(parameters);
m_HwndSource.CompositionTarget.BackgroundColor = Colors.Aqua;
m_HwndSource.RootVisual = (Visual)Content;
}
}
}
MainWindow.xaml
<Window x:Class="WindowControlTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
Loaded="OnLoaded"
>
<StackPanel>
<Label x:Name="stateLabel">Label</Label>
</StackPanel>
</Window>
MainWindow.xaml.cs - 查找并处理记事本并创建 Overlay
using System;
using System.Text;
using System.Windows;
namespace WindowControlTest
{
public partial class MainWindow : Window
{
private IntPtr m_TargetHwnd;
private Overlay m_Overlay;
public MainWindow()
{
InitializeComponent();
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
processWindows();
if (m_TargetHwnd != IntPtr.Zero)
{
m_Overlay = new Overlay(m_TargetHwnd);
m_Overlay.Show();
}
}
private void processWindows()
{
Win32.EnumWindows(delegate(IntPtr wnd, IntPtr param)
{
String text = GetWindowText(wnd);
Console.WriteLine("Window: " + text);
if (text.Contains("Notepad"))
{
m_TargetHwnd = wnd;
}
return true;
}, IntPtr.Zero);
}
public static string GetWindowText(IntPtr hWnd)
{
int size = Win32.GetWindowTextLength(hWnd);
if (size++ > 0)
{
var builder = new StringBuilder(size);
Win32.GetWindowText(hWnd, builder, builder.Capacity);
return builder.ToString();
}
return String.Empty;
}
}
}
(注意:许多 SO 问题解决了类似但不同的问题,例如 How to set Win32 window as owner of WPF window? assumes I am in control of the source code for the Win32 window, as do the examples 我可以在 MSDN 上找到。)