using System;using System.Windows;using System.Drawing;using System.Drawing.Drawing2D;using System.Windows .Forms ; namespace Control...{ public class XPButtonII:System.Windows.Forms.Button ...{ //state variable for mouse down private bool mouseDown=false; //state variable for mouse hover private bool mouseHover=false; public XPButtonII() ...{ //set the control UserPaint SetStyle(ControlStyles.UserPaint,true); //EventHandler for MouseDown //MouseDown+=new MouseEventHandler(OnMouseDown); //EventHandler for MouseUp //MouseUp+=new MouseEventHandler(OnMouseUp); //EventHandler for MouseEnter MouseEnter+=new EventHandler(OnMouseEnter); //EventHandler for MouseLeave MouseLeave+=new EventHandler(OnMouseLeave); Height=23;//Default Height Width=75;//Default Width } protected override void OnPaint(PaintEventArgs pe) ...{ //first, paint the control with parent form's background color pe.Graphics.FillRectangle(new SolidBrush(Parent.BackColor), pe.ClipRectangle); //if the button is disabled, draw the disable style if (Enabled == false) ...{ DrawDisableButton(pe.Graphics); } else if (mouseDown) ...{ //when mouse down, draw the mouse down style DrawMouseDownButton(pe.Graphics); } else if (mouseHover) ...{ //when mouse hover, draw the mouse hover style DrawMouseHoverButton(pe.Graphics); } else if (Focused) ...{ //when mouse is focused but not mouse hover, //draw the focus style DrawContainFocusButton(pe.Graphics); } else//else, draw the normal style ...{ DrawNormalButton(pe.Graphics); } WriteText(pe.Graphics);//write text } private void OnMouseDown(object sender,MouseEventArgs e) ...{ mouseDown=true; //mouse is down now } private void OnMouseUp(object sender,MouseEventArgs e) ...{ mouseDown=false; //mouse is up now //call paint action PaintEventArgs pe = new PaintEventArgs(CreateGraphics(),ClientRectangle); OnPaint(pe); } private void OnMouseEnter(object sender,EventArgs e) ...{ mouseHover=true; //mouse hover on //call paint action PaintEventArgs pe = new PaintEventArgs(CreateGraphics(),ClientRectangle); OnPaint(pe); } private void OnMouseLeave(object sender,EventArgs e) ...{ mouseHover=false; //mouse is not hover on //call paint action PaintEventArgs pe = new PaintEventArgs(CreateGraphics(),ClientRectangle); OnPaint(pe); } private void DrawBorder(Graphics g,int state) ...{ if (state==1)//draw normal style broder ...{ Pen p = new Pen(SystemColors.ControlLightLight,2); g.DrawLine(p,1,1,1,Height-2); g.DrawLine(p,1,1,Width-2,1); g.DrawLine(p,Width-1,2,Width-1,Height-2); g.DrawLine(p,2,Height-1,Width-2,Height-1); } else if (state==2)//draw hover style border ...{ Pen p = new Pen(Color.Yellow,2); g.DrawLine(p,1,1,1,Height-2); g.DrawLine(p,1,1,Width-2,1); g.DrawLine(p,Width-1,2,Width-1,Height-2); g.DrawLine(p,2,Height-1,Width-2,Height-1); } else if (state==3)//draw pressed style border ...{ Pen p = new Pen(SystemColors.ControlDark,2); g.DrawLine(p,1,1,1,Height-2); g.DrawLine(p,1,1,Width-2,1); g.DrawLine(p,Width-1,2,Width-1,Height-2); g.DrawLine(p,2,Height-1,Width-2,Height-1); } else if (state==4)//draw disabled style border ...{ Pen p = new Pen(SystemColors.ControlLight,2); g.DrawLine(p,1,1,1,Height-2); g.DrawLine(p,1,1,Width-2,1); g.DrawLine(p,Width-1,2,Width-1,Height-2); g.DrawLine(p,2,Height-1,Width-2,Height-1); } else if (state==5)//draw default style border ...{ Pen p = new Pen(Color.SkyBlue,2); g.DrawLine(p,1,1,1,Height-2); g.DrawLine(p,1,1,Width-2,1); g.DrawLine(p,Width-1,2,Width-1,Height-2); g.DrawLine(p,2,Height-1,Width-2,Height-1); } if (state==4)//draw disable style border ...{ Pen p = new Pen(Color.FromArgb(161,161,146),1); g.DrawLine(p,0,2,0,Height-3); g.DrawLine(p,2,0,Width-3,0); g.DrawLine(p,Width-1,2,Width-1,Height-3); g.DrawLine(p,2,Height-1,Width-3,Height-1); g.DrawLine(p,0,2,2,0); g.DrawLine(p,0,Height-3,2,Height-1); g.DrawLine(p,Width-3,0,Width-1,2); g.DrawLine(p,Width-3,Height-1,Width-1,Height-3); } else//draw normal style border ...{ g.DrawLine(Pens.Black,0,2,0,Height-3); g.DrawLine(Pens.Black,2,0,Width-3,0); g.DrawLine(Pens.Black,Width-1,2,Width-1,Height-3); g.DrawLine(Pens.Black,2,Height-1,Width-3,Height-1); g.DrawLine(Pens.Black,0,2,2,0); g.DrawLine(Pens.Black,0,Height-3,2,Height-1); g.DrawLine(Pens.Black,Width-3,0,Width-1,2); g.DrawLine(Pens.Black,Width-3,Height-1, Width-1,Height-3); } } private void DrawNormalButton(Graphics g)//draw normal style button ...{ //draw normal style border DrawBorder(g,1); //paint background PaintBack(g,SystemColors.ControlLightLight); } private void DrawMouseHoverButton(Graphics g) ...{ //draw mouse hover style border DrawBorder(g,2); //paint background PaintBack(g,SystemColors.ControlLightLight); } private void DrawMouseDownButton(Graphics g) ...{ //draw mouse down style border DrawBorder(g,3); //paint background PaintBack(g,SystemColors.ControlLight); } private void DrawDisableButton(Graphics g) ...{ //draw disable style border DrawBorder(g,4); //paint background PaintBack(g,SystemColors.ControlLight); } private void DrawContainFocusButton(Graphics g) ...{ //draw contain focuse style border DrawBorder(g,5); //paint background PaintBack(g,SystemColors.ControlLightLight); } //paint background area of the button private void PaintBack(Graphics g,Color c) ...{ g.FillRectangle(new SolidBrush(c),3,3, Width-6,Height-6); } private void WriteText(Graphics g) ...{ //calculate the text position int x=0,y=0; Size s = g.MeasureString(Text,Font).ToSize(); x=(Width-s.Width)/2; y=(Height-s.Height)/2; //draw text if (Enabled) //is enabled, draw black text g.DrawString(Text,Font,Brushes.Black,x,y); else //not enabled, draw gray text g.DrawString(Text,Font,Brushes.Gray,x,y); } }}using System;using System.Windows;using System.Drawing;using System.Drawing.Drawing2D;using System.Windows .Forms ; namespace Control...{ public class XPButton:System.Windows.Forms.Button ...{ private bool mouseover=false; public XPButton() ...{ // // TODO: 在此处添加构造函数逻辑 // 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); } } }