C#控件开发-自定义控件
一、第二种开发方式
前两篇文章讲到GDI编程基础和承继控件开发,本篇讲控件开发的第二种方式:自定义开发。
二、设计类
设计一个可以展示数值的黑色小面板,并且能够设定上下阈值。这就需要在定义类的时候,定义相应的字段,属性。本篇还未涉及到控件的自定义事件。
在Winform项目中【右键】->【添加】->【类】,取名为BlackPanel,让它继承自Control类。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
/// <summary>
/// :Control 继承自Control类
/// </summary>
public class BlackPanel : Control
{
/// <summary>
/// 构造函数
/// </summary>
public BlackPanel()
{
this.SetStyle(ControlStyles.SupportsTransparentBackColor |
ControlStyles.UserPaint |
ControlStyles.ResizeRedraw |
ControlStyles.DoubleBuffer, true);
//Opaque意思是不透明,这样设置就是让它支持透明
this.SetStyle(ControlStyles.Opaque, false);
// 黑底
this.BackColor = Color.Black;
// 白字
this.ForeColor = Color.White;
}
/// <summary>
/// IL判断 1-范围值 0-非范围值
/// </summary>
public int Type { get; set; }
/// <summary>
/// il1的下限
/// Description标注内容体现在属性面板里,封装后开发者可在属性在板里看到该属性内容
/// </summary>
[Description("设置输出端的IL值")]
public float IL1Lit { get; set; }
/// <summary>
/// il1上限
/// </summary>
public float IL1Big { get; set; }
/// <summary>
/// 均匀性
/// </summary>
public float ULLit { get; set; }
public float ULBig { get; set; }
private int wave = 1;
/// <summary>
/// 第几波长
/// </summary>
public int Wave { get => wave; set => wave = value; }
private int _ch = 1;
/// <summary>
/// 设置输出端
/// </summary>
public int Channel
{
set
{
_ch = value;
this.Invalidate();
}
get
{
return _ch;
}
}
private bool _pass = false;
/// <summary>
/// 是否合格
/// </summary>
public bool Pass
{
get { return _pass; }
}
private float _il = 0.0f;
/// <summary>
/// 输出端id的IL值
/// 判断这个端口输出是否合格
/// </summary>
[Description("设置输出端的IL值")]
[Category("外观")]
public float IL
{
set
{
_il = value;
// 要输出的内容,格式化显示
_content = string.Format("IL{0}_{1}:{2:f}", wave, _ch, _il);
if (_ch == 1 && _il <= IL1Big && _il >= IL1Lit)
{
// 端口1 非范围指标
this.BackColor = Color.Black;
this._pass = true;
}else
{
this.BackColor = Color.Red;
this._pass = false;
}
this.Invalidate();
}
get
{
return _il;
}
}
private String _content = "";
public override String Text
{
set
{
_content = value;
}
get { return string.Format("IL{0}_{1}:{2:f}", wave, _ch, _il); }
}
/// <summary>
/// 重写绘图事件
/// </summary>
/// <param name="e"></param>
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
// 从参数里取到Graphics类,用它的函数绘图
Graphics g = e.Graphics;
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.High;
using (SolidBrush sb = new SolidBrush(this.BackColor))
{
// 以下调用绘图函数画背景和数值
g.FillRectangle(sb, this.ClientRectangle);
sb.Color = this.ForeColor;
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
Rectangle stringRectangle = this.ClientRectangle;
g.DrawString(_content, new Font("宋体", 20, FontStyle.Bold), sb, this.ClientRectangle, sf);
}
}
}
}
三、编译使用
build整个项目,切换到窗体设计界面,打开左侧工具箱在最上面会出现刚开发的BlackPanel控件,像其它控件使用一样拖到界面上。
1) 设置下限2,上限10,数值8。
2)设置下限2,上限10,数值12。
四、结语
Winform开发上位机,可用自定义控件方式开发一些数据显示控件,代码控制显示效果。这是作者开发一款系统测试软件用到的控件,操作人可以直观的看到哪个地方标红,特别是一堆数要显示的时候。