C WinForm Method of Making Special shaped Forms and Controls

  • 2021-12-13 16:41:37
  • OfStack

This paper describes the method of making special-shaped form and control by C # WinForm. Share it for your reference, as follows:

The idea of making special-shaped forms or controls is to find a way to generate an region and then set it to the specified window or control. There are many ways to generate region, the most commonly used one is to generate it from a picture, "dig out" the transparent color part of the picture, and take the rest as an region. You can use SetWindowRgn API to set the region of a window or control, but. NET framework encapsulates this operation, and in C # you simply assign a value to the Region property of the window or control. Below I will I in C # in the implementation of the form of the core code posted out for everyone to see, have any opinion though to mention, you're welcome oh

First, it is a method to generate Region from Bitmap object:


/// <summary>
///  Acquire 1 The area of the opaque color part of the picture. 
/// </summary>
/// <param name="Picture"> Take the picture of its area. </param>
/// <param name="TransparentColor"> Transparent color. </param>
/// <returns> The area of the picture that is not transparent </returns>
private Region BmpRgn(Bitmap Picture, Color TransparentColor)
{
   int nWidth = Picture.Width;
   int nHeight = Picture.Height;
   Region rgn = new Region();
   rgn.MakeEmpty();
   bool isTransRgn;// Front 1 Are the points in the transparent area 
   Color curColor;// The color of the current dot 
   Rectangle curRect = new Rectangle();
   curRect.Height = 1;
   int x = 0, y = 0;
   // Scan the picture pixel by pixel, find out the opaque color parts 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 you encounter a transparent color or end of a line 
          {
            if(isTransRgn == false)// Exit valid area 
            {
              curRect.Width = x - curRect.X;
              rgn.Union(curRect);
            }
          }
          else// Opaque color 
          {
            if(isTransRgn == true)// Enter the effective 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 picture line by line, and merge those opaque rectangles (only one pixel high) into an Region object in every line (union). When scanning the whole picture, we get the Region we want. This algorithm has been introduced 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 takes the outline of d:\ a.bmp as 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, making special-shaped controls.

For more readers interested in C # related content, please check the topics on this site: "Summary of C # Form Operation Skills", "C # Data Structure and Algorithm Tutorial", "C # Common Control Usage Tutorial", "C # Object-Oriented Programming Introduction Tutorial" and "Summary of Thread Use Skills in C # Programming"

I hope this article is helpful to everyone's C # programming.


Related articles: