(五十九)c#Winform自定义控件-池子(工业)-HZHControls
2024-09-06 04:41:52
官网
前提
入行已经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 点个星星吧
最新文章
- Maven学习(三)生命周期
- Django 部署
- PHP--正则表达式和样式匹配--小记
- meate 标签使用介绍
- Photoshop CS6的安装
- 硬盘安装RedHat Enterprise Linux 6(转载)
- CodeIgniter中的session处理
- Ajax跨域请求数据实例(JSOPN方式)
- 介绍一种非常好用汇总数据的方式GROUPING SETS
- Educational Codeforces Round 36 (Rated for Div. 2) E. Physical Education Lessons
- findViewById中NullPointerException的错误
- 『Tarjan算法 无向图的割点与割边』
- CKEditor 4.5 filetools, XHR.withCredentials = true,
- ajax跨域问题及相关解决方案
- drf3 Serializers 序列化组件
- Java中try catch finally语句中含有return语句的执行情况(总结版)
- django----数据库操作(对model增删改查)
- Maven 学习 -- 目录
- hdu 5091 给定矩形覆盖尽量多点 扫描线+线段树
- 重复代码检查工具simian的基本用法
热门文章
- Python面向对象-获取对象信息type()、isinstance()、dir()
- Thread.Sleep线程休眠-小白向
- SpringCloud-使用熔断器仪表盘监控熔断
- JavaScript实现动态轮播图效果
- CSS 盒子模型、RestCSS、浮动、定位
- Spring Cloud进阶之路 | 一:服务注册与发现(nacos)
- python3实现栈的逻辑
- Redisson基本用法
- C#版本与.NET版本对应关系以及各版本的特性
- Consul-template+nginx实现自动负载均衡