C#中用winform和WPF实现圆角按钮、C#中类说明

一、C#类说明

Form.Designer.cs(窗体的设计类)存放窗体的布局,你的窗体定义了哪些控件,那些控件的名字、属性等等,拖动控件后自动生成
Form.cs(窗体的逻辑类)则是用来存放自定义方法和事件

二、 winform实现圆角按钮

首先建一个新类:RoundButton

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 WindowsFormsApp1
{
        public enum ControlState { Hover, Normal, Pressed }
        public class RoundButton : Button
        {

            private int radius;//半径 
                               //private Color _borderColor = Color.FromArgb(51, 161, 224);//边框颜色
            private Color _hoverColor = Color.FromArgb(220, 80, 80);//基颜色
            private Color _normalColor = Color.FromArgb(51, 161, 224);//基颜色
            private Color _pressedColor = Color.FromArgb(251, 161, 0);//基颜色

            private ContentAlignment _textAlign = ContentAlignment.MiddleCenter;

            public override ContentAlignment TextAlign
            {
                set
                {
                    _textAlign = value;
                    this.Invalidate();
                }
                get
                {
                    return _textAlign;
                }
            }

            /// <summary>
            /// 圆角按钮的半径属性
            /// </summary>
            [CategoryAttribute("Layout"), BrowsableAttribute(true), ReadOnlyAttribute(false)]
            public int Radius
            {
                set
                {
                    radius = value;
                    // 使控件的整个画面无效并重绘控件
                    this.Invalidate();
                }
                get
                {
                    return radius;
                }
            }
            [CategoryAttribute("Appearance"), DefaultValue(typeof(Color), "51, 161, 224")]
            public Color NormalColor
            {
                get
                {
                    return this._normalColor;
                }
                set
                {
                    this._normalColor = value;
                    this.Invalidate();
                }
            }
            [CategoryAttribute("Appearance"), DefaultValue(typeof(Color), "220, 80, 80")]
            public Color HoverColor
            {
                get
                {
                    return this._hoverColor;
                }
                set
                {
                    this._hoverColor = value;
                    this.Invalidate();
                }
            }

            [CategoryAttribute("Appearance"), DefaultValue(typeof(Color), "251, 161, 0")]
            public Color PressedColor
            {
                get
                {
                    return this._pressedColor;
                }
                set
                {
                    this._pressedColor = value;
                    this.Invalidate();
                }
            }

            public ControlState ControlState { get; set; }

            protected override void OnMouseEnter(EventArgs e)//鼠标进入时
            {
                ControlState = ControlState.Hover;//Hover
                base.OnMouseEnter(e);
            }
            protected override void OnMouseLeave(EventArgs e)//鼠标离开
            {
                ControlState = ControlState.Normal;//正常
                base.OnMouseLeave(e);
            }
            protected override void OnMouseDown(MouseEventArgs e)//鼠标按下
            {
                if (e.Button == MouseButtons.Left && e.Clicks == 1)//鼠标左键且点击次数为1
                {
                    ControlState = ControlState.Pressed;//按下的状态
                }
                base.OnMouseDown(e);
            }

            protected override void OnMouseUp(MouseEventArgs e)//鼠标弹起
            {
                if (e.Button == MouseButtons.Left && e.Clicks == 1)
                {
                    if (ClientRectangle.Contains(e.Location))//控件区域包含鼠标的位置
                    {
                        ControlState = ControlState.Hover;
                    }
                    else
                    {
                        ControlState = ControlState.Normal;
                    }
                }
                base.OnMouseUp(e);
            }
            public RoundButton()
            {
                ForeColor = Color.White;
                Radius = 20;
                this.FlatStyle = FlatStyle.Flat;
                this.FlatAppearance.BorderSize = 0;
                this.ControlState = ControlState.Normal;
                this.SetStyle(
                 ControlStyles.UserPaint |  //控件自行绘制,而不使用操作系统的绘制
                 ControlStyles.AllPaintingInWmPaint | //忽略背景擦除的Windows消息,减少闪烁,只有UserPaint设为true时才能使用。
                 ControlStyles.OptimizedDoubleBuffer |//在缓冲区上绘制,不直接绘制到屏幕上,减少闪烁。
                 ControlStyles.ResizeRedraw | //控件大小发生变化时,重绘。
                 ControlStyles.SupportsTransparentBackColor, //支持透明背景颜色
                 true);
            }


            //重写OnPaint
            protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
            {
                base.OnPaint(e);
                // base.OnPaintBackground(e);

                // 尽可能高质量绘制
                e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
                e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
                e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
                e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
                e.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;

                Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
                var path = GetRoundedRectPath(rect, radius);

                this.Region = new Region(path);

                Color baseColor;
                //Color borderColor;
                //Color innerBorderColor = this._baseColor;//Color.FromArgb(200, 255, 255, 255); ;

                switch (ControlState)
                {
                    case ControlState.Hover:
                        baseColor = this._hoverColor;
                        break;
                    case ControlState.Pressed:
                        baseColor = this._pressedColor;
                        break;
                    case ControlState.Normal:
                        baseColor = this._normalColor;
                        break;
                    default:
                        baseColor = this._normalColor;
                        break;
                }

                using (SolidBrush b = new SolidBrush(baseColor))
                {
                    e.Graphics.FillPath(b, path); // 填充路径,而不是DrawPath
                    using (Brush brush = new SolidBrush(this.ForeColor))
                    {
                        // 文本布局对象
                        using (StringFormat gs = new StringFormat())
                        {
                            // 文字布局
                            switch (_textAlign)
                            {
                                case ContentAlignment.TopLeft:
                                    gs.Alignment = StringAlignment.Near;
                                    gs.LineAlignment = StringAlignment.Near;
                                    break;
                                case ContentAlignment.TopCenter:
                                    gs.Alignment = StringAlignment.Center;
                                    gs.LineAlignment = StringAlignment.Near;
                                    break;
                                case ContentAlignment.TopRight:
                                    gs.Alignment = StringAlignment.Far;
                                    gs.LineAlignment = StringAlignment.Near;
                                    break;
                                case ContentAlignment.MiddleLeft:
                                    gs.Alignment = StringAlignment.Near;
                                    gs.LineAlignment = StringAlignment.Center;
                                    break;
                                case ContentAlignment.MiddleCenter:
                                    gs.Alignment = StringAlignment.Center; //居中
                                    gs.LineAlignment = StringAlignment.Center;//垂直居中
                                    break;
                                case ContentAlignment.MiddleRight:
                                    gs.Alignment = StringAlignment.Far;
                                    gs.LineAlignment = StringAlignment.Center;
                                    break;
                                case ContentAlignment.BottomLeft:
                                    gs.Alignment = StringAlignment.Near;
                                    gs.LineAlignment = StringAlignment.Far;
                                    break;
                                case ContentAlignment.BottomCenter:
                                    gs.Alignment = StringAlignment.Center;
                                    gs.LineAlignment = StringAlignment.Far;
                                    break;
                                case ContentAlignment.BottomRight:
                                    gs.Alignment = StringAlignment.Far;
                                    gs.LineAlignment = StringAlignment.Far;
                                    break;
                                default:
                                    gs.Alignment = StringAlignment.Center; //居中
                                    gs.LineAlignment = StringAlignment.Center;//垂直居中
                                    break;
                            }
                            // if (this.RightToLeft== RightToLeft.Yes)
                            // {
                            //     gs.FormatFlags = StringFormatFlags.DirectionRightToLeft;
                            // }
                            e.Graphics.DrawString(this.Text, this.Font, brush, rect, gs);
                        }
                    }
                }
            }
            /// <summary>
            /// 根据矩形区域rect,计算呈现radius圆角的Graphics路径
            /// </summary>
            /// <param name="rect"></param>
            /// <param name="radius"></param>
            /// <returns></returns>
            private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
            {
                #region 正确绘制圆角矩形区域
                int R = radius * 2;
                Rectangle arcRect = new Rectangle(rect.Location, new Size(R, R));
                GraphicsPath path = new GraphicsPath();
                // 左上圆弧 左手坐标系,顺时针为正 从180开始,转90度
                path.AddArc(arcRect, 180, 90);
                // 右上圆弧
                arcRect.X = rect.Right - R;
                path.AddArc(arcRect, 270, 90);
                // 右下圆弧
                arcRect.Y = rect.Bottom - R;
                path.AddArc(arcRect, 0, 90);
                // 左下圆弧
                arcRect.X = rect.Left;
                path.AddArc(arcRect, 90, 90);
                path.CloseFigure();
                return path;
                #endregion
            }

            protected override void OnSizeChanged(EventArgs e)
            {
                base.OnSizeChanged(e);
            }
        }
    }


