当前位置:   article > 正文

C#控件开发-按键美化_winform美化按钮

winform美化按钮

C#控件开发-按键美化

Winform控件美化有两种方式:一是继承自现有控件,使用现有控件的属性、方法开发美化。另一种是继承Control类,完全自定义控件开发。
这篇文章以按键美化为例讲继承现有控件开发。

一、鼠标状态

按键有三种鼠标状态:空状态,鼠标放上及鼠标按下。定义枚举类型

/// <summary>
/// 鼠标状态
/// </summary>
public enum MouseActionType
{
    None,
    Hover,
    Click
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
二、按键颜色

对应鼠标的三种状态,定义三种颜色与状态对应

  • _baseColor 空状态
  • _clickColor 按下
  • _enterColor 放上状态
三、自定义ButtonEx类

新建ButtonEx类,继承自Button类,引入System.Windows.Forms命令空间

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

namespace WindowsFormsApp1
{
    public class ButtonEx : Button
    {
    	private MouseActionType mouseAction;

        private string _title = "title";
        /// <summary>
        /// 
        /// </summary>
        [Description("获取或设置标题")]
        public string Title
        {
            get { return _title; }
            set
            {
                _title = value;
            }
        }

        private Color _baseColor = Color.DodgerBlue;

        private Color _clickColor = Color.FromArgb(255, 0, 0);
        [Description("鼠标按下颜色")]
        [Category("外观")]
        public Color ClickColor
        {
            get { return _clickColor; }
            set { _clickColor = value; }
        }

        private Color _enterColor = Color.Red;
        [Description("鼠标进入色")]
        [Category("外观")]
        public Color EnterColor
        {
            get { return _enterColor; }
            set { _enterColor = value; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public ButtonEx ()
        {
            mouseAction = MouseActionType.None;
            this.SetStyle(ControlStyles.SupportsTransparentBackColor |
                ControlStyles.UserPaint |
                ControlStyles.ResizeRedraw |
                ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.Opaque, false);
            this.Width = 20;
            this.Height = 20;
        }


        protected override void OnPaint(PaintEventArgs pevent)
        {
            //base.OnPaint(pevent);
            Graphics g = pevent.Graphics;
            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.CompositingQuality = CompositingQuality.HighQuality;

            Color clr = this.BackColor;
            switch (mouseAction)
            {
                case MouseActionType.None:
                    using (SolidBrush sb = new SolidBrush(BackColor))
                    {
                        g.FillRectangle(sb, this.ClientRectangle);
                    }
                    break;
                case MouseActionType.Hover:
                    using (SolidBrush sb = new SolidBrush(_enterColor))
                    {
                        g.FillRectangle(sb, this.ClientRectangle);
                    }
                    break;
                case MouseActionType.Click:
                    using (SolidBrush sb = new SolidBrush(_clickColor))
                    {
                        g.FillRectangle(sb, this.ClientRectangle);
                    }
                    break;


                default:
                    break;
            }

            using (Pen pen = new Pen(Color.Red, 2f))
            {
                g.DrawLine(pen, new Point(4, 4), new Point(Width - 4, Height - 4));
                g.DrawLine(pen, new Point(Width - 4, 4), new Point(4, Height - 4));
            }
        }
		// 以下重写鼠标事件
        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.mouseAction = MouseActionType.Click;
                this.Invalidate();
            }
            base.OnMouseDown(e);
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            this.mouseAction = MouseActionType.Hover;
            this.Invalidate();
            base.OnMouseUp(e);
        }

        protected override void OnMouseHover(EventArgs e)
        {
            this.mouseAction = MouseActionType.Hover;
            this.Invalidate();
            base.OnMouseHover(e);
        }

        protected override void OnMouseEnter(EventArgs e)
        {
            this.mouseAction = MouseActionType.Hover;
            this.Invalidate();
            base.OnMouseEnter(e);
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            this.mouseAction = MouseActionType.None;
            this.Invalidate();
            base.OnMouseLeave(e);
        }
    }

}
    /// <summary>
    /// 鼠标状态
    /// </summary>
public enum MouseActionType
{
    None,
    Hover,
    Click
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152

四、生成使用

使用按键前先生成解决方案,编译通过后工具栏里会出现相应按键
在这里插入图片描述
属性窗体对应颜色
在这里插入图片描述
F5启动程序
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/137897
推荐阅读
相关标签
  

闽ICP备14008679号