2015-12-29 34 views
2

首先,我想定义我的意思是透明点击在下面的文字:这意味着点击通过我的窗口没有任何处理,直接到无论哪个窗户都在下面。我需要这种行为,因为这个应用程序是为了覆盖游戏。制作一个WPF窗口点击,但不是它的控件

我搜索了有关使窗口对点击透明的问题。我想知道是否有一种方法可以使窗口本身对点击透明,而不是其控件(如文本框和按钮)。

下面是窗口标记:

<Window x:Class="QuestBrowser.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="Quest Browser" 
     Height="350" Width="525" 
     AllowsTransparency="True" WindowStyle="None" Topmost="True"> 
    <Window.Background> 
     <SolidColorBrush Color="White" Opacity="0.1"/> 
    </Window.Background> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition/> 
      <RowDefinition/> 
     </Grid.RowDefinitions> 
     <TextBox Name="inputTxtBox"></TextBox> 
    </Grid> 
</Window> 

而且波纹管,使窗口透明的点击(其它问题几乎复制粘贴代码,因为我不擅长在所有低级别的Windows编程)。

namespace Win32Utils 
{ 
    public static class WindowHelper 
    { 
     const int WS_EX_TRANSPARENT = 0x00000020; 
     const int GWL_EXSTYLE = (-20); 

     public static void SetWindowExTransparent(IntPtr hwnd) 
     { 
      var extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE); 
      SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_TRANSPARENT); 
     } 
    } 
} 

方法WindowHelper.SetWindowExTransparent从我的方法Window.OnSourceInitialized(按照指示在帖子里我从一个透明窗口中的代码)的覆盖范围内称为:

protected override void OnSourceInitialized(EventArgs e) 
{ 
    base.OnSourceInitialized(e); 
    var hwnd = new WindowInteropHelper(this).Handle; 
    WindowHelper.SetWindowExTransparent(hwnd); 
} 

这确实使窗口对点击透明,但我放入它的所有控件也是透明的(例如TextBox)。有没有办法使窗口表面透明点击,但确保TextBox仍然能够正常接收鼠标和键盘输入?

我想避免出于用户隐私原因的鼠标和键盘挂钩,因为,正如我所说,我不太了解Windows API。如果钩子是唯一的方法,我会非常感激他们如何工作的“傻瓜式”解释。

回答

3

你可以试试这个

<Window x:Class="WpfApplication1.Window1" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="Window1" 
     Height="300" 
     Width="300" 
     AllowsTransparency="True" 
     WindowStyle="None" > 
    <Window.Background> 
     <SolidColorBrush Color="#FFB0B0B0" 
         Opacity="0.05" /> 
    </Window.Background> 
    <Grid> 
     <Button Content="Button" 
       HorizontalAlignment="Left" 
       Margin="39,125,0,0" 
       VerticalAlignment="Top" 
       Width="75" /> 
     <Label Content="Label" 
       HorizontalAlignment="Left" 
       Margin="114,50,0,0" 
       VerticalAlignment="Top" /> 
     <TextBox HorizontalAlignment="Left" 
       Height="23" 
       Margin="101,201,0,0" 
       TextWrapping="Wrap" 
       Text="TextBox" 
       VerticalAlignment="Top" 
       Width="120" /> 

    </Grid> 
</Window> 

它通常做的是建立一个透明的窗口。(点击进入和Invisble)。但是控件是可见的,而不是通过点击。 或者您可以尝试How to create a semi transparent window in WPF that allows mouse events to pass through

+0

非常感谢!我所需要做的只是让窗户完全透明,而不像我所做的那样只是部分透明。我不会想象窗口背景的颜色定义了它的行为。 – FinnTheHuman

+0

如果它满足您的疑问,请标记它的答案 – AEonAX

+0

我会尽快完成所有测试。再次感谢。 – FinnTheHuman

相关问题