C# Winform中制作精美控件(2)

2024-06-23 12:52

本文主要是介绍C# Winform中制作精美控件(2),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

仓库温度监控系统重有个控件,就是温度监控,还是比较精美的,那么我们来看看制作的要点有哪些。

前面我们讨论过布局和圆角按钮。这节主要关注温度计控件

1. 布局: 两个Panel将界面分位上下两个部分,Dock.Top  Dock.Fill分别设置给他们。

2.  温度按钮采用的有自定义的圆角按钮

3. 温度计控件

从设置部分,我们可以看到最重要的几个属性是:

Value=10

MaxValue=50    

MercuryColor 水银颜色

GlassTubeColor 玻璃管颜色

// 
// uTemperValue
// 
this.uTemperValue.BMaxValue = new decimal(new int[] {
30,
0,
0,
0});
this.uTemperValue.BMinValue = new decimal(new int[] {
0,
0,
0,
0});
this.uTemperValue.Font = new System.Drawing.Font("宋体", 6.6F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.uTemperValue.ForeColor = System.Drawing.Color.White;
this.uTemperValue.GlassTubeColor = System.Drawing.Color.FromArgb(((int)(((byte)(211)))), ((int)(((byte)(211)))), ((int)(((byte)(211)))));
this.uTemperValue.LeftTemperatureUnit = STMS.STMSApp.UControls.UThermometer.TemperatureUnit.C;
this.uTemperValue.Location = new System.Drawing.Point(254, 6);
this.uTemperValue.MaxValue = new decimal(new int[] {
50,
0,
0,
0});
this.uTemperValue.MercuryColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(77)))), ((int)(((byte)(59)))));
this.uTemperValue.MinValue = new decimal(new int[] {
0,
0,
0,
0});
this.uTemperValue.Name = "uTemperValue";
this.uTemperValue.Size = new System.Drawing.Size(79, 150);
this.uTemperValue.SpCount = 5;
this.uTemperValue.TabIndex = 3;
this.uTemperValue.Value = new decimal(new int[] {
10,
0,
0,
0});
this.uTemperValue.ValueColor = System.Drawing.Color.White;
this.uTemperValue.ValueFont = new System.Drawing.Font("宋体", 7.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
// 
// SRTemperLight
// 
this.SRTemperLight.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(137)))), ((int)(((byte)(201)))), ((int)(((byte)(151)))));
this.SRTemperLight.Location = new System.Drawing.Point(110, 117);
this.SRTemperLight.Name = "SRTemperLight";
this.SRTemperLight.Size = new System.Drawing.Size(20, 20);
this.SRTemperLight.TabIndex = 2;

4. 温度计控件源码

