抛砖引玉——XP风格的按钮源代码

发表于:2007-06-30来源:作者:点击数: 标签:
using System; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Data; using System. Windows .Forms; using System.Diagnostics; namespace EastSpider { /// summary /// Summary description for VSNetButton
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Diagnostics;

namespace EastSpider
{
    /// <summary>
    /// Summary description for VSNetButton.
    /// </summary>
    public class XPButton : System.Windows.Forms.Button
    {
        
        bool gotFocus = false;
        bool mouseDown = false;
        bool mouseEnter = false;
        bool useDisableImage = true;
        
        public XPButton()
        {
            SetStyle(ControlStyles.AllPaintingInWmPaint|ControlStyles.UserPaint|ControlStyles.Opaque, true);
        }

        public bool UseDisableImage
        {

            get
            {
                return useDisableImage;
            }
            set
            {
                useDisableImage = value;
            }

        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            Graphics g = pe.Graphics;

            if ( mouseDown )
            {
                DrawSelectedState(g, ColorUtil.VSNetPressedColor);
                return;
            }

            if (mouseEnter)
            {
                DrawSelectedState(g, ColorUtil.VSNetSelectionColor);
                return;
            }
            if ( Enabled )
                DrawNormalState(pe.Graphics);
            else
                DrawDisableState(pe.Graphics);
        }

        protected override void OnMouseEnter(EventArgs e)
        {
            mouseEnter = true;
            base.OnMouseEnter(e);
            Invalidate();
        
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            mouseEnter = false;
            base.OnMouseLeave(e);
            Invalidate();
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            // Don@#t call base class
            // it makes the painting to screw up
            base.OnMouseDown(e);
            mouseDown = true;
            Invalidate();
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            // Don@#t call base class
            // it makes the painting to screw up
            Debug.WriteLine("OnMouseUp callled...");
            base.OnMouseUp(e);
            mouseDown = false;
            Invalidate();
        }

        protected override void OnGotFocus(EventArgs e)
        {
            base.OnGotFocus(e);
            gotFocus = true;
            Invalidate();
        }
        
        protected override void OnLostFocus(EventArgs e)
        {
            base.OnLostFocus(e);
            gotFocus = false;
            Invalidate();
        }

        protected void DrawNormalState(Graphics g)
        {
            Rectangle rc = ClientRectangle;
            // Draw background
            g.FillRectangle(new SolidBrush(SystemColors.Control), rc);
            
            if ( Image != null )
            {
                SizeF sizeF = Image.PhysicalDimension;
                int imageWidth = (int)sizeF.Width;
                int imageHeight = (int)sizeF.Height;
                                
                // We are assuming that the button image is smaller than
                // the button itself
                if ( imageWidth > rc.Width || imageHeight > rc.Height)
                {
                    Debug.WriteLine("Image dimensions need to be smaller that button@#s dimension...");
                    return;
                }
                                
                int x = (Width - imageWidth)/2;
                int y = (Height - imageHeight)/2;
                g.DrawImage(Image, x, y, imageWidth, imageHeight);
            }
        }

        protected void DrawDisableState(Graphics g)
        {
            // Draw disable image
            Rectangle rc = ClientRectangle;
            // Erase whaterver that was there before
            g.FillRectangle(new SolidBrush(SystemColors.Control), rc);
            // Draw border rectangle
            g.DrawRectangle(new Pen(new SolidBrush(SystemColors.ControlDark)),
                rc.Left, rc.Top, rc.Width-1, rc.Height-1);

            // Draw disable image
            if ( Image != null )
            {
                SizeF sizeF = Image.PhysicalDimension;
                int imageWidth = (int)sizeF.Width;
                int imageHeight = (int)sizeF.Height;
                                
                // We are assuming that the button image is smaller than
                // the button itself
                if ( imageWidth > rc.Width || imageHeight > rc.Height)
                {
                    Debug.WriteLine("Image dimensions need to be smaller that button@#s dimension...");
                    return;
                }
                                
                int x = (Width - imageWidth)/2;
                int y = (Height - imageHeight)/2;
                ControlPaint.DrawImageDisabled(g, Image, x, y, SystemColors.Control);

            }
                
        }


        protected void DrawSelectedState(Graphics g, Color selColor)
        {
            Rectangle rc = ClientRectangle;
            // Erase whaterver that was there before
            g.FillRectangle(new SolidBrush(selColor), rc);
            // Draw border rectangle
            g.DrawRectangle(new Pen(new SolidBrush(SystemColors.Highlight)),
                rc.Left, rc.Top, rc.Width-1, rc.Height-1);
                                        
            // Draw image
            if ( Image != null )
            {
                SizeF sizeF = Image.PhysicalDimension;
                int imageWidth = (int)sizeF.Width;
                int imageHeight = (int)sizeF.Height;
                                
                // We are assuming that the button image is smaller than
                // the button itself
                if ( imageWidth > rc.Width || imageHeight > rc.Height)
                {
                    Debug.WriteLine("Image dimensions need to be smaller that button@#s dimension...");
                    return;
                }
                                
                int x = (Width - imageWidth)/2;
                int y = (Height - imageHeight)/2;
                
                int gap = 1;
                if ( selColor != ColorUtil.VSNetPressedColor )
                {
                    // Draw disable image first to produce the popping image effect
                    if ( useDisableImage )
                    {
                        ControlPaint.DrawImageDisabled(g, Image, x, y, SystemColors.Control);
                    }
                    else
                        gap = 0;
                }
                else
                {
                    gap = 0;
                }
                g.DrawImage(Image, x-gap, y-gap, imageWidth, imageHeight);

            }
        }


    }

}


原文转自:http://www.ltesting.net