Control
The idea of making an irregular form or control is usually to find a way to generate a region, and then set it to the specified window or control. There are many ways to generate region, the most common is from a picture generation, the picture in the transparent color part of the "pull" off, the rest of the part as a region. The region of a window or control can be set with the SetWindowRgn API, but the. NET framework encapsulates this operation, in C # as long as you assign a value to a window or control's Region property. The following I will be in C # to implement the core code of the form of the key to show everyone to see what the opinion, despite mentioning, don't be polite oh j
First, it's a way to generate region from a Bitmap object:
///
Gets an area of the non-transparent color portion of a picture.
///
Take a picture of its area.
Transparent color.
An area of the non-transparent color part of a picture
Private Region Bmprgn (Bitmap picture, Color TransparentColor)
{
int nwidth = Picture.width;
int nheight = Picture.height;
Region rgn = new Region ();
Rgn. Makeempty ();
BOOL istransrgn;//before a point is in the transparent area
Color curcolor;//The current point
Rectangle currect = new Rectangle ();
Currect.height = 1;
int x = 0, y = 0;
Scan this image pixel by piece, find the opaque color parts and combine them.
for (y = 0; y < nheight; ++y)
{
Istransrgn = true;
for (x = 0; x < nwidth; ++x)
{
Curcolor = Picture.getpixel (x,y);
if (Curcolor = = TransparentColor | | | x = = nWidth-1)//If a transparent color or line end is encountered
{
if (Istransrgn = false)//exit valid area
{
Currect.width = X-currect.x;
Rgn. Union (currect);
}
}
else//non-transparent color
{
if (Istransrgn = = true)//Enter active area
{
Currect.x = X;
Currect.y = Y;
}
}//if Curcolor
Istransrgn = Curcolor = = TransparentColor;
}//for x
}//for y
return RGN;
}
The principle is very simple, that is, scan the image line by row, in each row of those opaque color of the rectangle (only one like ICAO) merge (union) into a region object, when scanning the entire picture, we get the desired region. This algorithm is introduced in many articles.
With the region, the following is simple:
This. Region = Bmprgn (New Bitmap ("D:\\a.bmp"), Color.FromArgb (0, 0, 0));
The code above is to take the contour of the d:\a.bmp as the region of the main window, assuming the background black (Color.FromArgb (0, 0, 0) of the picture.
In fact, not only form, any control can use this method to set region, to create a special-shaped control.