创建一个完全透明的 WPF 窗口来捕获鼠标事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1646346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create a fully transparent WPF window to capture mouse events
提问by James Cadd
I'm trying to trap mouse events in WPF by using a topmost, transparent non-modal window. I'm finding that this works fine if the opacity of the window is 0.01 or greater and it has a background color, but when the opacity is set to 0 it no longer receives mouse messages. Is there a way make this window look fully transparent and still get mouse input?
我正在尝试使用最顶层的透明非模态窗口在 WPF 中捕获鼠标事件。我发现如果窗口的不透明度为 0.01 或更大并且它具有背景颜色,这可以正常工作,但是当不透明度设置为 0 时,它不再接收鼠标消息。有没有办法让这个窗口看起来完全透明并且仍然获得鼠标输入?
回答by Will Eddins
As far as I know, no.
据我所知,没有。
When a Control
or Window
is fully transparent, it can then be clicked through. This is the case if you set your Window.Background="Transparent"
, or Opacity="0"
. As far as I know, this is by design in WPF.
当Control
或Window
完全透明时,可以单击它。如果您将Window.Background="Transparent"
, 或Opacity="0"
. 据我所知,这是 WPF 中的设计。
When using an opacity of 0.01, you should barely see the window, if at all. This is likely your best bet at achieving the functionality.
当使用 0.01 的不透明度时,您应该几乎看不到窗口,如果有的话。这可能是实现功能的最佳选择。
Edit:Another solution, which I tried and does work, is to set the background color to an almost-transparent color. I used Background="#01000000"
, thus giving an alpha value of 1. This makes your window background transparent-looking, but allows you to place controls on it with the window at full opacity.
编辑:我尝试过并且确实有效的另一个解决方案是将背景颜色设置为几乎透明的颜色。我使用了Background="#01000000"
,因此 alpha 值为 1。这使您的窗口背景看起来透明,但允许您在窗口完全不透明的情况下在其上放置控件。
回答by Sujith
In Visual Studio 2010: Select your window in your design view.
在 Visual Studio 2010 中:在设计视图中选择您的窗口。
Set the properties of your window to:
将窗口的属性设置为:
- AllowsTransparency : check it
- Background : Transparent
- WindowStyle : None
- AllowsTransparency :检查它
- 背景:透明
- WindowStyle : 无
回答by Ray Burns
Just set Background=Brushes.Transparent instead of Background=null.
只需设置 Background=Brushes.Transparent 而不是 Background=null。
You don't need to use opacity at all (ie. just leave it at 100% opacity).
您根本不需要使用不透明度(即,将其保留为 100% 不透明度)。
回答by RAM
For example i think your control name is MyGrid
and you want it be Transparent
and always get MouseOverEvent
.....
例如,我认为您的控件名称是MyGrid
并且您希望它是Transparent
并且总是得到MouseOverEvent
.....
If(window AllowsTransparency
is True
andthe window Background
is Transparent
) Then
If(window AllowsTransparency
is True
andthe window Background
is Transparent
) Then
usea color like
#01777777
forMyGrid
Background
Or0.01
forMyGrid
Opacity
.
使用类似
#01777777
forMyGrid
Background
Or0.01
for的颜色MyGrid
Opacity
。
Else
别的
usesomething like
#00777777
forMyGrid
Background
Or0.00
forMyGrid
Opacity
.
使用类似
#00777777
forMyGrid
Background
Or0.00
for 的东西MyGrid
Opacity
。
回答by Drew Noakes
You might find it simpler to use Mouse.Capture
.
您可能会发现使用更简单Mouse.Capture
。
https://msdn.microsoft.com/en-us/library/ms771301.aspx
https://msdn.microsoft.com/en-us/library/ms771301.aspx
When an object captures the mouse, all mouse related events are treated as if the object with mouse capture perform the event, even if the mouse pointer is over another object.
当一个对象捕获鼠标时,所有与鼠标相关的事件都被视为具有鼠标捕获的对象执行该事件,即使鼠标指针在另一个对象上也是如此。
回答by Cameron MacFarland
Setting the opacity to 100% (or any non-zero value), and the background to Transparent (instead of null) should make most controls hittable.
将不透明度设置为 100%(或任何非零值),并将背景设置为透明(而不是 null)应该使大多数控件都可以点击。
Make sure to set IsHitTestVisibleto true. Not all controls can be hit, even if the opacity is 100% and the background is transparent.
确保将IsHitTestVisible设置为 true。并非所有控件都可以点击,即使不透明度为 100% 并且背景是透明的。