官網
前提
入行已經7,8年了,一直想做一套漂亮點的自定義控件,於是就有了本系列文章。
GitHub:https://github.com/kwwwvagaa/NetWinformControl
碼雲:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
如果覺得寫的還行,請點個 star 支持一下吧
來都來了,點個【推薦】再走吧,謝謝
NuGet
Install-Package HZH_Controls
目錄
http://www.hzhcontrols.com/blog-63.html
用處及效果
準備工作
之前的瓶子是朝下的,這裏擴展一下 朝上
開始
增加一個屬性
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)); } } |
最後的話
如果你喜歡的話,請到 https://gitee.com/kwwwvagaa/net_winform_custom_control 點個星星吧