C#winform making profiled Forms/controls

Source: Internet
Author: User
Tags image line transparent color

The idea of making an irregular form or control is generally a way to create a region and then set it to the specified window or control. There are many ways to generate region, the most common is to create from a picture, the picture of the transparent part of the "key" off, the rest of the part as a region. Setting the region of a window or control can be done with the SetWindowRgn API, although the. NET framework encapsulates this operation, as long as you assign a value to the region property of a window or control in C #. Below I will be in C # to implement the core code of the form to show you to see, what is the opinion despite mentioning, do not hesitate oh j
First, there is a way to generate region based on the bitmap object:
<summary>
Gets an area of the non-transparent color portion of the picture.
</summary>
<param name= "Picture" > Take pictures of its area. </param>
<param name= "TransparentColor" > Transparent color. </param>
<returns> image areas of non-transparent colors </returns>
Private region Bmprgn (Bitmap picture, Color TransparentColor)
{
int nwidth = Picture.width;
int nheight = Picture.height;
Region RGN = new region ();
Rgn. Makeempty ();
BOOL istransrgn;//Whether the previous point is in the transparent area
Color curcolor;//The colors of the current point
Rectangle currect = new Rectangle ();
Currect.height = 1;
int x = 0, y = 0;
Scan this image by pixel to find the areas of non-transparent color and merge 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 Active Zone
{
Currect.width = X-currect.x;
Rgn. Union (currect);
}
}
else//non-transparent color
{
if (Istransrgn = = true)//Enter the active zone
{
Currect.x = X;
Currect.y = Y;
}
}//if Curcolor
Istransrgn = Curcolor = = TransparentColor;
}//for x
}//for y
return RGN;
}

The principle is very simple, is to scan the image line by row, in each row of those non-transparent color of the rectangle (only one like Sugao) merge (union) into a region object, when the entire picture is scanned, the resulting is what we want the region. This algorithm is described in many articles.

With region, the following is simple:
This. Region = Bmprgn (New Bitmap ("D:\\a.bmp"), Color.FromArgb (0, 0, 0));
The above code is the outline of the D:\a.bmp as the main window of the region, assuming that the picture's background black (Color.FromArgb (0, 0, 0)).
In fact, not only the form, any control can use this method to set region, make the special-shaped control.

C#winform making profiled Forms/controls

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.