前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >(八十七)c#Winform自定义控件-朝上的瓶子

(八十七)c#Winform自定义控件-朝上的瓶子

作者头像
冰封一夏
发布2020-05-26 23:59:01
4430
发布2020-05-26 23:59:01
举报

官网

http://www.hzhcontrols.com/

前提

入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。

GitHub:https://github.com/kwwwvagaa/NetWinformControl

码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

如果觉得写的还行,请点个 star 支持一下吧

欢迎前来交流探讨: 企鹅群568015492

企鹅群568015492
企鹅群568015492

来都来了,点个【推荐】再走吧,谢谢

NuGet

代码语言:javascript
复制
Install-Package HZH_Controls

目录

http://www.hzhcontrols.com/blog-63.html

用处及效果

image.png
image.png

准备工作

之前的瓶子是朝下的,这里扩展一下 朝上

(五十六)c#Winform自定义控件-瓶子(工业)

开始

增加一个属性

private Direction direction = Direction.Down; [Description("瓶子方向,默认朝下"), Category("自定义")] public Direction Direction { get { return direction; } set { direction = value; Refresh(); } }

重绘里面判断朝上的代码

else { //写文字 var size = g.MeasureString(title, Font); g.DrawString(title, Font, new SolidBrush(ForeColor), new PointF((this.Width - size.Width) / 2, this.Height - size.Height - 2)); //画空瓶子 GraphicsPath pathPS = new GraphicsPath(); Point[] psPS = new Point[] { new Point(m_workingRect.Left + m_workingRect.Width / 4, m_workingRect.Top), new Point(m_workingRect.Right - 1- m_workingRect.Width / 4, m_workingRect.Top), new Point(m_workingRect.Right - 1, m_workingRect.Top + 15), new Point(m_workingRect.Right - 1, m_workingRect.Bottom), new Point(m_workingRect.Left , m_workingRect.Bottom), new Point(m_workingRect.Left, m_workingRect.Top + 15), }; pathPS.AddLines(psPS); pathPS.CloseAllFigures(); g.FillPath(new SolidBrush(bottleColor), pathPS); //画液体 decimal decYTHeight = (m_value / maxValue) * m_workingRect.Height; GraphicsPath pathYT = new GraphicsPath(); Rectangle rectYT = Rectangle.Empty; if (decYTHeight > m_workingRect.Height - 15) { PointF[] psYT = new PointF[] { new PointF((float)(m_workingRect.Left+(decYTHeight-(m_workingRect.Height-15)))+3,(float)(m_workingRect.Bottom-decYTHeight)), new PointF((float)(m_workingRect.Right-(decYTHeight-(m_workingRect.Height-15)))-3,(float)(m_workingRect.Bottom-decYTHeight)), new PointF(m_workingRect.Right-1, m_workingRect.Top+15), new PointF(m_workingRect.Right-1, m_workingRect.Bottom), new PointF(m_workingRect.Left, m_workingRect.Bottom), new PointF(m_workingRect.Left, m_workingRect.Top+15), }; pathYT.AddLines(psYT); pathYT.CloseAllFigures(); rectYT = new Rectangle(m_workingRect.Left + (int)(decYTHeight - (m_workingRect.Height - 15)) +1, (int)(m_workingRect.Bottom - decYTHeight - 4), m_workingRect.Width - (int)(decYTHeight - (m_workingRect.Height - 15)) * 2-2 , 10); } else { PointF[] psYT = new PointF[] { new PointF(m_workingRect.Left,(float)(m_workingRect.Bottom-decYTHeight)), new PointF(m_workingRect.Right-1,(float)(m_workingRect.Bottom-decYTHeight)), new PointF(m_workingRect.Right-1,m_workingRect.Bottom), new PointF(m_workingRect.Left,m_workingRect.Bottom), }; pathYT.AddLines(psYT); pathYT.CloseAllFigures(); rectYT = new Rectangle(m_workingRect.Left, m_workingRect.Bottom - (int)decYTHeight - 5, m_workingRect.Width, 10); } g.FillPath(new SolidBrush(liquidColor), pathYT); g.FillPath(new SolidBrush(Color.FromArgb(50, bottleMouthColor)), pathYT); //画液体面 g.FillEllipse(new SolidBrush(liquidColor), rectYT); g.FillEllipse(new SolidBrush(Color.FromArgb(50, Color.White)), rectYT); //画高亮 int intCount = m_workingRect.Width / 2 / 4; int intSplit = (255 - 100) / intCount; for (int i = 0; i < intCount; i++) { int _penWidth = m_workingRect.Width / 2 - 4 * i; if (_penWidth <= 0) _penWidth = 1; g.DrawLine(new Pen(new SolidBrush(Color.FromArgb(10, Color.White)), _penWidth), new Point(m_workingRect.Width / 2, m_workingRect.Top + 15), new Point(m_workingRect.Width / 2, m_workingRect.Bottom)); if (_penWidth == 1) break; } //画瓶底 g.FillEllipse(new SolidBrush(liquidColor), new RectangleF(m_workingRect.Left, m_workingRect.Bottom - 5, m_workingRect.Width - 2, 10)); g.FillEllipse(new SolidBrush(Color.FromArgb(50, liquidColor)), new RectangleF(m_workingRect.Left, m_workingRect.Bottom - 5, m_workingRect.Width - 2, 10)); //画瓶口 g.FillRectangle(new SolidBrush(bottleMouthColor), new Rectangle(m_workingRect.Left + m_workingRect.Width / 4, m_workingRect.Top - 15 + 1, m_workingRect.Width / 2, 15)); //画瓶颈阴影 GraphicsPath pathPJ = new GraphicsPath(); Point[] psPJ = new Point[] { new Point(m_workingRect.Left+m_workingRect.Width/4, m_workingRect.Top), new Point(m_workingRect.Right-1-m_workingRect.Width/4, m_workingRect.Top), new Point(m_workingRect.Right-1, m_workingRect.Top+15), new Point(m_workingRect.Left, m_workingRect.Top+15) }; pathPJ.AddLines(psPJ); pathPJ.CloseAllFigures(); g.FillPath(new SolidBrush(Color.FromArgb(50, bottleMouthColor)), pathPJ); //写编号 if (!string.IsNullOrEmpty(m_NO)) { var nosize = g.MeasureString(m_NO, Font); g.DrawString(m_NO, Font, new SolidBrush(ForeColor), new PointF((this.Width - nosize.Width) / 2, m_workingRect.Bottom - nosize.Height - 10)); } }

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-05-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 官网
  • 前提
  • NuGet
  • 目录
  • 用处及效果
  • 准备工作
  • 开始
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档