winform扩展控件,Button的圆角和从下往上的渐变

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace mybutton
{
    public class ButtonEX : Button
    {


        [Browsable(true), DefaultValue(90), Description("按钮主体颜色渐变方向,X轴顺时针开始")]
        [Category("Appearance")]
        public int GradientAngle { get; set; }
        protected override void OnPaint(PaintEventArgs pevent)
        {
            base.OnPaint(pevent);

            //圆角和渐变的方法调用
            Draw(pevent.ClipRectangle, pevent.Graphics, false, Color.Blue);
        }

        public int Radius { get; set; }
        public ButtonEX()
        {
            //这些得带上,不然会有黑边
            FlatStyle = FlatStyle.Flat;
            FlatAppearance.BorderSize = 0;
            FlatAppearance.BorderColor = Color.FromArgb(0, 0, 0, 0);
            FlatAppearance.MouseDownBackColor = Color.Transparent;
            FlatAppearance.MouseOverBackColor = Color.Transparent;
        }


        /// <summary>
        /// 画圆角和渐变的方法
        /// </summary>
        /// <param name="rectangle"></param>
        /// <param name="g"></param>
        /// <param name="cusp"></param>
        /// <param name="begin_color"></param>
        /// <param name="end_colorex"></param>
        void Draw(Rectangle rectangle, Graphics g, bool cusp, Color begin_color, Color? end_colorex = null)
        {
            Color end_color = end_colorex == null ? begin_color : (Color)end_colorex;
            int span = 2;
            //抗锯齿
            g.SmoothingMode = SmoothingMode.AntiAlias;
            //渐变填充
            Brush b = new LinearGradientBrush(
                 //起始位置
                 new Point(this.Width, this.Height),
                 //结束位置
                 new Point(this.Width, -1),
                 //起始颜色
                 Color.FromArgb(255, 192, 0),   // Opaque red
                 //结束颜色
                 Color.FromArgb(255, 243, 218));  // Opaque blue);
            //画尖角
            if (cusp)
            {
                span = 10;
                PointF p1 = new PointF(rectangle.Width - 12, rectangle.Y + 10);
                PointF p2 = new PointF(rectangle.Width - 12, rectangle.Y + 30);
                PointF p3 = new PointF(rectangle.Width, rectangle.Y + 20);
                PointF[] ptsArray = { p1, p2, p3 };
                g.FillPolygon(b, ptsArray);
            }
            //填充
            g.FillPath(b, DrawRoundRect(rectangle.X, rectangle.Y, rectangle.Width - span, rectangle.Height - 1, Radius));
        }
       
        /// <summary>
        /// 画圆角的方法
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="radius"></param>
        /// <returns></returns>
        GraphicsPath DrawRoundRect(int x, int y, int width, int height, int radius)
        {
            //四边圆角
            GraphicsPath gp = new GraphicsPath();
            gp.AddArc(x, y, radius, radius, 180, 90);
            gp.AddArc(width - radius, y, radius, radius, 270, 90);
            gp.AddArc(width - radius, height - radius, radius, radius, 0, 90);
            gp.AddArc(x, height - radius, radius, radius, 90, 90);
            gp.CloseAllFigures();
            return gp;
        }
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
WinForm 圆角控件是指将控件的边角变为圆角形状的一种效果。这种效果可以在很多场景中使用,比如展示用户头像时将头像切成圆形显示,或者对其他控件进行圆角处理以增加美观度。 在实现 WinForm 圆角控件的过程中,可以使用代码来简单实现。一种常见的方法是通过自定义控件或者使用已有的类库来实现。 一个常见的实现方法是使用 `GraphicsPath` 类和 `Region` 类来创建一个具有圆角边界的 `Region` 对象,然后将该 `Region` 对象应用到控件上。 具体的实现步骤如下: 1. 创建一个继承自指定控件类型的自定义控件类,比如 `RoundButton`。 2. 在自定义控件类的构造函数中创建一个 `GraphicsPath` 对象,并使用 `AddRectangle` 方法将控件的边界添加到 `GraphicsPath` 对象中。 3. 调用 `AddArc` 方法来添加圆角到 `GraphicsPath` 对象中,可以根据需求自定义圆角的大小。 4. 创建一个 `Region` 对象,并将 `GraphicsPath` 对象应用到 `Region` 对象上。 5. 调用控件的 `SetRegion` 方法,将 `Region` 对象应用到控件上。 以下是一个示例代码: ```csharp using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; public class RoundButton : Button { public RoundButton() { // 创建 GraphicsPath 对象 GraphicsPath path = new GraphicsPath(); path.AddRectangle(new Rectangle(0, 0, Width, Height)); // 添加圆角到 GraphicsPath 对象 int cornerRadius = 10; // 可根据需求自定义圆角大小 path.AddArc(0, 0, cornerRadius, cornerRadius, 180, 90); path.AddArc(Width - cornerRadius, 0, cornerRadius, cornerRadius, 270, 90); path.AddArc(Width - cornerRadius, Height - cornerRadius, cornerRadius, cornerRadius, 0,90); path.AddArc(0, Height - cornerRadius, cornerRadius, cornerRadius, 90, 90); // 创建 Region 对象并应用到控件上 Region = new Region(path); } } ``` 使用这个自定义的圆角控件类时,只需要将控件类型替换为 `RoundButton`,并设置相应的属性即可。 这样,通过自定义控件类或者类似的方法,就可以实现 WinForm 圆角控件的效果。 <span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [将任意WinForm控件裁剪成圆形控件/圆角控件](https://download.csdn.net/download/lgj123xj/85454156)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [c#winform如何画圆角控件,看完这篇文章你就会了](https://blog.csdn.net/qq_35620884/article/details/121367379)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Winform控件优化之圆角1](https://blog.csdn.net/m0_56743332/article/details/129003352)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值