C # create a special form/Control Using winform

Source: Internet
Author: User
The idea of creating a special form or control is generally to generate a region and then set it to the specified window or control. There are many ways to generate region. The most common method is to generate a region from an image. The transparent part of the image is "hidden", and the rest is used as a region. You can use setmediawrgn API to set the region of a window or control. However, the. NET Framework encapsulates this operation. in C #, you only need to assign values to the region attribute of the window or control. Next I will implement the core of the special form in C #. Code Post it for you to see if you have any comments. Despite mentioning it, we are welcome. J is a method for generating region based on Bitmap objects: /// <summary> /// obtain the area of the non-transparent part of the image. /// </Summary> /// <Param name = "picture"> obtain the image of the region. </Param> /// <Param name = "transparentcolor"> transparent color. </Param> // <returns> private region BMP RGn (Bitmap Picture, color transparentcolor) {int nwidth = picture. width; int nheight = picture. height; region RGN = new region (); RGN. makeempty (); bool istransrgn; // whether the previous vertex is in the transparent area color curcolor; // The current vertex color rectangle currect = new rectangle (); currect. height = 1; int x = 0, y = 0; // scan the image pixel by pixel to locate and merge the areas with non-transparent colors. 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 there is a transparent color or line tail {If (istransrgn = false) // exit the valid area {currect. width = x-currect. x; RGN. union (currect) ;}} else // non-transparent color {If (istransrgn = true) // enter the valid area {currect. X = x; currect. y = y ;}// if curcolor istransrgn = curcolor = transparentcol Or;} // for x} // For y return RGN;} the principle is very simple, that is, to scan the image row by row, merge the non-transparent rectangles (only one pixel height) in each row into a region object. When a complete image is scanned, what we get is the desired region. This Algorithm In many Article . With region, the following is simple: This. region = BMP RGn (New Bitmap ("D: \ a.bmp"), color. fromargb (0, 0, 0); the above Code uses the outline of D: \ a.bmp as the region of the main window. Assume that the background of the image is black (color. fromargb (0, 0, 0 )). In fact, it is not just form. Any control can use this method to set region and create a special control.

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.