How to make special form and control in C # WinForm

Source: Internet
Author: User
Tags image line transparent color
This article mainly introduces C # WinForm The method of making irregular form and control, and analyzes the principle, realization steps and related operation skill of winform making irregular form and control with instance form, the friend who need can refer to

The examples in this article describe C # WinForm methods for making profiled forms and controls. Share to everyone for your reference, as follows:

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 everyone, what is the opinion despite the mention, not polite oh

First, there is a way to generate region based on the bitmap object:

<summary>///gets an area of the non-transparent part of the picture. </summary>///<param name= "picture" > Take pictures of its area. </param>///<param name= "TransparentColor" > Transparent color. </param>///<returns> Picture area of non-transparent color </returns>private region bmprgn (Bitmap pictures, color   TransparentColor) {int nwidth = Picture.width;   int nheight = Picture.height;   Region RGN = new region (); Rgn.   Makeempty ();   BOOL istransrgn;//Whether the previous point is color curcolor;//the current point in the transparent area 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 you encounter a transparent color or a line end {if (Istransrgn = = false)//exit the active area              {currect.width = X-currect.x; Rgn.            Union (currect); }} else//non-transparent color {if (Istransrgn = = true)//Enter the active area {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.

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.