2015-06-16 65 views
1

我想附加一個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一樣。)

回答

5

我認爲你走錯了路,而不是讓你的覆蓋兒童記事本窗口,您應該嘗試通過HwndHost將主窗口的孩子記錄下來,然後在宿主上方維護疊加層。

這是我的博客post介紹了一個library我寫了一個WPF裝飾器在由HwndHost託管的任何hwnd上。一個簡單的網頁瀏覽器的演示看起來是這樣的:

snapshot http://i58.tinypic.com/kej3bd.png

+0

這就是我們到底下去的路徑。這並不完全令人滿意,因爲使您的子窗口看起來像原始的免費窗口並不總是微不足道的。但是,當保持WPF作爲父項時,事情開始變得更好。 –

+0

另外 - 不錯的博客文章! –

+0

不錯的博客文章,不錯的演示項目。很抱歉,我們無法使用您的代碼,因爲它受版權保護(無許可證):/ – Kryptos

相關問題