|
I have a control with a graphic. I put some labels over it, and set the background to transparent. This doesn't mean the graphic shows through, it means the background color shows through.
Do you know how to get a truly transparent background on a label? I've tried using graphics drawstring() but it doesn't look as smooth and it's much more of a pain... _thanks | | MigrationUser 1 Wednesday, January 29, 2003 6:27 PM | In Windows Forms there is no way to get true transparency. It is just not supported by Win32.
We do fake it though. For a Label control with BackColor set to Color.Transparent we take the background of the Label's parent container (typically the Form) and blit the corresponding area into the Label's background.
This gives the appearance of transparency but it wont paint any controls that are also overlapping the label, like a PictureBox in the container underneath the Label.
Make sense?
- mike | | MigrationUser 1 Thursday, January 30, 2003 3:10 AM | Not true that Win32 doesn't support it; its pretty simple to do it in VB or a normal dialog resource. Even in the windows forms library, you can override CreateParams and manually set the WS_EX_TRANSPARENT style and (sort of) get transparency. Two problems with that approach though; its dependent on the Win32 window styles which might not exist in other Winforms platforms, and the winforms paint code doesn't render everything in the right order. If you call all of the paint events yourself (which takes some trial and error to get right), it works adequitely for many apps.
| | MigrationUser 1 Wednesday, February 05, 2003 8:52 AM | I'm not familiar with the WS_EX_TRANSPARENT style, but the MSDN docs for it state:
"Specifies that a window created with this style is to be transparent. That is, any windows that are beneath the window are not obscured by the window. A window created with this style receives WM_PAINT messages only after all sibling windows beneath it have been updated"
This to me says that if you have multiple levels of control nesting, say a label on a Panel in a UserControl on a Form, then label still wont render as transparent.
Is there a way to get this to work? If so, I think it'd make a really cool sample to have a library of controls that render with this style bit set.
- mike | | MigrationUser 1 Thursday, February 06, 2003 4:28 AM | Thanks for your replies...
If you're interested in the resolution, I didn't bother with the labels anymore. I just created a bitmap object, loaded my background into it, then used graphics drawstring to write out the labels. We were just mocking it up for a client, after they figure out the business stuff I may have to come back to it...
What I thought was curious is that if you create a new bitmap, and try to drawstring in it the text looks all jacked, even if you've go anti-aliasing on. On the other hand if you load up a gif first, like what I did, then drawstring on top of that it looks normal... | | MigrationUser 1 Friday, February 07, 2003 2:04 PM | I hadn't looked at this problem in a while (I got around it in my project by simply not using UserControls, and just handling Paint events), but I just dug up my test program.
The basic code below sets up the transparency; note that I simply override OnPaintBackground as a simpler solution than setting a dozen styles. If you want alpha blended backgrounds, etc, you'd have to set the appropriate styles instead.
This code alone makes the usercontrol transparent, but very odd things happen both in the designer and at runtime with animations. I managed to work around some of the issues by using reflection to call the InvokePaint and InvokePaintBackground private methods on the parent and sibling controls; that code never worked quite right in general, so I didn't post it here. I may look into the problem some more in the future.
Thanks, Robert W. Grubbs
protected override CreateParams CreateParams { get { CreateParams cp = base.CreateParams; cp.ExStyle ^= 0x00000020; //WS_EX_TRANSPARENT return cp; } }
protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs pevent) { } | | MigrationUser 1 Monday, February 10, 2003 3:33 PM |
|