我想附加一个WPF窗口作为外部应用程序的子窗口,如记事本提供一个覆盖。在研究了我可以在SO和MSDN上找到的所有答案后,当我的WPF应用程序运行时,我已经在记事本的角落创建了一个牢固的叠加层。然而,如何在外部Win32应用程序的窗口中添加WPF覆盖图?
- 只要记事本获得焦点,覆盖消失,
- 以及表示上记事本叠加,叠加也单独示出作为窗口
- 上记事本中覆盖不接收任何的MouseMove事件(但单独的窗口确实
这里是最小的例子来说明这个问题:
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?假设我在控制Win32窗口的源代码,就像我可以在MSDN上找到的examples一样。)
这就是我们到底下去的路径。这并不完全令人满意,因为使您的子窗口看起来像原始的免费窗口并不总是微不足道的。但是,当保持WPF作为父项时,事情开始变得更好。 –
另外 - 不错的博客文章! –
不错的博客文章,不错的演示项目。很抱歉,我们无法使用您的代码,因为它受版权保护(无许可证):/ – Kryptos