different levels of opacity in a Form

L

Lee Gillie

Scott said:
I'm trying to create a form with different levels of opacity. Is this
possible?

I checked my .NET Framework Class Reference documentation for the Forms
class, and found out there are two levels of opacity possible with the
Visible property. Do you need more?
 
S

Scott W

I'm not sure how the visible property relates opacity, but I need at least
three levels of opacity

1) The background is to be transparent
2) most of the painted graphics I would like to have at 50% opacity
3) the remaining painted graphics I would like to have at 100% opacity

how can I do this?
 
M

MarkR

You can create a completely transparent section of a form (e.g. to make a
circular form) using the Region property. You can also set the
Form.BackgroundColor to Color.Transparent if you want. (see also
ControlStyles.SupportsTransparentBackColor).

You can then paint the graphics using alpha-blending, by either creating a
color to paint with (Color c = Color.FromArgb(128, Color.Blue); ///
creates a 50% opaque blue) or creating a ImageMatrix with element[3][3] set
to your desired opacity and calling DrawImage using a ColorAttributes
structure using that ImageMatrix, kinda like this:

ColorMatrix cm = new ColorMatrix();
cm.Matrix00 = cm.Matrix11 = cm.Matrix22 = cm.Matrix44 = 1;
cm.Matrix33 = AlphaBlendAmount;
// Create an ImageAttributes object and set its color matrix.
ImageAttributes imageAtt = new ImageAttributes();
imageAtt.SetColorMatrix(
cm,
ColorMatrixFlag.Default,
ColorAdjustType.Bitmap);
...
DrawImage(..., imageAtt);
 
P

Phil Wright

It sounds like you want per-pixel alpha blending and this is not provided by
the framework in the standard Form. At a Win32 level it is achieved by
adding the WS_EX_LAYERED style to a top-level window and then using the
appropriate calls to provide the OS with a bitmap that can have different
alpha levels on a per-pixel basis.

The .NET Form only provides the reduced support of having a single color
remapped to be transparent. But you could achieve the same effect by having
two top level Form's and placing them on top of each other.

Phil Wright
Follow my C# component microISV startup at...
http://componentfactory.blogspot.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top