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

用处及效果

准备工作

这个也是用GDI+画的,应该算是最简单的控件了,本来不打算单独写一篇文章的,但是好歹也是个控件吧,于是就写这里了

开始

添加一个类UCPond ,继承UserControl

属性

/// <summary>
        /// The maximum value
        /// </summary>
        private decimal maxValue = 100;

        /// <summary>
        /// Gets or sets the maximum value.
        /// </summary>
        /// <value>The maximum value.</value>
        [Description("最大值"), Category("自定义")]
        public decimal MaxValue
        {
            get { return maxValue; }
            set
            {
                if (value < m_value)
                    return;
                maxValue = value;
                Refresh();
            }
        }

        /// <summary>
        /// The m value
        /// </summary>
        private decimal m_value = 0;

        /// <summary>
        /// Gets or sets the value.
        /// </summary>
        /// <value>The value.</value>
        [Description("值"), Category("自定义")]
        public decimal Value
        {
            get { return m_value; }
            set
            {
                if (value < 0)
                    return;
                if (value > maxValue)
                    m_value = maxValue;
                else
                    m_value = value;
                Refresh();
            }
        }

        /// <summary>
        /// The wall color
        /// </summary>
        private Color wallColor = Color.FromArgb(255, 77, 59);

        /// <summary>
        /// Gets or sets the color of the wall.
        /// </summary>
        /// <value>The color of the wall.</value>
        [Description("池壁颜色"), Category("自定义")]
        public Color WallColor
        {
            get { return wallColor; }
            set
            {
                wallColor = value;
                Refresh();
            }
        }

        /// <summary>
        /// The wall width
        /// </summary>
        private int wallWidth = 2;

        /// <summary>
        /// Gets or sets the width of the wall.
        /// </summary>
        /// <value>The width of the wall.</value>
        [Description("池壁宽度"), Category("自定义")]
        public int WallWidth
        {
            get { return wallWidth; }
            set
            {
                if (value <= 0)
                    return;
                wallWidth = value;
                Refresh();
            }
        }

        /// <summary>
        /// The liquid color
        /// </summary>
        private Color liquidColor = Color.FromArgb(3, 169, 243);

        [Description("液体颜色"), Category("自定义")]
        public Color LiquidColor
        {
            get { return liquidColor; }
            set { liquidColor = value; }
        }

重绘

protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            if (Height <= 0)
                return;
            var g = e.Graphics;
            g.SetGDIHigh();
            int intHeight = (int)(m_value / maxValue * this.Height);
            if (intHeight != 0)
            {
                g.FillRectangle(new SolidBrush(liquidColor), new Rectangle(0, this.Height - intHeight, this.Width, intHeight));
            }
            //划边
            g.FillRectangle(new SolidBrush(wallColor), 0, 0, wallWidth, this.Height);
            g.FillRectangle(new SolidBrush(wallColor), 0, this.Height - wallWidth, this.Width, wallWidth);
            g.FillRectangle(new SolidBrush(wallColor), this.Width - wallWidth-1, 0, wallWidth, this.Height);
        }

完整代码

// ***********************************************************************
// Assembly         : HZH_Controls
// Created          : 2019-09-06
//
// ***********************************************************************
// <copyright file="UCPond.cs">
//     Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
// </copyright>
//
// Blog: https://www.cnblogs.com/bfyx
// GitHub:https://github.com/kwwwvagaa/NetWinformControl
// gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
//
// If you use this code, please keep this note.
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;

namespace HZH_Controls.Controls
{
    /// <summary>
    /// Class UCPond.
    /// Implements the <see cref="System.Windows.Forms.UserControl" />
    /// </summary>
    /// <seealso cref="System.Windows.Forms.UserControl" />
    public class UCPond : UserControl
    {
        /// <summary>
        /// The maximum value
        /// </summary>
        private decimal maxValue = 100;

        /// <summary>
        /// Gets or sets the maximum value.
        /// </summary>
        /// <value>The maximum value.</value>
        [Description("最大值"), Category("自定义")]
        public decimal MaxValue
        {
            get { return maxValue; }
            set
            {
                if (value < m_value)
                    return;
                maxValue = value;
                Refresh();
            }
        }

        /// <summary>
        /// The m value
        /// </summary>
        private decimal m_value = 0;

        /// <summary>
        /// Gets or sets the value.
        /// </summary>
        /// <value>The value.</value>
        [Description("值"), Category("自定义")]
        public decimal Value
        {
            get { return m_value; }
            set
            {
                if (value < 0)
                    return;
                if (value > maxValue)
                    m_value = maxValue;
                else
                    m_value = value;
                Refresh();
            }
        }

        /// <summary>
        /// The wall color
        /// </summary>
        private Color wallColor = Color.FromArgb(255, 77, 59);

        /// <summary>
        /// Gets or sets the color of the wall.
        /// </summary>
        /// <value>The color of the wall.</value>
        [Description("池壁颜色"), Category("自定义")]
        public Color WallColor
        {
            get { return wallColor; }
            set
            {
                wallColor = value;
                Refresh();
            }
        }

        /// <summary>
        /// The wall width
        /// </summary>
        private int wallWidth = 2;

        /// <summary>
        /// Gets or sets the width of the wall.
        /// </summary>
        /// <value>The width of the wall.</value>
        [Description("池壁宽度"), Category("自定义")]
        public int WallWidth
        {
            get { return wallWidth; }
            set
            {
                if (value <= 0)
                    return;
                wallWidth = value;
                Refresh();
            }
        }

        /// <summary>
        /// The liquid color
        /// </summary>
        private Color liquidColor = Color.FromArgb(3, 169, 243);

        [Description("液体颜色"), Category("自定义")]
        public Color LiquidColor
        {
            get { return liquidColor; }
            set { liquidColor = value; }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="UCPond"/> class.
        /// </summary>
        public UCPond()
        {
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.Selectable, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            this.SetStyle(ControlStyles.UserPaint, true);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
            this.Size = new Size(150, 50);
        }
        /// <summary>
        /// 引发 <see cref="E:System.Windows.Forms.Control.Paint" /> 事件。
        /// </summary>
        /// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.PaintEventArgs" />。</param>
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            if (Height <= 0)
                return;
            var g = e.Graphics;
            g.SetGDIHigh();
            int intHeight = (int)(m_value / maxValue * this.Height);
            if (intHeight != 0)
            {
                g.FillRectangle(new SolidBrush(liquidColor), new Rectangle(0, this.Height - intHeight, this.Width, intHeight));
            }
            //划边
            g.FillRectangle(new SolidBrush(wallColor), 0, 0, wallWidth, this.Height);
            g.FillRectangle(new SolidBrush(wallColor), 0, this.Height - wallWidth, this.Width, wallWidth);
            g.FillRectangle(new SolidBrush(wallColor), this.Width - wallWidth-1, 0, wallWidth, this.Height);
        }
    }
}

最后的话

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

作者:冰封一夏
出处: http://www.hzhcontrols.com/doc.html
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,
且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
GitHub:https://github.com/kwwwvagaa/NetWinformControl
码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

原文出处:冰封一夏

原文链接:https://www.cnblogs.com/bfyx/p/11477241.html

本文观点不代表Dotnet9立场,转载请联系原作者。

发表评论

登录后才能评论