Create a fully transparent WPF window to capture mouse events

前端 未结 6 2118
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-29 08:30

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

相关标签:
6条回答
  • 2020-12-29 09:08

    In Visual Studio 2010: Select your window in your design view.

    Set the properties of your window to:

    • AllowsTransparency : check it
    • Background : Transparent
    • WindowStyle : None
    0 讨论(0)
  • 2020-12-29 09:13

    For example i think your control name is MyGrid and you want it be Transparent and always get MouseOverEvent.....

    If (window AllowsTransparency is True and the window Background is Transparent) Then

    use a color like #01777777 for MyGrid Background Or 0.01 for MyGrid Opacity.

    Else

    use something like #00777777 for MyGrid Background Or 0.00 for MyGrid Opacity.

    0 讨论(0)
  • 2020-12-29 09:15

    You might find it simpler to use Mouse.Capture.

    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.

    0 讨论(0)
  • 2020-12-29 09:22

    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.

    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.

    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.

    0 讨论(0)
  • 2020-12-29 09:23

    Just set Background=Brushes.Transparent instead of Background=null.

    You don't need to use opacity at all (ie. just leave it at 100% opacity).

    0 讨论(0)
  • 2020-12-29 09:25

    Setting the opacity to 100% (or any non-zero value), and the background to Transparent (instead of null) should make most controls hittable.

    Make sure to set IsHitTestVisible to true. Not all controls can be hit, even if the opacity is 100% and the background is transparent.

    0 讨论(0)
提交回复
热议问题