views:

1205

answers:

3

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?

+1  A: 

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).

Ray Burns
I tried this out, thinking the same possibility, but it doesn't work. When the Background is transparent, clicks are not captured by the program, instead grabbing whatever program is underneath.
Will Eddins
Can you show an example? I use this approach (generally #00000000 because it's so easy to type) and it seems to work just fine for me.
Egor
+4  A: 

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.

Will Eddins
Yeah, I couldn't find a workaround either. It makes sense.
James Cadd
See my edit for a different solution. You can change the background to a near-transparent color, and still keep your window at full opacity incase you want to overlay some controls on the screen.
Will Eddins
The second solution does work but it looks the same as setting the Opacity to 0.01.
James Cadd
Are you able to see it? On my screen, it looked completely transparent, I couldn't tell the difference. It should look the same for the most part, the only difference is you can overlay non-transparent things. Since I'm not sure what you're using this for, not sure what the best solution was.
Will Eddins
Actually, even a fully transparent color value works - I use #00000000 for this purpose. It works because Opacity is not 0, and the brush is a solidcolorbrush - the alpha value doesn't seem to be cosidered. Brushes.Transparent also works because it's simply a #00FFFFFF solidcolorbrush
Egor
@Egor: When I was trying this, transparent brushes caused the Window to be click-through. This was on .NET 3.5 SP1... so maybe a different version?
Will Eddins
A: 

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.

Cameron MacFarland
Maybe I'm doing something wrong, but when I do a Show on a window with these settings it doesn't get displayed at all (i.e. the window's not present when you alt+tab).
James Cadd