请问大家在C#中怎样自绘按钮呢,鼠标在按钮上方按钮变为红色,鼠标按下变为蓝色,应该怎么实现?
别的控件有DrawItem,但是按钮控件好像没有.谢谢!

解决方案 »

  1.   

    有事件啊
    buttonclick 等等的
      

  2.   

    自定义控件,重写onpaint,onmouseclick....等相关事件
      

  3.   

     [ToolboxBitmap(typeof(System.Windows.Forms.Button))]
        public partial class Button : System.Windows.Forms.Button
        {
            private State state = State.Normal;
            public Button()
            {
                Font = ControlCommon.Font;
                BackColor = Color.Transparent;
                try
                {
                    //打开双缓冲
                    this.SetStyle(ControlStyles.DoubleBuffer, true);
                    this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
                    this.SetStyle(ControlStyles.UserPaint, true);
                    this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
                    this.SetStyle(ControlStyles.StandardDoubleClick, false);
                    this.SetStyle(ControlStyles.Selectable, false);
                    this.ResizeRedraw = true;                
                }
                catch { }
            }        //[DefaultValue(Color.Transparent.ToArgb())]
            //public override Color BackColor
            //{
            //    get { return base.BackColor; }
            //    set { base.BackColor = value; }
            //}
            //[DefaultValue(Color.FromArgb(33, 33, 33).ToArgb())]
            //public override Color ForeColor
            //{
            //    get { return base.ForeColor; }
            //    set { base.ForeColor = value; }
            //}
            protected override void OnMouseEnter(EventArgs e)
            {
                state = State.MouseOver;
                this.Invalidate();
                base.OnMouseEnter(e);
            }        protected override void OnMouseLeave(EventArgs e)
            {
                state = State.Normal;
                this.Invalidate();
                base.OnMouseLeave(e);
            }        protected override void OnMouseDown(MouseEventArgs e)
            {
                if ((e.Button & MouseButtons.Left) != MouseButtons.Left) return;            state = State.MouseDown;
                this.Invalidate();
                base.OnMouseDown(e);
            }        protected override void OnMouseUp(MouseEventArgs e)
            {
                if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
                    state = State.Normal;
                this.Invalidate();
                base.OnMouseUp(e);
            }        protected override void OnPaint(PaintEventArgs e)
            {
                if (SkinImage.button.img == null)
                {
                    base.OnPaint(e);
                    return;
                }            int i = (int)state;
                if (this.Focused && state != State.MouseDown) i = 5;
                if (!this.Enabled) i = 4;
                Rectangle rc = this.ClientRectangle;
                Graphics g = e.Graphics;            //重绘控件背景
                base.InvokePaintBackground(this, new PaintEventArgs(e.Graphics, base.ClientRectangle));            SkinDraw.DrawRect2(g, SkinImage.button, rc, i);            Image img = null;
                Size txts, imgs;            txts = Size.Empty;
                imgs = Size.Empty;            if (this.Image != null)
                {
                    img = this.Image;
                }
                else if (this.ImageList != null && this.ImageIndex != -1)
                {
                    img = this.ImageList.Images[this.ImageIndex];
                }            if (img != null)
                {
                    imgs.Width = img.Width;
                    imgs.Height = img.Height;
                }            StringFormat format1;
                using (format1 = new StringFormat())
                {
                    format1.HotkeyPrefix = HotkeyPrefix.Show;
                    SizeF ef1 = g.MeasureString(this.Text, this.Font, new SizeF((float)rc.Width, (float)rc.Height), format1);
                    txts = Size.Ceiling(ef1);
                }            rc.Inflate(-4, -4);
                if (imgs.Width * imgs.Height != 0)
                {
                    Rectangle imgr = rc;
                    imgr = SkinDraw.HAlignWithin(imgs, imgr, this.ImageAlign);
                    imgr = SkinDraw.VAlignWithin(imgs, imgr, this.ImageAlign);
                    if (!this.Enabled)
                    {
                        ControlPaint.DrawImageDisabled(g, img, imgr.Left, imgr.Top, this.BackColor);
                    }
                    else
                    {
                        g.DrawImage(img, imgr.Left, imgr.Top, img.Width, img.Height);
                    }
                }            //Rectangle txtr = rc;
                //txtr = SkinDraw.HAlignWithin(txts, txtr, this.TextAlign);
                //txtr = SkinDraw.VAlignWithin(txts, txtr, this.TextAlign);            Brush brush2;
                format1 = new StringFormat();
                format1.HotkeyPrefix = HotkeyPrefix.Show;            if (this.RightToLeft == RightToLeft.Yes)
                {
                    format1.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
                }
                brush2 = new SolidBrush(this.ForeColor);
                //if (this.Enabled)
                //{
                //    this.ForeColor = Color.FromArgb(33, 33, 33);
                //}
                //else
                //{
                //    this.ForeColor = Color.FromArgb(200, 200, 200);
                //}
                format1.LineAlignment = StringAlignment.Center;
                format1.Alignment = StringAlignment.Center;
                g.DrawString(this.Text, this.Font, brush2, ClientRectangle, format1);
                brush2.Dispose();
            }
        }