C# button重绘

1.添加一个新类

using System;
using  System.Windows;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Text;
using  System.Drawing;

namespace WindowsFormsApplication2
{
    class MyButton:System.Windows.Forms.Button
    {
        private bool mouseDown = false;
        private bool mouseHover = false;

        public MyButton()
        {
            SetStyle(ControlStyles.UserPaint, true);

            MouseDown += new MouseEventHandler(OnMouseDown);
            MouseUp += new MouseEventHandler(OnMouseUp);
            MouseEnter += new EventHandler(OnMouseEnter);
            MouseLeave +=new EventHandler(OnMouseLeave);

            Height = 23;
            Width = 72;
            BackColor = Color.Red;
            ForeColor = Color.Black;
            FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            Font = System.Windows.Forms.Control.DefaultFont;

           // UseWaitCursor = true;
        }

        private void OnMouseDown(object sender, MouseEventArgs e)
        {
            mouseDown = true;
        }

        private void OnMouseUp(object sender, MouseEventArgs e)
        {
                mouseDown = false;
            OnPaint(new PaintEventArgs(CreateGraphics(), ClientRectangle));
        }

        private void OnMouseEnter(object sender, EventArgs e)
        {
            mouseHover = true;
            OnPaint(new PaintEventArgs(CreateGraphics(), ClientRectangle));
        }

        private void OnMouseLeave(object sender, EventArgs e)
        {
            mouseHover = false;
            OnPaint(new PaintEventArgs(CreateGraphics(),ClientRectangle));
        }

        protected  override void OnPaint(PaintEventArgs pevent)
        {
            pevent.Graphics.FillRectangle(new SolidBrush(Parent.BackColor), pevent.ClipRectangle );
            if (Enabled == false)
            {
                DrawDisableButton(pevent.Graphics);
            }
            else if(mouseDown)
            {
                DrawMoseDownButton(pevent.Graphics);
            }
            else if (mouseHover)
            {
                DrawMosueHoverButton(pevent.Graphics);
            }
            else if(Focused)
            {
                DrawContaionFoceuButton(pevent.Graphics);
            }
            WriteText(pevent.Graphics);
        }

        private void WriteText(Graphics g)
        {
             int x = 0, y = 0;
              Size s = g.MeasureString(Text, Font).ToSize();
               x = (Width - s.Width) / 2;
               y = (Height - s.Height) / 2;
                if (Enabled)
                  g.DrawString(Text, Font, Brushes.Black, x, y);
               else
                   g.DrawString(Text, Font, Brushes.Gray, x, y);
        }

        private void DrawContaionFoceuButton(Graphics g)
        {
             DrawBorder(g, 5);
             PaintBack(g, SystemColors.ControlLightLight);
        }

        private void DrawMosueHoverButton(Graphics g)
        {
            DrawBorder(g, 2);
            PaintBack(g, SystemColors.ControlLightLight);
        }

        private void DrawMoseDownButton(Graphics g)
        {
            DrawBorder(g, 3);
            PaintBack(g, SystemColors.ControlLight);
        }

        private void DrawDisableButton(Graphics g)
        {
            DrawBorder(g, 4);
            PaintBack(g, SystemColors.ControlLight);
        }

        private void PaintBack(Graphics g, Color c)
         {
              g.FillRectangle(new SolidBrush(c), 3, 3, Width - 6, Height-6);
        }
        private void DrawBorder(Graphics g, int state)
        {
            if (state == 1) // draw normal style border
            {
                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)
            {
                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
            {
                 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);
            }
        }

 

    }

}
2.在Form中的设计文件中

this.button1 = new MyButton();

private MyButton button1;

posted @ 2013-10-21 16:20  vstion  阅读(2418)  评论(0编辑  收藏  举报