官网

http://www.hzhcontrols.com

前提

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

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

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

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

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

麻烦博客下方点个【推荐】,谢谢

NuGet

Install-Package HZH_Controls

目录

https://www.cnblogs.com/bfyx/p/11364884.html

用处及效果

准备工作

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

开始

添加一个类UCPond ,继承UserControl

属性

  /// <summary>
/// The maximum value
/// </summary>
private decimal maxValue = ; /// <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 = ; /// <summary>
/// Gets or sets the value.
/// </summary>
/// <value>The value.</value>
[Description("值"), Category("自定义")]
public decimal Value
{
get { return m_value; }
set
{
if (value < )
return;
if (value > maxValue)
m_value = maxValue;
else
m_value = value;
Refresh();
}
} /// <summary>
/// The wall color
/// </summary>
private Color wallColor = Color.FromArgb(, , ); /// <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 = ; /// <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 <= )
return;
wallWidth = value;
Refresh();
}
} /// <summary>
/// The liquid color
/// </summary>
private Color liquidColor = Color.FromArgb(, , ); [Description("液体颜色"), Category("自定义")]
public Color LiquidColor
{
get { return liquidColor; }
set { liquidColor = value; }
}

重绘

  protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (Height <= )
return;
var g = e.Graphics;
g.SetGDIHigh();
int intHeight = (int)(m_value / maxValue * this.Height);
if (intHeight != )
{
g.FillRectangle(new SolidBrush(liquidColor), new Rectangle(, this.Height - intHeight, this.Width, intHeight));
}
//划边
g.FillRectangle(new SolidBrush(wallColor), , , wallWidth, this.Height);
g.FillRectangle(new SolidBrush(wallColor), , this.Height - wallWidth, this.Width, wallWidth);
g.FillRectangle(new SolidBrush(wallColor), this.Width - wallWidth-, , 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 = ; /// <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 = ; /// <summary>
/// Gets or sets the value.
/// </summary>
/// <value>The value.</value>
[Description("值"), Category("自定义")]
public decimal Value
{
get { return m_value; }
set
{
if (value < )
return;
if (value > maxValue)
m_value = maxValue;
else
m_value = value;
Refresh();
}
} /// <summary>
/// The wall color
/// </summary>
private Color wallColor = Color.FromArgb(, , ); /// <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 = ; /// <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 <= )
return;
wallWidth = value;
Refresh();
}
} /// <summary>
/// The liquid color
/// </summary>
private Color liquidColor = Color.FromArgb(, , ); [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(, );
}
/// <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 <= )
return;
var g = e.Graphics;
g.SetGDIHigh();
int intHeight = (int)(m_value / maxValue * this.Height);
if (intHeight != )
{
g.FillRectangle(new SolidBrush(liquidColor), new Rectangle(, this.Height - intHeight, this.Width, intHeight));
}
//划边
g.FillRectangle(new SolidBrush(wallColor), , , wallWidth, this.Height);
g.FillRectangle(new SolidBrush(wallColor), , this.Height - wallWidth, this.Width, wallWidth);
g.FillRectangle(new SolidBrush(wallColor), this.Width - wallWidth-, , wallWidth, this.Height);
}
}
}

最后的话

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

(五十九)c#Winform自定义控件-池子(工业)-HZHControls的更多相关文章

  1. (五十)c#Winform自定义控件-滑块

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  2. (八十)c#Winform自定义控件-分割线标签-HZHControls

    官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...

  3. (五十一)c#Winform自定义控件-文字提示-HZHControls

    官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...

  4. 第三百五十九节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)介绍以及安装

    第三百五十九节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)介绍以及安装 elasticsearch(搜索引擎)介绍 ElasticSearch是一个基于 ...

  5. “全栈2019”Java第五十九章:抽象类与抽象方法详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  6. (三十)c#Winform自定义控件-文本框(三)

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  7. SpringBoot进阶教程(五十九)整合Codis

    上一篇博文<详解Codis安装与部署>中,详细介绍了codis的安装与部署,这篇文章主要介绍介绍springboot整合codis.如果之前看过<SpringBoot进阶教程(五十二 ...

  8. (二十)c#Winform自定义控件-有后退的窗体

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  9. (六十)c#Winform自定义控件-鼓风机(工业)

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

随机推荐

  1. python基础知识第九篇(函数)

    函数 >>>>>>>>>>> : 使用函数的好处 1.代码重用 2.保持一致性,方便维护 3.可扩展性 定义方法 def test01 ...

  2. 一起学Vue之入门篇

    概述 Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架.与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用.Vue 的核心库只关注视图层,不仅易于上手,还 ...

  3. JS---另一个定时器:一次性的

    之前学的定时器:setInterval和清除定时器 clearInterval(定时器id); //常用的,反复的执行 window.setInterval(function () { alert(& ...

  4. Hadoop2.8分布式集群安装与测试

    1.hadoop2.x 概述 个).每一个都有相同的职能.一个是active状态的,一个是standby状态的.当集群运行时,只有active状态的NameNode是正常工作的,standby状态的N ...

  5. Appium(七):Appium API(一) 应用操作

    1. 应用操作 本章所罗列的方法主要针对应用的操作,如应用的安装.卸载.关闭.启动等. 把前面的启动代码放在这里,后面只展示不同的部分. # coding:utf-8 from appium impo ...

  6. K 折验证

    为了在调节网络参数(比如训练的轮数)的同时对网络进行评估,你可以将数据划分为训练集和验证集.但由于数据点很少,验证集会非常小(比如大约100 个样本).因此,验证分数可能会有很大波动,这取决于你所选择 ...

  7. Redux学习及应用

    Redux学习及应用 一:Redux的来源? Redux 是 JavaScript 状态容器,提供可预测化的状态管理.Redux是由 Flux 演变而来,但受 Elm 的启发,避开了 Flux 的复杂 ...

  8. 【AGC028D】Chord

    Problem Description 给定一个圆,圆上均等地放着 \(2n\) 个点,已有 \(k\) 对点之间连好了边,从中选择剩下 \(n-k\) 对点随意连边. 求所有连边方案中,联通块的个数 ...

  9. 【Leetcode 做题学算法周刊】第五期

    首发于微信公众号<前端成长记>,写于 2019.12.06 背景 本文记录刷题过程中的整个思考过程,以供参考.主要内容涵盖: 题目分析设想 编写代码验证 查阅他人解法 思考总结 目录 10 ...

  10. 字节跳动——IT技术工程师面试题

    .自我介绍 .项目介绍 .争对个人项目进行提问 .场景模拟 .1如何知道用户的指定视频(类似于QQ发视频)的服务是正常的 .使用appum进行自动化测试 .使用bat脚本获取进程状态,然后定时发送em ...