C#winform Form/Control in the realization of special shape

Source: Internet
Author: User
Tags image line transparent color
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.



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.