Fix transparency issues for static text and check box under Windows
|Reported by:||wxBen||Owned by:|
|Keywords:||transparent checkbox static text||Cc:|
Under Microsoft Windows, wxStaticText and wxCheckBox are typically only transparent in so much as their backgrounds are painted with the background colour of a parent. This is nice and usually works well, but if you place one over say a ribbon bar which has a background gradient, then you need true transparency.
If you Google this, you will see other people struggling with this as well. The wxWidgets forums contains code for TransparentStaticText class. Also, there is a Python tutorial about writing your own check box class in order to have a transparent one with numerous references to it. See here for example: http://social.msdn.microsoft.com/Forums/eu/vcgeneral/thread/a0db67c8-3b51-4451-a636-a2737e6b3e27 and see the screenshot.
Why does this not work out of the box? I do not want to add all sorts of extra classes and things to my application. I have been stepping through the code and trying things like:
But to no avail. The core issues seem to be this:
- True transparency requires an extended Windows style of WS_EX_TRANSPARENT. Without this, it will not work.
- A window with this style should not paint the background in response to WM_ERASEBKGND and indicate that it has handled the message.
- For a child control the parent should, in response to messages like WM_CTLCOLORSTATIC, set the background mode to transparent and return a handle to the HOLLOW_BRUSH. See here for an example:
So my code changes are this:
- Fixed the wxControl class to detect the WS_EX_TRANSPARENT style and do the proper thing in general in response to messages like WM_CTLCOLORSTATIC. This makes transparent static texts work.
- Fixed wxCheckBox which was always passing in zero for the extended style and ignoring any wxTRANSPARENT_WINDOW passed in. You absolutely need it here and other controls pass it on to the window creation code. Why is the checkbox different? In the case of the checkbox, also switch to owner draw, as it does not always work otherwise.
- Added some comments to the documentation to help people down the line.
I also update the ribbon bar sample case and added a static text and checkbox onto it. Note how the ribbon background comes through, but if you remove WS_EX_TRANSPARENT or any of the code changes, the backgrounds revert to white.
These changes only come into play when a non default wxTRANSPARENT_WINDOW is passed when creating these controls. This style is also not demonstrated in any of the samples or used much in the code, so the impact of the changes should be low. The changes are also consistent with recommendations from the MSDN forums about how this is supposed to be done. Also, our application has a lot of native transparent controls, so I have some experience with this.
While I did not test this for radio buttons and such, it may work for them as well, and if needed only a minor code tweak should be necessary to get those to work as well.
Change History (24)
Changed 21 months ago by wxBen
comment:22 Changed 11 months ago by vadz
- Resolution set to wontfix
- Status changed from new to closed