单击WPF窗口,但不单击其控件 [英] Making a WPF window click-through, but not its controls
问题描述
首先,我想在下面的文字中定义对点击透明的意思:这意味着点击经过我的窗口而没有任何处理,直接到达以下哪个窗口.我需要这种行为,因为此应用程序本应是游戏的叠加层.
First of all I want to define what I mean by transparent to clicks in the text bellow: It means the clicks go through my window without any processing, directly to whichever window is bellow it. I need this behaviour because this application is meant to be an overlay over a game.
我搜索了有关使窗口对点击透明的问题.我想知道是否有一种方法可以使窗口本身对点击透明,但对控件(例如文本框和按钮)透明.
I searched the questions relating to making a window transparent to clicks. I was wondering if there was a way to make the window itself to be transparent to clicks, but not its controls (such as text boxes and buttons).
这是窗口的标记:
<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底层编程).
And bellow is the code that makes the window transparent to clicks (pretty much copy-pasted from other question, because I'm not good at all at low level Windows programming).
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);
}
}
}
从我对Window.OnSourceInitialized方法的覆盖内调用WindowHelper.SetWindowExTransparent方法(如我从中获得透明窗口代码的帖子中所述):
The method WindowHelper.SetWindowExTransparent is called from within my override of the method Window.OnSourceInitialized (as instructed in the post where I got the code for a transparent window from):
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
var hwnd = new WindowInteropHelper(this).Handle;
WindowHelper.SetWindowExTransparent(hwnd);
}
这确实使窗口对单击透明,但是我放入其中的所有控件也都是透明的(例如,TextBox).有没有一种方法可以使窗口表面对单击透明,但要确保TextBox仍能正常接收鼠标和键盘输入?
This does make the window transparent to clicks, but all controls I put into it are transparent as well (for example, the TextBox). Is there a way to make the Window surface transparent to clicks, but ensure the TextBox still receives mouse and keyboard input normally?
出于用户隐私的原因,并且因为如我所说,我对Windows API的了解不多,所以我想避免使用鼠标和键盘的钩子.如果挂钩是唯一的方法,那么我将非常感谢"For Dummies"式的解释,说明它们如何工作.
I wanted to avoid mouse and keyboard hooks for both user privacy reasons and because, as I said, I don't understand much about the Windows API. If hooks are the only way, I would really appreciate a "For Dummies"-style explanation on how they work.
推荐答案
可以尝试一下
<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>
它的主要作用是创建一个透明窗口.(单击并隐藏).但是控件是可见的,不能单击.或者您可以尝试
What It basically does Is create a transparent window.(Click through and Invisble). But controls are visible and not-click through. Or you can try How to create a semi transparent window in WPF that allows mouse events to pass through
这篇关于单击WPF窗口,但不单击其控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!