views:

993

answers:

7

I tried setting the opacity of my form to 50%, and then drawing a string on it. It seems that the string I draw on it also has an opacity of 50%. How would I draw a non transparent string , but let the form background show through 50%?

Would also be willing to do this in WPF if it is possible, but I would need explicit instructions or an example project as I have never done it before.

To clarify, I want the form background to be a black 80% opaque to what is underneath, and then I want to draw text, etc.. on it and have it appear 100% opaque.

+2  A: 

You can use the TransparencyKey.

http://msdn.microsoft.com/en-us/library/system.windows.forms.form.transparencykey.aspx

David Stratton
This isn't the same, what this says is that when you draw whatever color this key is set to, it will show as transparent, which means 0% Opaque (or fully transparent depending on if you are a cup half full guy). It doesn't allow you to set it to 50% transparent.
esac
OK. Thanks for the update. You're right, of course. The TransparencyKey will only work if you want to make it 100% tranparent. (I'm a cup half full guy myself.)
David Stratton
A: 

in the ControlStyles enumeration MSDN there is a value called SupportsTransparentBackColor. In your form contructor run this:

SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
SetStyle(ControlStyles.SupportsTransparentBackColor, true);

then you can set the background color to Color.Transparent.

Hope this helps.

Alastair Pitts
Hm, this sounds like what I want, however I am getting a gray box, and it seems that anything I draw on top if it is not showing. I tried Color.Transparent and this.BackColor = Color.FromArgb(180, 0, 0, 0);
esac
I am thinking this won't work , from MSDN: If true, the control accepts a BackColor with an alpha component of less than 255 to simulate transparency. Transparency will be simulated only if the UserPaint bit is set to true and the parent control is derived from Control.I am guessing that since the main form has no parent that it fails the last test.
esac
Ah, my apologies. I didn't notice that. I suspect your theory is correct.
Alastair Pitts
A: 

Use TransparencyKey to render the window as transparent. Create an empty panel with the background color of your and give it the desired opacity.

Then create all your children in a separate container/panel (itself a sibling to the translucent panel created above), and have it not render a background at all.

This should give you the effect of a translucent background but visible controls.

Richard Szalay
I just tried this, and I am getting an opaque background through the transparent container, and it happens to be the color I am setting the TransparencyKey to. For example, I set the TransparencyKey to pink, and then I put a 80% opaque black background panel on, and what I see is a pinkish/black opaque background.
esac
Try settings the TransparencyKey / form background to Transparent.
Richard Szalay
This goes back to the same issue below. Color.Transparent isn't supported unless I can set 'SupportsTransparentBackColor' which isn't allows unless the parent is a Control. For the Main form, there is no parent: SetStyle(ControlStyles.SupportsTransparentBackColor, true); this.TransparencyKey = Color.Transparent; this.BackColor = Color.Transparent;
esac
+8  A: 

This is very easily done in WPF:

  1. Set WindowStyle="None" on the Window (note: this is required, you cannot have Transparency and the standard windows chrome)
  2. Set AllowsTransparency="True" on the Window
  3. Set a Background on the Window using a brush with transparency, such as Background="#AAFFFFFF"

Here's a quick sample:

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
Height="300" Width="300" AllowsTransparency="True" Background="#AAFFFFFF" WindowStyle="None">
<Grid>
 <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="20" FontWeight="Bold">Hello World!</TextBlock>
</Grid>

Now obviously, since you've disabled the standard Windows chrome, you will need to supply your own button to close/minimize/maximize/drag the window. You can do this pretty easily yourself or you could look into purchasing something like Blendables which comes with a "Chromeless Window" control that you could use.

Drew Marsh
A: 

In winforms, MSDN says this:

The Opacity property enables you to specify a level of transparency for the form and its controls. When this property is set to a value less than 100 percent (1.00), the entire form, including borders, is made more transparent.

So, any child controls of the form that you set opacity on will have their opacity changed. If you want to achieve different levels of opacity on individual controls, you'll have to switch to WPF.

scottm
A: 

I assume that this is a WinForms project, though you said you’d be willing to try WPF.

Lemme switch to my lateral thinking cap: Any particular reason you couldn’t just fake it? As in, make a bitmap in PhotoShop Elements or Paint Shop Pro or some such that you will set as the entire background of the WinForm, and you simply draw the partially transparent area over which the controls will be placed right on that bitmap?

Joel_MMCC
A: 
Paul Sasik