C # WinForm making irregular Forms (code)

Source: Internet
Author: User
Tags transparent color
Previously wanted to make irregular forms, mostly using API functions to implement, in C #, you can also do not use API functions can still make beautiful irregular forms, the following describes the relevant methods, the following is the code.

        private void Form1_Load (object sender, EventArgs e)        {            //Redraw window style            string fileName = @ "C:\Users\admin\ Desktop\yuan1.png ";            Bitmap Mybitmap = new Bitmap (fileName);            Createcontrolregion (this, mybitmap);            This. BackColor = color.white;//here for adding part of this              . TransparencyKey = color.white;//Here for add part          }
        <summary>///Redraw window styles///</summary>//<param name= "Control" ></pa ram>//<param name= "bitmap" ></param> public static void Createcontrolregion (Control control , Bitmap Bitmap) {//Return if control and Bitmap are NULL//Determine if there is a control and bitmap if ( Control = = NULL | |            Bitmap = = null) return; Sets the control size to bitmap size control. Width = Bitmap.            Width; Control. Height = Bitmap.            Height;             Check If we are dealing with form here//when the control is a form if (control is System.Windows.Forms.Form) {//cast to a form ' object//cast to form form ' form = (form) cont                Rol When the form's boundary FormBorderStyle is not none, the size of the form should be set slightly larger than the bitmap size. Width = control.                Width; Form. Height = control.                Height; There is no boundary form. FOrmborderstyle = Formborderstyle.none; Sets a bitmap as a form background picture.                BackgroundImage = bitmap;                Calculates the bounds of the opaque part of the bitmap GraphicsPath GraphicsPath = Calculatecontrolgraphicspath (bitmap); Apply a new region form.                Region = new Region (GraphicsPath); The following statement is added for yourself, and the problem form does not add the two sentences. Width = Bitmap.                Width; Form. Height = Bitmap.            Height; }//When the control is a Button else if (control is System.Windows.Forms.Button) {//force-Go                Change To Button button = (button) control; The button text button is not displayed.                Text = ""; Change the cursor's style button.                Cursor = Cursors.hand; Sets the button's background picture button.                BackgroundImage = bitmap;                Calculates the bounds of the opaque part of the bitmap GraphicsPath GraphicsPath = Calculatecontrolgraphicspath (bitmap); ApplyNew Region//apply a fresh zone button.                Region = new Region (GraphicsPath); button. Width = Bitmap.                Width; button. Height = Bitmap.                Height; button. FlatStyle = flatstyle.popup;//Here is the add part}} private static GraphicsPath CALCULATECONTROLGRAPHICSP            Ath (Bitmap Bitmap) {//create GraphicsPath GraphicsPath GraphicsPath = new GraphicsPath (); Use the color of the top left corner as our transparent color colortransparent = bitmap.            GetPixel (0, 0);            The first one to find the x int colopaquepixel = 0; Bias calendar All rows (y direction) for (int row = 0; row < bitmap. Height-1;                row++) {//Reset value//reset Colopaquepixel = 0; Partial Calendar All columns (x direction) for (int col = 0; col < bitmap. Width-1; col++) {//If it is a point that does not need to be processed transparently, then mark, and then continue to the biased if (bitmap. GetPixel (Col, ROW)! = colortransparent) {colopaquepixel = col;                        Create a new variable to record the current point int colnext = col; From the found opaque point, continue to find the opacity point, until you find or reach the picture width for (colnext = Colopaquepixel; Colnext < bitmap. Width; colnext++) {Color GPI = bitmap.                            GetPixel (Colnext, Row); if (bitmap.                            GetPixel (colnext, row) = = Colortransparent) {break;                            }}//Add an opaque point to the graphics path {                        Graphicspath.addrectangle (New Rectangle (Colopaquepixel, Row, Colnext-colopaquepixel, 1));                    } col = Colnext;        }}} return graphicsPath; }

Perfect!

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.