public partial class UThermometer : UserControl
{public UThermometer(){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);this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;this.Size = new Size(60, 200);}private Color glassTubeColor = Color.FromArgb(211, 211, 211);[Description("玻璃管颜色"), Category("自定义")]public Color GlassTubeColor{get { return glassTubeColor; }set{glassTubeColor = value;Refresh();}}private Color mercuryColor = Color.FromArgb(255, 77, 59);[Description("水银颜色"), Category("自定义")]public Color MercuryColor{get { return mercuryColor; }set{mercuryColor = value;Refresh();}}private decimal minValue = 0;[Description("最小值"), Category("自定义")]public decimal MinValue{get { return minValue; }set{minValue = value;Refresh();}}private decimal maxValue = 50;[Description("最大值"), Category("自定义")]public decimal MaxValue{get { return maxValue; }set{maxValue = value;Refresh();}}private decimal mValue = 10;[Description("刻度值"), Category("自定义")]public decimal Value{get { return mValue; }set{mValue = value;Refresh();}}private int spCount = 6;[Description("分隔份数"), Category("自定义")]public int SpCount{get { return spCount; }set{if (value <= 0)return;spCount = value;Refresh();}}[Description("获取或设置控件显示的文字的字体"), Category("自定义")]public override Font Font{get{return base.Font;}set{base.Font = value;Refresh();}}[Description("获取或设置控件的文字及刻度颜色"), Category("自定义")]public override System.Drawing.Color ForeColor{get{return base.ForeColor;}set{base.ForeColor = value;Refresh();}}private Color valueColor = Color.White;[Description("温度值的文本颜色"), Category("自定义")]public Color ValueColor{get{return valueColor;}set{valueColor = value;Refresh();}}private Font valueFont = new Font("宋体", 10);public Font ValueFont{get{return valueFont;}set{valueFont = value;Refresh();}}private TemperatureUnit leftTemperatureUnit = TemperatureUnit.C;[Description("左侧刻度单位,不可为none"), Category("自定义")]public TemperatureUnit LeftTemperatureUnit{get { return leftTemperatureUnit; }set{if (value == TemperatureUnit.None)return;leftTemperatureUnit = value;Refresh();}}/// <summary>/// 高温线的温度值/// </summary>public decimal bMaxValue;public decimal BMaxValue{get { return bMaxValue; }set{bMaxValue = value;Refresh();}}/// <summary>///低温线的温度值/// </summary>public decimal bMinValue;public decimal BMinValue{get { return bMinValue; }set{bMinValue = value;Refresh();}}Rectangle m_rectWorking; //工作区Rectangle m_rectLeft;//刻度区域/// <summary>/// 温度计单位/// </summary>public enum TemperatureUnit{/// <summary>/// 不显示/// </summary>None,/// <summary>/// 摄氏度/// </summary>C,/// <summary>/// 华氏度/// </summary>F,/// <summary>/// 开氏度/// </summary>K,/// <summary>/// 兰氏度/// </summary>R,/// <summary>/// 列氏度/// </summary>Re}private void UThermometer_SizeChanged(object sender, EventArgs e){m_rectWorking = new Rectangle(this.Width / 2 - this.Width / 8, this.Width / 4, this.Width / 4, this.Height - this.Width / 2);m_rectLeft = new Rectangle(this.Width / 2 - this.Width / 8, m_rectWorking.Top + m_rectWorking.Width / 2, (this.Width - this.Width / 4) / 2 - 2, m_rectWorking.Height - m_rectWorking.Width * 2);}/// <summary>/// 画温度计/// </summary>/// <param name="e"></param>protected override void OnPaint(PaintEventArgs e){base.OnPaint(e);var g = e.Graphics;g.SmoothingMode = SmoothingMode.HighQuality;//玻璃管GraphicsPath path = new GraphicsPath();path.AddLine(m_rectWorking.Left, m_rectWorking.Bottom, m_rectWorking.Left, m_rectWorking.Top + m_rectWorking.Width / 2);path.AddArc(new Rectangle(m_rectWorking.Left, m_rectWorking.Top, m_rectWorking.Width, m_rectWorking.Width), 180, 180);path.AddLine(m_rectWorking.Right, m_rectWorking.Top + m_rectWorking.Width / 2, m_rectWorking.Right, m_rectWorking.Bottom);path.CloseAllFigures();g.FillPath(new SolidBrush(glassTubeColor), path);//底部var rectDi = new Rectangle(this.Width / 2 - m_rectWorking.Width, m_rectWorking.Bottom - m_rectWorking.Width - 2, m_rectWorking.Width * 2, m_rectWorking.Width * 2);g.FillEllipse(new SolidBrush(glassTubeColor), rectDi);g.FillEllipse(new SolidBrush(mercuryColor), new Rectangle(rectDi.Left + 4, rectDi.Top + 4, rectDi.Width - 8, rectDi.Height - 8));//值float fltHeightValue = (float)(Value / (maxValue - minValue) * m_rectLeft.Height);RectangleF rectValue = new RectangleF(m_rectWorking.Left + 4, m_rectLeft.Top + (m_rectLeft.Height - fltHeightValue), m_rectWorking.Width - 8, fltHeightValue + (m_rectWorking.Height - m_rectWorking.Width / 2 - m_rectLeft.Height));g.FillRectangle(new SolidBrush(mercuryColor), rectValue);////刻度decimal decSplit = (maxValue - minValue) / spCount;decimal decSplitHeight = m_rectLeft.Height / spCount;for (int i = 0; i <= spCount; i++){g.DrawLine(new Pen(new SolidBrush(ForeColor), 1), new PointF(m_rectLeft.Left + 1, (float)(m_rectLeft.Bottom - decSplitHeight * i)), new PointF(m_rectLeft.Left + 8, (float)(m_rectLeft.Bottom - decSplitHeight * i)));var valueLeft = (minValue + decSplit * i).ToString("0.#");var sizeLeft = g.MeasureString(valueLeft, Font);g.DrawString(valueLeft, Font, new SolidBrush(ForeColor), new PointF(m_rectLeft.Left - sizeLeft.Width-2, m_rectLeft.Bottom - (float)decSplitHeight * i - 5));if (i != spCount){if (decSplitHeight > 40){var decSp1 = decSplitHeight / 10;for (int j = 1; j < 10; j++){if (j == 5){g.DrawLine(new Pen(new SolidBrush(ForeColor), 1), new PointF(m_rectLeft.Left + 1, (m_rectLeft.Bottom - (float)decSplitHeight * i - ((float)decSp1 * j))), new PointF(m_rectLeft.Left + 8, (m_rectLeft.Bottom - (float)decSplitHeight * i - ((float)decSp1 * j))));}else{g.DrawLine(new Pen(new SolidBrush(ForeColor), 1), new PointF(m_rectLeft.Left + 1, (m_rectLeft.Bottom - (float)decSplitHeight * i - ((float)decSp1 * j))), new PointF(m_rectLeft.Left + 5, (m_rectLeft.Bottom - (float)decSplitHeight * i - ((float)decSp1 * j))));}}}else if (decSplitHeight > 10){g.DrawLine(new Pen(new SolidBrush(ForeColor), 1), new PointF(m_rectLeft.Left + 1, (m_rectLeft.Bottom - (float)decSplitHeight * i - (float)decSplitHeight / 2)), new PointF(m_rectLeft.Left + 5, (m_rectLeft.Bottom - (float)decSplitHeight * i - (float)decSplitHeight / 2)));}}}//单位string strLeftUnit = GetUnitChar(leftTemperatureUnit);g.DrawString(strLeftUnit, Font, new SolidBrush(ForeColor), new PointF(m_rectLeft.Left + 2, 5));float bmaxValue = (float)(BMaxValue / (maxValue - minValue) * m_rectLeft.Height);float bminValue = (float)(BMinValue / (maxValue - minValue) * m_rectLeft.Height);//低温线g.DrawLine(new Pen(new SolidBrush(Color.Blue), 1), m_rectWorking.Left + 2, m_rectLeft.Top + (m_rectLeft.Height - bminValue), m_rectWorking.Left + m_rectWorking.Width, m_rectLeft.Top + (m_rectLeft.Height - bminValue));//高温线g.DrawLine(new Pen(new SolidBrush(Color.Orange), 1), m_rectWorking.Left + 2, m_rectLeft.Top + (m_rectLeft.Height - bmaxValue), m_rectWorking.Left + m_rectWorking.Width, m_rectLeft.Top + (m_rectLeft.Height - bmaxValue));var sizeValue = g.MeasureString(mValue.ToString("0.##"), ValueFont);g.DrawString(mValue.ToString("0.##"), ValueFont, new SolidBrush(ValueColor), new PointF(rectDi.Left + (rectDi.Width - sizeValue.Width) / 2, rectDi.Top + (rectDi.Height - sizeValue.Height) / 2 + 1));}private string GetUnitChar(TemperatureUnit unit){string strUnit = "℃";switch (unit){case TemperatureUnit.C:strUnit = "℃";break;case TemperatureUnit.F:strUnit = "℉";break;case TemperatureUnit.K:strUnit = "K";break;case TemperatureUnit.R:strUnit = "°R";break;case TemperatureUnit.Re:strUnit = "°Re";break;}return strUnit;}
}partial class UThermometer
{/// <summary> /// 必需的设计器变量。/// </summary>private System.ComponentModel.IContainer components = null;/// <summary> /// 清理所有正在使用的资源。/// </summary>/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>protected override void Dispose(bool disposing){if (disposing && (components != null)){components.Dispose();}base.Dispose(disposing);}#region 组件设计器生成的代码/// <summary> /// 设计器支持所需的方法 - 不要修改/// 使用代码编辑器修改此方法的内容。/// </summary>private void InitializeComponent(){this.SuspendLayout();// // UThermometer// this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;this.Name = "UThermometer";this.Size = new System.Drawing.Size(67, 230);this.SizeChanged += new System.EventHandler(this.UThermometer_SizeChanged);this.ResumeLayout(false);}#endregion
}

这篇关于C# Winform中制作精美控件(2)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1087209

相关文章

C# 中变量未赋值能用吗,各种类型的初始值是什么

对于一个局部变量,如果未赋值,是不能使用的 对于属性,未赋值,也能使用有系统默认值,默认值如下: 对于 int 类型,默认值是 0;对于 int? 类型,默认值是 null;对于 bool 类型,默认值是 false;对于 bool? 类型,默认值是 null;对于 string 类型,默认值是 null;对于 string? 类型,哈哈,没有这种写法,会出错;对于 DateTime 类型,默

C#中,decimal类型使用

在Microsoft SQL Server中numeric类型,在C#中使用的时候,需要用decimal类型与其对应,不能使用int等类型。 SQL:numeric C#:decimal

XMG xib中不属于一个类的控件,拖线到指定的类中

1.比如我现在有一个view绑定为GreenView,我们按住control向类里面拖线的方式想要达到目的,显然拖不进去。例图如下 那么我们此时还想要达到目的,就需要自己去GreenView的类内部去写IBo 然后这面连接起来 2.第二,大哥郝良建给做的扩展 可以在.h或者.m中写一个NSObject的属性 然后在xib中对应的位置创建一个NSObject的属性

Avalonia 常用控件二 Menu相关

1、Menu 添加代码如下 <Button HorizontalAlignment="Center" Content="Menu/菜单"><Button.Flyout><MenuFlyout><MenuItem Header="打开"/><MenuItem Header="-"/><MenuItem Header="关闭"/></MenuFlyout></Button.Flyout></B

