KevB
KevB

Reputation: 3690

Changing form opactity without changing opacity of contained controls

Is it is possible, with Windows Forms in .NET, to change the opacity of a form without it automatically changing the opacity of the controls within the form?

I have a form that is running maximized, that contains a flowlayoutpanel in the centre of the form with controls inside it. I would like to lower the opacity of the form so that the "spare" part around the flowlayoutpanel is partly transparent, but the flowlayoutpanel itself remains solid (im aiming for a lightbox style effect).

Upvotes: 4

Views: 4160

Answers (3)

CharithJ
CharithJ

Reputation: 47530

Form.TransparencyKey Property

When the TransparencyKey property is assigned a Color, the areas of the form that have the same BackColor will be displayed transparently.

If the color assigned to the TransparencyKey property is the same as any controls on the form, they also will be displayed transparently. For example, if you have a Button control on a form that has its TransparencyKey property set to SystemColors.Control, the control will be displayed transparently unless the BackColor property of the Button control is changed to a different color.

Upvotes: 1

Jeff Yates
Jeff Yates

Reputation: 62377

Unfortunately, Opacity is a property of Form, not Control. The only way I can imagine this would be possible is if you custom drew the form yourself and used an alpha-component in the brush used to paint the form background.

Even then, I don't think it would work as the desktop composition manager (especially relevant on Windows Vista) needs to know how your form is supposed to be composed with the rest of the desktop, and I think the brush would only be transparent against the form background, rather than whatever is underneath it.

Upvotes: 0

Dave Arkell
Dave Arkell

Reputation: 3980

Of course, this is possible in WPF by setting the background of the form to transparent (as opposed to setting opacity to 0, which basically sets transparency on both foreground and background).

If you really wanted to, I don't see why you couldn't embed a winforms component into a transparent wpf form, using WindowsFormsHost.

Upvotes: 2

Related Questions