陈程的技术博客

  • 关于作者
全栈软件工程师
一个专注于技术研究创新的程序员
  1. 首页
  2. .NET
  3. 正文

DreamSkin自定义美化控件-RoundButton圆角矩形按钮控件

2016年7月13日 568点热度 0人点赞 0条评论

RoundButton圆角矩形控件

 

控件圆角可以控制,动态,颜色,文字全部采用配置的方法

属性:

 属性名称 属性说明
SkinFillColor  按钮填充颜色
SkinFrameColor  按钮边框颜色
SkinButtonBackColor  按钮除外的背景色,需要与按钮的背景颜色一致,默认为白色
SkinHoverColor  鼠标移动上面渐变颜色
SkinInnerTextFont  显示文字字体
SkinInnerTextForeColor  显示文字颜色
SkinInnerTextAlignment  水平方向文字对齐标准
SkinInnerTextLineAlignment  垂直方向文字对齐标准
SkinInnerText  显示文字
SkinCornerRadius  按钮圆角角度,默认为8

 

相关源码:

Graphics g;
     private Color SaveFillColor;
     private Color SaveFrameColor;

     public RoundButton()
     {
         InitializeComponent();
         this.MouseHover += BuyButton_MouseHover;
         this.MouseLeave += BuyButton_MouseLeave;
     }

     private void RoundButton_Load(object sender, EventArgs e)
     {
         SaveFillColor = SkinFillColor;
         SaveFrameColor = SkinFrameColor;

         if (SkinInnerTextFont == null)
             SkinInnerTextFont = this.Font;
     }

     private void RoundButton_Paint(object sender, PaintEventArgs e)
     {
         g = e.Graphics;
         Draw();
     }

     /// <summary>
     /// 绘制Button颜色和文字
     /// </summary>
     private void Draw()
     {
         g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
         Brush backBrush = new SolidBrush(SkinButtonBackColor);
         g.FillRectangle(backBrush, new Rectangle(0, 0, this.Width, this.Height));
         FillRoundRectangle(g, SkinFillColor, new Rectangle(0, 0, this.Width - 1, this.Height - 1), SkinCornerRadius);
         Brush brush = new SolidBrush(SkinFrameColor);
         DrawRoundRectangle(g, new Pen(brush, 1), new Rectangle(0, 0, this.Width - 1, this.Height - 1), 8);
         if (!String.IsNullOrEmpty(this.SkinInnerText))
         {
             StringFormat sf = new StringFormat();
             sf.Alignment = SkinInnerTextAlignment;
             sf.LineAlignment = SkinInnerTextLineAlignment;

             Brush writeBrush = new SolidBrush(innerTextForeColor);
             g.DrawString(this.SkinInnerText, SkinInnerTextFont, writeBrush, this.ClientRectangle, sf);
         }
     }

     /// <summary>
     /// 鼠标移动到控件上方
     /// </summary>
     void BuyButton_MouseHover(object sender, EventArgs e)
     {
         g = this.CreateGraphics();
         SkinFillColor = SkinHoverColor;
         SkinFrameColor = SkinHoverColor;
         Draw();
     }

     /// <summary>
     /// 鼠标移出到控件上方
     /// </summary>
     void BuyButton_MouseLeave(object sender, EventArgs e)
     {
         g = this.CreateGraphics();
         SkinFillColor = SaveFillColor;
         SkinFrameColor = SaveFrameColor;
         Draw();
     }

     private void RoundButton_MouseDown(object sender, MouseEventArgs e)
     {
         g = this.CreateGraphics();
         SkinFrameColor = SaveFillColor;
         Draw();
     }

     private void RoundButton_MouseUp(object sender, MouseEventArgs e)
     {
         g = this.CreateGraphics();
         SkinFrameColor = SkinHoverColor;
         Draw();
     }

     #region 绘制圆角矩形
     private static void DrawRoundRectangle(Graphics g, Pen pen, Rectangle rect, int cornerRadius)
     {
         using (GraphicsPath path = CreateRoundedRectanglePath(rect, cornerRadius))
         {
             g.DrawPath(pen, path);
         }
     }
     private static void FillRoundRectangle(Graphics g, Color color, Rectangle rect, int cornerRadius)
     {
         using (GraphicsPath path = CreateRoundedRectanglePath(rect, cornerRadius))
         {
             Brush brush = new SolidBrush(color);
             g.FillPath(brush, path);
         }
     }
     internal static GraphicsPath CreateRoundedRectanglePath(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;
     }

     #endregion

 

标签: C# winform
最后更新:2021年4月1日

博主

全栈工程师,侧重项目技术解决方案规划和开发

打赏 点赞
< 上一篇
下一篇 >

文章评论

取消回复

分类
  • .NET (65)
  • docker (3)
  • linux (12)
  • python (20)
  • web (14)
  • 小程序 (4)
  • 数据库 (2)
  • 未分类 (4)
  • 杂七杂八 (10)
标签聚合
linux C# centos python js winform DevExpress nginx
最新 热点 随机
最新 热点 随机
.NET开发手册标准参考 招募兼职前端开发 Centos安装dotnet6环境 VS上切换分支,vs编译运行出现bug,A fatal error was encountered彻底解决方案 用C#封装一个线程安全的缓存器,达到目标定时定量更新入库 C#通过特性的方式去校验指定数据是否为空
小程序-云开发 获取用户的openid等信息 C# 一个发送邮件的帮助类 机器学习目标检测之印章检测和分类 提供一个.net的讯飞在线语音合成SDK paddleX 模型标注后数据集转换 DevExpress控件-使用LookUpEdit控件

COPYRIGHT © 2021 陈程的技术博客. ALL RIGHTS RESERVED.

THEME KRATOS MADE BY VTROIS