Rewrite the button control under C #

Source: Internet
Author: User
Control XPButton.cs Code

Using System;

Using System.Windows;

Using System.Drawing;

Using System.Drawing.Drawing2D;

Using System.Windows. Forms;



Namespace Xpctrl

{

<summary>

Summary description of the Xpbutton.

</summary>

public class XPButton:System.Windows.Forms.Button

{

private bool Mouseover=false;

Public Xpbutton ()

{

//

TODO: Add constructor logic here

//

This. Cursor = System.Windows.Forms.Cursors.Hand;

}

protected override void OnPaint (System.Windows.Forms.PaintEventArgs e)

{

Base. OnPaint (e);

System.Drawing.Graphics Pp=this. CreateGraphics ();

E.graphics.clear (color.transparent);

E.graphics.drawellipse (New System.Drawing.Pen (system.drawing.color.whitesmoke,5), 0,0,this. Width,this. Height);

System.Drawing.SolidBrush dd=new SolidBrush (System.Drawing.Color.WhiteSmoke);

E.graphics.fillellipse (dd,0,0,this. Width,this. Height);



(This. Backcolor.tostring ())

Color C5 = Color.FromArgb

(255,255,255);

Color C2 = Color.FromArgb

(192,192,192);

if (mouseover)

{

C5=color.fromargb (245,245,245);

C2=color.fromargb (192,192,192);

C2=color.fromargb (180,175,190);

}

Brush B = New System.Drawing.Drawing2D.LinearGradientBrush

(ClientRectangle, C5, C2, lineargradientmode.vertical);

System.drawing.region=new Region (

int Offsetwidth=this. WIDTH/50;

Point[] Points=new point[8];

Points[0]. X=offsetwidth;

Points[0]. y=0;



POINTS[1]. X=this. Width-offsetwidth;

POINTS[1]. y=0;



POINTS[2]. X=this. Width;

POINTS[2]. Y=offsetwidth;



POINTS[3]. X=this. Width;

POINTS[3]. Y=this. Height-offsetwidth;



POINTS[4]. X=this. Width-offsetwidth;

POINTS[4]. Y=this. Height;



POINTS[5]. X=offsetwidth;

POINTS[5]. Y=this. Height;



POINTS[6]. x=0;

POINTS[6]. Y=this. Height-offsetwidth;



POINTS[7]. x=0;

POINTS[7]. Y=offsetwidth;

E.graphics.fillrectangle (b, ClientRectangle);

E.graphics.fillpolygon (b,points,fillmode.winding);

if (this. Focused)

{

int offsetwidth1= (this. WIDTH-5)/50+2;

Point[] Points1=new point[8];

Points1[0]. X=offsetwidth1;

Points1[0]. y=2;



POINTS1[1]. X=this. Width-offsetwidth1;

POINTS1[1]. y=2;



POINTS1[2]. X=this. Width-1;

POINTS1[2]. Y=offsetwidth1;



POINTS1[3]. X=this. Width-1;

POINTS1[3]. Y=this. Height-offsetwidth1;



POINTS1[4]. X=this. Width-offsetwidth1;

POINTS1[4]. Y=this. Height-1;



POINTS1[5]. X=1;

POINTS1[5]. Y=this. Height-1;



POINTS1[6]. x=2;

POINTS1[6]. Y=this. Height-offsetwidth1;



POINTS1[7]. x=2;

POINTS1[7]. Y=offsetwidth1;

E.graphics.drawpolygon (New Pen (color.yellow,2), points1);

Pen p=new Pen (color.orange,2);

Pen p1=new Pen (color.wheat,2);

P.dashstyle=dashstyle.dashdot;

E.graphics.drawline (p1,points1[0],points1[1]);



E.graphics.drawline (p,points1[1],points1[2]);

E.graphics.drawline (P,points1[2],points1[3]);

E.graphics.drawline (P,points1[3],points1[4]);

E.graphics.drawline (P,points1[4],points1[5]);

E.graphics.drawline (P,points1[5],points1[6]);

E.graphics.drawline (P1,points1[6],points1[7]);

E.graphics.drawline (P1,points1[7],points1[0]);

}

E.graphics.drawpolygon (New Pen (color.darkblue,2), points);

E.graphics.drawline (New Pen (color.darkblue,2), new Point (0,0), new Point (this. width,0));

E.graphics.drawline (New Pen (color.darkblue,2), new Point (0,0), New Point (0,this. Height));

E.graphics.drawline (New Pen (color.darkblue,2), new Point (this. Width,this. Height), new Point (this. width,0));

E.graphics.drawline (New Pen (color.darkblue,2), new Point (this. Width,this. Height), new Point (0,this. Height));

StringFormat Drawformat = new StringFormat ();

Drawformat.formatflags = Stringformatflags.displayformatcontrol;

Drawformat.linealignment=stringalignment.center;

Drawformat.alignment=system.drawing.stringalignment.center;



E.graphics.drawstring (this. Text,this. Font,new LinearGradientBrush (this. clientrectangle,color.black,color.black,lineargradientmode.vertical), this. Clientrectangle,drawformat);

B.dispose ();





}

protected override void Onleave (EventArgs e)

{

Base. Onleave (e);

}



protected override void Onmousehover (EventArgs e)

// {

//

Mouseover=true;

This. Invalidate (FALSE);

Base. Onmousehover (e);

// }

protected override void Onmouseenter (EventArgs e)

{

Mouseover=true;

This. Invalidate (FALSE);

Base. Onmouseenter (e);

}



protected override void Onnotifymessage (System.Windows.Forms.Message m)

{

Base. Onnotifymessage (m);

}



protected override void OnMouseLeave (EventArgs e)

{

Mouseover=false;

This. Invalidate (FALSE);

Base. OnMouseLeave (e);

}

private void DrawButton (System.Drawing.Graphics g)

{



}

protected override void OnPaintBackground (System.Windows.Forms.PaintEventArgs pevent)

{



Color C5 = Color.FromArgb

(255,255,255);

Color C2 = Color.FromArgb

(192,192,192);

if (mouseover)

// {

C5=color.fromargb (245,245,245);

C2=color.fromargb (192,192,192);

C2=color.fromargb (180,175,190);

// }

Brush B = New System.Drawing.Drawing2D.LinearGradientBrush

(ClientRectangle, C5, C2, lineargradientmode.vertical);



Pevent. Graphics. DrawRectangle (New Pen (color.transparent,2), this. ClientRectangle);

Pevent. Graphics. Clear (Color.wheat);

Base. OnPaintBackground (pevent);



}

}

}




Called in Form1.cs


My method is to drag a Windows button first, and then change two places


One is the variable definition:


Private Xpctrl.xpbutton button1;


The other is the code generated by the Windows Forms Designer:


This.button1 = new Xpctrl.xpbutton ();


Effect:





This color that I use is grey, if change color can be very beautiful






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.