Creating Non-Rectangular custom Shaped Forms in C#

Custom shaped Windows Forms in any schedule are most commonly used for splash screens. Any way they are not itsybitsy to splash screens only, music players are a good example of that. For anything use, the .Net Framework gives us two easy ways to accomplish this effect, one using the Form property TransparencyKey and the other using the property Region.

Creating Non-Rectangular custom Shaped Forms in C#

Both methods require need a pre-created background image. You can select to originate the image programmatically, but the whole point of this is to originate remarkable graphics which is easier to do with a good image editing program. Make sure your image has a contrasting background color. Remember, this is the color that will be filtered out, don't make it similar to the rest of the image's colors.

For the transparency method, simply:

1) Set the Form BackgroundImage to the image you created

2) Set the FormBorderStyle to None

3) Set the Form Width and Height to the BackgroundImage Width and Height

4) Set the Form TransparencyKey to Color.Fromargb(0,0,0) but replace 0,0,0 with your background color.

For the region method:

1) Set the Form BackgroundImage to the image you created

2) originate a new System.Drawing.Drawing2D.GraphicsPath object.

3) Go through each pixel of the BackgroundImage, for any pixel that does not match the background color (Color.Fromargb(#, #, #)), add a new rectangle to the GraphicsPath object. The rectangle will be created with new Rectangle(x, y, 1, 1), x and y being the coordinates of the given pixel.

4) Set the Form Region to new Region(GraphicsPath object here)

Creating Non-Rectangular custom Shaped Forms in C#

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.