winform 自定义控件-按钮

本文介绍了如何在WinForm中实现一个自定义按钮控件,通过颜色渐变产生立体效果。控件支持鼠标不同状态下的颜色变化,并提供了圆角矩形和边框颜色的自定义选项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

winform 自定义按钮的实现

按钮效果图如下所示。

 

 

    通过颜色渐变渲染,让控件产生立体效果,上述的按钮就是通过线性渐变画刷二次渐变填充出来的。

    详看代码,后面有时间再加入类似win7计算器的淡入淡出效果。

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Drawing;

using System.Data;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Drawing.Drawing2D;

using System.Drawing.Text;

 

namespace RogerBar

{

    [ToolboxItem(true)]

    public partial class BottonEx : Button

    {

        #region Constructor

        public BottonEx()

        {

            SetStyle(ControlStyles.SupportsTransparentBackColor, true);

            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);

            SetStyle(ControlStyles.AllPaintingInWmPaint, true);

            SetStyle(ControlStyles.ResizeRedraw, true);

            SetStyle(ControlStyles.UserPaint, true);

            this.BackColor = Color.Transparent;

            this.FlatAppearance.BorderSize = 0;

            this.FlatStyle = FlatStyle.Flat;

            InitializeComponent();

        }

        #endregion

 

        #region Private

 

        //按钮状态

        enum ButtonState

        {

            //正常

            Normal,

            //鼠标进入

            Hover,

            //鼠标按下

            Pressed

        };

        private ButtonState State = ButtonState.Normal;

 

        #endregion

 

        #region 属性

 

        #region 颜色表

        [TypeConverter(typeof(ExpandableObjectConverter))]

        public class RenderColorTable

        {

            public Color TopStart { setget; }

            public Color TopEnd { setget; }

            public float Scale { setget; }

            public Color BottomStart { setget; }

            public Color BottomEnd { setget; }

            public Color BorderColor { setget; }

            public RenderColorTable(

                                    Color topStart, Color topEnd,

                                    Color bottomStart, Color bottomEnd,

                                    Color bor

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值