官网

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中的boolean类型

    Python中所有数据类型的值自带布尔值.如此多的数据类型中只需要记住只有0.None.空.False的布尔值为False,其余的为True. print(bool(0)) print(bool(No ...

  2. 重载和重写的区别?构造器 Contructor 构造器是否可被 override?

    重载 发生在同一类,方法名必须相同,参数类型不同,顺序不同,类型不同,方法返回值和返回类型可以不同 重写 发生在子父类,方法名.参数名参数列表必须相同.返回值范围小于等于父类,抛出异常范围小于等于父类 ...

  3. 人员简历管理系统-毕业设计(包括文档+源代码+答辩PPT)

    1.开发环境 开发工具:Microsoft Visual Studio 2010操作系统:Windows7(推荐)或更高版本 数据库:sql server 2000 或者更高版本Web服务器:IIS ...

  4. HTTP认知(请求与响应)

    web的工作是:浏览器发送请求报文 + 服务端返回响应报文 通俗的说一下web工作的一个流程: 浏览器向服务端发送HTTP请求报文:这条请求报文组成由请求行.请求头.请求体三大部分组成: 1.请求行 ...

  5. Android 项目优化(七):阿里巴巴Android开发手册整理总结

    本来之前觉得Android项目优化系列的文章基本整理完毕了,但是近期看了一下<阿里Android开发手册>有了很多收获,想再整理一篇,下面就开始吧. 先在这里列一下之前整理的文章及链接: ...

  6. 管道及 I/O 重定向

    I/O重定向 I/O Redirection 标准输入.标准输出.标准错误输出重定向及综合案例输入重定向及结合案例 标准输入.标准输出.标准错误 file descriptors (FD,文件描述符 ...

  7. QT获取linux下的当前用户名

    故事背景:客户端启动的时候需要加载机器/home/xx/test.jpg的图片作为背景图,但是有的机器用户名叫AAA,有的机器名叫BBB,所以我需要获取当前用户的home目录 技术调研:QStanda ...

  8. 记druid 在配置中心下的一个大坑: cpu 达到 100%

    把我们的dubbo 应用移步到配置中心上去之后,发现我们的应用过一段时间就会出现cpu 100%的情况 (大概是12个小时),一开始cpu占用是2-5% 的样子,什么都没做,后面竟然用尽了cpu.. ...

  9. mybatis中 == 和 != 的用法

    != 的用法 <if test="xxx != null and xxx !=''"> == 的用法(相较于!=,仅需将双引号和单引号的位置换一下即可) <if ...

  10. winform实现自定义折叠面板控件

    代码文件:https://github.com/Caijt/CollapsePanel 最近在学习做winform,想实现一个系统导航菜单,系统菜单以模块进行分组,菜单是树型结构. 效果类似旧版QQ的 ...