C#winform make profiled Forms/controls

Source: Internet
Author: User
Tags transparent color
Control the idea of making an alien form or control is generally 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. Below I will be in C # to implement the core code of the form of the key to show you to see what the comments, despite mentioning, don't be polite oh J first, is a way to generate region based on the bitmap object:///<summary>///Gets an area of the non-transparent color part of the picture. </summary>///<param name= "picture" > Take pictures of its area. </param>///<param name= "TransparentColor" > Transparent color. </param>///<returns> photo </returns>private Region bmprgn (Bitmap picture, color TransparentColor) {     int nwidth = picture.width;     int nHeight = picture.height;     Region rgn = new Region ();     rgn. Makeempty ();     bool istransrgn;//The color of the previous point in the transparent area      color curcolor;//the current point      Rectangle currect = new Rectangle ();     currect.height = 1;  & nbsp;  int x = 0, y = 0;    //pixel-by-scan this image, find the opaque color part of the area and merge.      for (y = 0; y < nheight ++y)      {          istransrgn = true;         for (x = 0; x < nwidth; ++x)  &nbs p;       {               Curcolor = Picture.getpixel (x,y);               if (curcolor = TransparentColor | | | x = = nWidth-1)//If you encounter a transparent color or line end        & nbsp;           {                        if (Istransrgn = False )//exit valid area                         {                             currect.width = x-currect.x;                              Rgn. Union (currect);                       }                   }                    else//non-transparent color                     {                        if (Istransrgn = = true)//Enter active area &Nbsp;                       {                             currect.x = x;                              Currect.y = y;                       }                   }//if curcolor                    Istransrgn = Curcolor = = transparentcolor;                   }//for x        }//for y         The return rgn;    }  principle is simply to scan the picture line by row, merging the opaque rectangles (only one like ICAO) into a region object in each row. When the whole picture is scanned, we get the region we want. 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 put the D:\ The outline of the a.bmp is the region of the main window, assuming that the background of the picture is black (Color.FromArgb (0, 0, 0)). 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.