使用XmlPullParser制作BindView工具

在之前我写过了一个BindView的工具,之前使用的最要是正则表达的文本分析做的。最近,工作我认识了Android的XML解析,我又想起了这个问题。发现这个问题,其实用XmlPullParser更好解决。所以我重新写了这个工具。简单多了,而且不用格式化代码。 先分析一下如何写,简易思路如下 Created with Raphaël 2.1.0 输入文本路径 读取x

算法与数据结构面试宝典——回溯算法详解(C#,C++)

文章目录 1. 回溯算法的定义及应用场景2. 回溯算法的基本思想3. 递推关系式与回溯算法的建立4. 状态转移方法5. 边界条件与结束条件6. 算法的具体实现过程7. 回溯算法在C#,C++中的实际应用案例C#示例C++示例 8. 总结回溯算法的主要特点与应用价值 回溯算法是一种通过尝试各种可能的组合来找到所有解的算法。这种算法通常用于解决组合问题,如排列、组合、棋盘游

C# 命名管道中客户端访问服务器时,出现“对路径的访问被拒绝”

先还原一下我出现错误的情景:我用C#控制台写了一个命名管道服务器,然后用ASP.NET写了一个客户端访问服务器,运行之后出现了下面的错误: 原因:服务器端的访问权限不够,所以是服务器端的问题,需要增加访问权限。(网上很多都说是文件夹的权限不够,情况不同,不适用于我这种情况) 解决办法: (1)在服务器端相应地方添加以下代码。 PipeSecurity pse = new PipeSec

3_创建Tab控件

1,新建MFC 对话框项目,为对话框添加Tab控件,选中Tab控件,新建控件变量m_tab_ctrl 2,为Tab控件添加tab项 m_tab_ctrl.InsertItem(0, L”000”),参数1,哪个位置;参数2,item的名称 3,为Tab控件添加监听事件, void C测试Dlg::OnTcnSelchangeTab1(NMHDR *pNMHDR, LRESUL

如何通过示例将旧版 C# 转换为 C# 12

随着 C# 的不断发展,每个新版本都会引入强大的新功能,从而提高语言的功能和可读性。通过从旧版本的 C# 迁移到 C# 12,您可以获得更高效、更易于维护和更具表现力的代码。 由于代码库遗留、公司限制以及对旧语言功能的熟悉,许多开发人员仍在使用旧版本的 C#。升级似乎很困难,但现代版本的 C# 具有显著的优势,例如更好的性能、增强的功能和更高的安全性。 通过增量重构、试点项目和团队培训逐步

C# 日志框架Serilog使用

1、框架和说明        C#日志框架Serilog支持多种场景输出,简单验证了一下,比较方便        包的安装,推荐直接使用“推荐NuGet包管理器”安装Serilog.AspNetCore,常见的组件都已经集成在一个包中,使用比较方便 2、配置文件        Serilog可以由配置文件来定义行为,而且配置文件的修改即时生效。参考配置文件如下: {"Serilog":