当前位置:编程学习 > C#/ASP.NET >>

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

答案: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
         

上一个:Globalized Property Grid (一)
下一个:Working with Anchoring and Docking Properties(ZT)

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,