WS_EX_TRANSPARENT - What does it actually do?
Asked Answered
C

2

4

In my project, I create a form with the opacity controlled by the user. If the form was fully transparent the mouse events 'fell through' (without my intervention), otherwise my form handled them.

After reading this question and overriding the CreateParams property to set the WS_EX_TRANSPARENT flag on my form, it now allows mouse events to fall through when the opacity is any value <255.

This is exactly what I want, but it concerns me that I don't understand why it works.

From what I've read, WS_EX_TRANSPARENT is meant to make the form appear transparent by 'stealing bits' from the form below it in its Paint method, therefore:

  1. The Paint method of my form and all the controls in it should never be called, right? As WS_EX_TRANSPARENT should cause Windows to override them, so why does it affect input handling but not the drawing of my form?

  2. The opacity should have no impact on the handling of mouse events, as if Paint is being overridden the 'local' opacity should not matter, no?

Could someone explain, what this flag really does? How does it work?

Confined answered 29/5, 2011 at 0:4 Comment(0)
M
9

WS_EX_TRANSPARENT makes your events (like mouse clicks) fall through your window, amongst other things. Opacity is a separate concept, it instructs window manager to apply alphablending when drawing your form. Those two things are not related, but when you combine them you get the effect you need in your case.

So:

  1. Paint method of your form is called normally as it should, opacity has nothing to do with it.

  2. Opacity does not have anything to do with mouse events, as described in the first paragraph.

Manyplies answered 29/5, 2011 at 0:22 Comment(3)
I understand, but how can it be explained that the my opacity level defines whether the mouse falls through or not? (It is definately the opacity and the flag working together - if I remove the flag the window must have the opacity set to 0 before it will ignore mouse events)Confined
MSDN is sketchy on the subject, see this article for further experimentation: codeproject.com/KB/vb/ClickThroughWindows.aspx.Manyplies
Thanks, documentation really seems nonexistent! I'll just be glad that I can make it do what I want make sure I keep a good eye on its behaviour during testing ;)Confined
T
0

It makes the window invisible to mouse events, or -as Microsoft puts it- it doesn't obscure the windows below. I believe it doesn't actually steal pixels from the windows below, but Windows itself will probably blend those two pictures together, using the level of transparancy you supplied.

Transparent windows can be useful for showing some progress or a splash screen, but you'll have to program a way to close them, because just click the X won't work since the mouse click will pass through it.

So not only does it change the level of visual transparency, but it modifies the behaviour too. I wonder where you would have read otherwise.

[edit]

Don't Windows in C# just have an opacity property?

Toddler answered 29/5, 2011 at 0:17 Comment(3)
"Don't Windows in C# just have an opacity property?" - WPF does, WinForms does not.Weisburgh
Wrong! WinForms do indeed have an opacity property: msdn.microsoft.com/en-us/library/…Loya
this site [bobpowell.net/transcontrols.htm] mentioned taking pixels from the control underneath the one flagged as transparent. (First paragraph, or maybe I misunderstood?) If I understand you right, you're saying that the opacity property 'overrides' the original behaviour of the flag with regards to blending, but is there any documentation that binds the behaviour of the mouse to the opacity, or do you think this may just be some if statement hidden in Windows for that flag that goes 'alpha < 1' - written with the expectation that it would be 1 or 0?Confined

© 2022 - 2024 — McMap. All rights reserved.