using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; using System.Windows.Forms; using System.Windows.Forms.VisualStyles; namespace StudyWinForm1 { class MyEllipseButton:Button { //state variable for mouse down private bool mouseDown=false; //state variable for mouse hover private bool mouseHover=false; public MyEllipseButton() { //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 //set the back color to blue BackColor = System.Drawing.Color.Blue; //set the appearance style to flat FlatStyle = System.Windows.Forms.FlatStyle.Flat; //use system default font Font = System.Windows.Forms.Control.DefaultFont; //use white color to write text and draw the border ForeColor = System.Drawing.Color.White; } 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); //pe.Graphics.FillEllipse(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(System.Drawing.SystemColors.ControlLightLight, 2); g.DrawEllipse(p, 0, 0, Width - 6, Height - 6); } else if (state==2)//draw hover style border { Pen p = new Pen(Color.Yellow,2); g.DrawEllipse(p, 0, 0, Width - 7, Height - 7); } else if (state==3)//draw pressed style border { Pen p = new Pen(System.Drawing.SystemColors.ControlDark, 2); g.DrawEllipse(p, 0, 0, Width - 7, Height - 7); } else if (state==4)//draw disabled style border { Pen p = new Pen(System.Drawing.SystemColors.ControlLight, 2); g.DrawEllipse(p, 0, 0, Width - 7, Height - 7); } else if (state==5)//draw default style border { Pen p = new Pen(Color.SkyBlue,2); g.DrawEllipse(p, 0, 0, Width - 7, Height - 7); } if (state==4)//draw disable style border { Pen p = new Pen(Color.FromArgb(161,161,146),1); g.DrawEllipse(p, 0, 0, Width - 6, Height - 6); g.DrawEllipse(p, 1, 1, Width - 7, Height - 7); } else//draw normal style border { g.DrawEllipse(Pens.Black, 0, 0, Width - 6, Height - 6); g.DrawEllipse(Pens.Black, 1, 1, Width - 7, Height - 7); } } private void DrawNormalButton(Graphics g)//draw normal style button { //draw normal style border DrawBorder(g,1); //paint background //PaintBack(g, System.Drawing.SystemColors.ControlLightLight); PaintBack(g, Color.Yellow); } private void DrawMouseHoverButton(Graphics g) { //draw mouse hover style border DrawBorder(g,2); //paint background //PaintBack(g, System.Drawing.SystemColors.ControlLightLight); PaintBack(g, Color.Violet); } private void DrawMouseDownButton(Graphics g) { //draw mouse down style border DrawBorder(g,3); //paint background //PaintBack(g, System.Drawing.SystemColors.ControlLight); PaintBack(g, Color.Tomato); } private void DrawDisableButton(Graphics g) { //draw disable style border DrawBorder(g,4); //paint background //PaintBack(g, System.Drawing.SystemColors.ControlLight); PaintBack(g, Color.Transparent); } private void DrawContainFocusButton(Graphics g) { //draw contain focuse style border DrawBorder(g,5); //paint background //PaintBack(g, System.Drawing.SystemColors.ControlLightLight); PaintBack(g, Color.Turquoise); } //paint background area of the button private void PaintBack(Graphics g,Color c) { //g.FillRectangle(new SolidBrush(c), 3, 3, Width - 6, Height - 6); g.FillEllipse(new SolidBrush(c), 0, 0, Width - 6, Height - 6); } private void WriteText(Graphics g) { //calculate the text position int x=0,y=0; System.Drawing.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); } } }