然后在Form.Designer.cs中直接使用,也可以在工具箱中直接拖拽

private RoundButton  roundButton1;

三、WPF实现圆角按钮

直接修改XAML即可
方式一:

<Button Content="圆角按钮1" Margin="479,186,124,176">
    <Button.Resources>
        <Style TargetType="{x:Type Border}">
            <Setter Property="CornerRadius" Value="40"></Setter>
        </Style>
    </Button.Resources>
</Button>

方式二:

<Button x:Name="button" Content="圆角按钮实现2" FontSize="40" BorderThickness="0" HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top" Width="472" Height="200" Foreground="Black">
    <Button.Template>
        <ControlTemplate TargetType="{x:Type Button}">
            <Border BorderThickness="1" BorderBrush="Black" CornerRadius="30" Background="{TemplateBinding Background}">
                <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Border>
        </ControlTemplate>
    </Button.Template>
</Button>

注意:
1、需要添加button 的template
2、设置border的时候,必须要设置background, 否则会提示content 被多次使用
3、属性
BorderThickness:边框的粗细,值越大越粗
BorderBrush:边框的颜色
CornerRadius:圆角的大小
Background:背景颜色"{TemplateBinding Background}":这个就是使用上面的Background属性值作为他的值
:文字垂直居中对齐
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值