The QQ form is rounded, The Kugoo form is also rounded, And the QQ Input Method skin form is irregular... The main form of the program used by N is not in every square. In fact, it is drawn by calling the 2D painting function according to the specified path, in this lesson, I will explain to entry-level scholars about how C # implements this function.
Let's take a look at the implementation effect (the font on the left is also part of the form ):
In fact, its core is implemented around Drawing2D. The GraphicsPath method GetPixel is used to take the color of a dot in the upper left corner as our transparent color, and then traverse each pixel coordinate point cyclically. If it is found to be transparent, this parameter is not added to the drawing form area.
See the figure below for how it is implemented:
Key points:
1. Set the current form to borderless (this. FormBorderStyle = System. Windows. Forms. FormBorderStyle. None ;);
Second, exclude the call of capture to the error thread. checkforillegalcrossthreadcils = false;
Third, calculate the boundary of the opaque part of the bitmap. The Code is as follows:
# Region // calculate the border of the opaque part in the bitmap
Private GraphicsPath CalculateControlGraphicsPath (Bitmap bitmap)
{
// Create GraphicsPath
GraphicsPath graphicsPath = new GraphicsPath ();
// Use the color at the top left corner as our transparent color
Color colorTransparent = bitmap. GetPixel (0, 0 );
// Offset all rows (Y direction)
For (int row = 0; row <bitmap. Height; row ++)
{
// X of the first vertex to be found
Int colOpaquePixel = 0;
// Returns a partial calendar of all columns (X direction)
For (int col = 0; col <bitmap. Width; col ++)
{
// If it is a point that does not require transparent processing, mark it and continue the calendar.
If (bitmap. GetPixel (col, row )! = ColorTransparent)
{
// Record the current
ColOpaquePixel = col;
/// Start from the opacity point found, continue to look for the opacity point until it finds or reaches the Image Width
While (col <bitmap. Width)
If (bitmap. GetPixel (col ++, row) = colorTransparent)
Break;
// Add the opacity point to graphicspath
GraphicsPath. AddRectangle (new Rectangle (colOpaquePixel, row, col-colOpaquePixel-1, 1 ));
}
}
}
Return graphicsPath;
}
# Endregion
Fourth, set the drawing path area of the current form
This. Region = new Region (this. CalculateControlGraphicsPath (this. BackgroundImage as Bitmap ));
Code http://www.bkjia.com/uploadfile/2011/1106/20111106044816864.rar
Author: Ai Wei from the blog