Use C # To develop a program with irregular forms similar to the QQ input method + code packaging and sharing

Source: Internet
Author: User

 

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.