button垂直居中_C#控件美化之路(3):GDI+绘制一个Button控件

d1ec2e5e970acd85b4e81dea1c50c383.png

C#自定义控件Button

接上一篇文章,GDI+绘制Lable控件,此控件两个知识点。

一、绘制外形边框。(备注:可以添加一个属性是否显示边框)

二、绘制按钮文字。(备注:按钮文字水平垂直方向居中)。

上一篇文章中已经将文字绘制详细说明,当前教程,只贴部分代码。不做详细说明。可以浏览上一篇文章详细了解。C#控件美化之路自己动手绘制用GDI+绘制一个Lable控件(二)

新建一个类,继承Control。构造函数中可以设置一个默认大小

this.Size = new Size(80, 30);

声明公有属性,属性包含,显示文本,已经是否显示外边框。外边框可以用bool值,根据个人喜好修改。文中直接使用BorderStyle。具体代码可以在VS中使用F12转跳查看详情。

        #region 公有属性        public string Text { get => base.Text; set { base.Text = value; this.Invalidate(); } }        [Category("Wen"),Description("是否显示边框"),DefaultValue(BorderStyle.None)]        public BorderStyle BorderStyle { get; set; }        #endregion

接下来贴关键代码。重绘内容,依然是重写Paint。

代码片段一。绘制文本居中(其他部分代码可以在如何手绘Lable中找到方法)

            g.DrawString(this.Text,                         this.Font,                          new SolidBrush(this.ForeColor),                          rec,                         new StringFormat(StringFormatFlags.NoClip | StringFormatFlags.NoWrap)            {                LineAlignment = StringAlignment.Center,                Alignment = StringAlignment.Center,                Trimming = StringTrimming.EllipsisCharacter            });

代码片段二。绘制外边框:

            if (this.BorderStyle == BorderStyle.FixedSingle)            {                System.Drawing.Drawing2D.GraphicsPath path = CreateRoundedRectanglePath(new Rectangle(0, 0, Width - 1, Height - 1), 10);                using (Pen pen = new Pen(Color.FromArgb(153, 153, 153)))                {                    g.DrawPath(pen, path);                }            }

此方法,主要将一个矩形形状生成一个矩形圆弧形状 CreateRoundedRectanglePath 代码块如下

        ///         /// 根据矩形和圆得到一个圆角矩形Path        ///         /// The rect.        /// The corner radius.        /// GraphicsPath.        public static GraphicsPath CreateRoundedRectanglePath(this Rectangle rect, int cornerRadius)        {            GraphicsPath roundedRect = new GraphicsPath();            roundedRect.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);            roundedRect.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);            roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);            roundedRect.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);            roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);            roundedRect.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);            roundedRect.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);            roundedRect.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);            roundedRect.CloseFigure();            return roundedRect;        }

到此一个按钮形状完成。预览效果

e386277877f7fbbfe4f60359c4ee53d4.png

目前为止,按钮形状完成。一般按钮,鼠标移动到按钮位置,背景颜色会改变。所以我们可以重写鼠标事件。直接修改背景。

        #region 私有属性        private Color backColor = Color.Transparent;        #endregion

私有属性,主要记录鼠标进入控件之前的颜色,便于鼠标离开后,控件恢复之前颜色。

        protected override void OnMouseLeave(EventArgs e)        {            base.OnMouseLeave(e);            this.BackColor = this.backColor;        }        protected override void OnMouseEnter(EventArgs e)        {            base.OnMouseEnter(e);            this.backColor = this.BackColor;            this.BackColor = Color.FromArgb(63, 63, 65);        }

至此。一个按钮绘制完成。

当然本文中,鼠标事件主要用背景颜色改变实现。为了更加美化,可以是直接使用文中描述的Path路径填充绘制。具体方法有兴趣的小伙伴可以试试。

预览效果

91417c3b26db0c830cc0e5c0c6872005.gif
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以帮你绘制一个菱形的WinForms按钮件。以下是一个基本的示例代码: ```csharp using System; using System.Drawing; using System.Windows.Forms; namespace DiamondButtonExample { public partial class DiamondButton : Button { public DiamondButton() { InitializeComponent(); SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true); FlatStyle = FlatStyle.Flat; FlatAppearance.BorderSize = 0; BackColor = Color.Transparent; Size = new Size(100, 100); } protected override void OnPaint(PaintEventArgs pevent) { GraphicsPath path = new GraphicsPath(); Point[] points = { new Point(ClientRectangle.Width / 2, 0), new Point(ClientRectangle.Width, ClientRectangle.Height / 2), new Point(ClientRectangle.Width / 2, ClientRectangle.Height), new Point(0, ClientRectangle.Height / 2) }; path.AddPolygon(points); Region = new Region(path); base.OnPaint(pevent); } } } ``` 要使用这个自定义的按钮件,可以按照以下步骤进行: 1. 创建一个新的 WinForms 项目。 2. 在解决方案资源管理器中,右键单击项目,选择“添加”->“类”。 3. 将上述代码复制到新创建的类文件中。 4. 在主窗体或其他窗体上添加一个按钮件。 5. 在设计器中选择该按钮件,然后在属性窗口中找到“Modifiers”属性,并将其设置为“Public”。 6. 打开主窗体或其他窗体的代码文件,在顶部添加 `using DiamondButtonExample;`。 7. 在窗体的构造函数或加载事件中,使用如下代码创建和添加自定义的菱形按钮件: ```csharp DiamondButton diamondButton = new DiamondButton(); Controls.Add(diamondButton); ``` 现在你应该能够在你的 WinForms 项目中看到一个菱形按钮件了。你可以根据需要修改按钮的大小、颜色和其他属性。希望这能帮到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值