winform自定义控件---UCWaterTank水池

UCWaterTank水池

水池控件一一派生 于UserControl,用户控件
用作储水池或供水池效果呈现
创建过程:
1.设置控件样式:
2.属性扩展: Value 当前水位值
ValueColor水位深度颜色
BorderColor 边框颜色
RectWidth边框粗细
MaxValue 最大水位值
3.绘制外观:边框水位深度

public partial class UCWaterTank : UserControl
    {
        public UCWaterTank()
        {
            InitializeComponent();
            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);
            RectWidth = 2;
            this.Size = new Size(100, 200);         
        }
        float f=1.0f;
        float fHeight = 0.0f;
        private RectangleF valueRect;

        int m_value;
        [Description("当前水位值"), Category("自定义")]
        public int Value
        {
            set
            {
                if (value > m_maxValue)
                    m_value = m_maxValue;
                else if (value < 0)
                    m_value = 0;
                else
                    m_value = value;
                UpdateHeight();
                Refresh();
            }
            get
            {
                return m_value;
            }
        }

        private int m_maxValue = 100;

        [Description("最大值"), Category("自定义")]
        public int MaxValue
        {
            get { return m_maxValue; }
            set
            {
                if (value < m_value)
                    m_maxValue = m_value;
                else
                    m_maxValue = value;
                UpdateHeight();
                Refresh();
            }
        }    

        [Description("值颜色"), Category("自定义")]
        private Color valueColor = Color.Blue;
        public Color ValueColor
        {
            get { return valueColor; }
            set
            {
                valueColor = value;
       
                Refresh();
            }
        }

        [Description("边框宽度"), Category("自定义")]
        private int rectWidth = 1;
        public  int RectWidth
        {
            get
            {
                return rectWidth;
            }
            set
            {
                rectWidth = value;
                UpdateHeight();
                Refresh();
            }
        }

        [Description("边框颜色"), Category("自定义")]
        private Color borderColor = Color.Blue;
        public Color BorderColor
        {
            get { return borderColor; }
            set
            {
                borderColor = value;
                Refresh();
            }
        }

        /// <summary>
        /// 更新比例系数和实际水位矩形高度
        /// </summary>
        private void UpdateHeight()
        {
            f = (float)(this.Height- 2 * RectWidth) / (float)MaxValue;
            //当前水位高度
            fHeight = (float)this.Value * f;
            //水位实际值的矩形
            valueRect = new RectangleF(this.RectWidth, (this.Height - fHeight-rectWidth), this.Width - 2 * RectWidth-1, fHeight );
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            UpdateHeight();
        }

        /// <summary>
        /// 重绘
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            //呈现质量
            g.SmoothingMode = SmoothingMode.AntiAlias;
            //Pen对象创建
            Pen pen = new Pen(new SolidBrush(BorderColor), (float)RectWidth);
            //水池边框矩形
            Rectangle rect = new Rectangle(0, 0, this.Width-1, this.Height-1);
            //圆角矩形路径 
            GraphicsPath rectpath = PaintClass.GetRoundRectangle(rect, 5);
            //画边线
        
            //填充水位
            g.FillRectangle(new SolidBrush(ValueColor), valueRect);
            g.DrawPath(pen, rectpath);
        }
    }
WinForms中的自定义控件开发允许开发者创建具有特定功能的控件,这些控件可以用于多种应用程序。仪表盘控件是一种常见的自定义控件,它模拟物理仪表盘,用于显示从简单到复杂的各种数据。在C#开发WinForms仪表盘控件通常涉及以下几个步骤: 1. **创建控件类**:首先,你需要继承自`UserControl`类,创建一个新的类,这个类将作为基础来定义你的仪表盘控件。 2. **设计界面**:在类中,使用设计器或代码来绘制控件的用户界面。这可能包括刻度、指针和数据标签等元素。 3. **编写业务逻辑**:实现控件的数据绑定和逻辑处理,这可能包括如何读取和显示数据,以及如何响应用户的交互。 4. **属性和事件**:定义公共属性来获取和设置控件的外观和行为,如颜色、范围等,并且创建事件以允许外部代码响应特定的用户操作,如值改变等。 5. **测试和调试**:在完成开发后,需要对控件进行彻底的测试,确保在不同情况下都能正确工作。 以下是一个简单的示例代码,展示了如何创建一个基础的仪表盘控件: ```csharp using System; using System.Drawing; using System.Windows.Forms; public class DashboardControl : UserControl { private float value = 0; // 仪表盘的当前值 // 公共属性,允许外部设置仪表盘值 public float Value { get { return value; } set { if (value != this.value) { this.value = value; Invalidate(); // 重绘控件 } } } // 绘制控件 protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Graphics g = e.Graphics; // 绘制仪表盘的刻度和指针等 // ... } // 其他方法,如响应用户事件等 } ``` 开发自定义控件是一个复杂的过程,它需要深入了解WinForms框架、GDI+绘图以及可能涉及的动画和数据绑定技术。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值