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

官网

http://www.hzhcontrols.com/

前提

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

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

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

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

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

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

NuGet

Install-Package HZH_Controls

目录

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

用处及效果

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));
                }
            }

最后的话

如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧

posted @ 2020-05-15 21:14  冰封一夏  阅读(693)  评论(0编辑  收藏
HZHControls控件库官网:http://hzhcontrols.com