I'm about to 开发者_JS百科lose my mind here.
Why won't the checkbox control blend with what's behind it? The question applies to all WinForms controls, but I'm using this as an example.A picture is worth a thousand words:
And a few more words:
What's behind theCheckBox
are colored PictureBox
es and a Button
.
The CheckBox
's BackColor
is set to Transparent
. But somehow it decides that that means it should share the BackColor
of the containing Form
(is that its idea of the illusion of transparency?).
Is this not possible in WinForms? I could swear I did this before.
UPDATE:
I just tried this: On that form, set theCheckBox
's BackColor
to Transparent
, then change the BackColor
of the containing Form
to some other color, and the CheckBox
will match that BackColor
. What the.......?This is a side effect of controls being Windows windows. A window is responsible for drawing itself, the OnPaintBackground and OnPaint methods take care of that.
This rendering model doesn't support transparency well. There is support for true transparency by using layered windows. That's implemented by the video adapter, Windows uses it hardware overlay feature. But that only works for toplevel windows, not child windows. Note the Form.Opacity and Form.TransparencyKey properties.
There's partial support for transparency through a trick. A control can fake it by asking the parent window to draw itself first inside the control window. That produces the background pixels, it can then draw on top of that. Setting the BackColor property to Color.Transparent enables this trick for controls that support this. All of the ButtonBase derived classes do. But not controls that are wrappers for native Windows controls.
"Asking the parent window" is where the flaw in this trick becomes visible in your screen shot. You are seeing the form pixels. Stacking effects don't work, it never considers any intermediary window in the Z-order, only the parent. This is fixable but very ugly, there's a KB article that show the code.
Also notable is that WPF doesn't have this restriction. Controls are not windows, they render by painting themselves on top of the parent. Layers of paint. Transparency is now trivial, just don't paint.
Bob Powell has written an excellent article about transparent controls. Check it out:
https://web.archive.org/web/20141227200000/http://bobpowell.net/transcontrols.aspx
Can you set the backcolor of the checkbox manually to the color you want? (The value in the picturebox behind it)
'Transparent' may mean something different from what you want to MS.
Also, try changing the zorder of the pictureboxes (bring to front) and see if that changes the checkbox's underlying color.
精彩评论