当前位置: 首页 > news >正文

随笔一些用C#封装的控件

可拖动的滑块
在这里插入图片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace PSS_PXIe_DOA_IS_SOFT.UcControl
{public partial class ucTrackBar : System.Windows.Forms.Control{private float _value;private float _min = 0;private float _max = 100;private int _thumbWidth = 20;private int _thumbHeight = 20;private bool _isDragging = false;private Rectangle _thumbRect;private Rectangle _thumbRect1;public event EventHandler ValueChanged;public ucTrackBar(){this.Size = new Size(300, 50);this.DoubleBuffered = true;InitializeComponent();}public float Value{get { return _value; }set{if (value < _min) value = _min;if (value > _max) value = _max;if (_value != value){_value = value;UpdateThumbPosition();ValueChanged?.Invoke(this, EventArgs.Empty);this.Invalidate();}}}public float Minimum{get { return _min; }set{_min = value;if (_value < _min) _value = _min;UpdateThumbPosition();this.Invalidate();}}public float Maximum{get { return _max; }set{_max = value;if (_value > _max) _value = _max;UpdateThumbPosition();this.Invalidate();}}protected override void OnPaint(PaintEventArgs e){base.OnPaint(e);Font boldFont = new Font("宋体", 9, FontStyle.Bold);// 绘制刻度标记using (var pen = new Pen(Color.Black, 2)){for (float i = _min; i <= _max; i += 10){float x = (i - _min) * (this.Width - _thumbWidth) / (_max - _min);e.Graphics.DrawLine(pen, x + 10, this.Height - 35, x + 10, this.Height - 20);//数字长度为1时朝右边来一点,为2时朝左边移动一点if (i.ToString().Length == 1){e.Graphics.DrawString(i.ToString(), boldFont, Brushes.Black, x + 5, this.Height - 20);}else if (i.ToString().Length == 2){e.Graphics.DrawString(i.ToString(), boldFont, Brushes.Black, x, this.Height - 20);}}for (float i = _min; i <= _max; i += 2){if (i % 5 == 0){continue;}float x = (i - _min) * (this.Width - _thumbWidth) / (_max - _min);e.Graphics.DrawLine(pen, x + 10, this.Height - 35, x + 10, this.Height - 25);}}// 绘制背景轨道using (var brush = new SolidBrush(Color.White)){e.Graphics.FillRectangle(brush, 0, (this.Height - 40) / 2, this.Width, 3);}// 绘制倒三角滑块using (var brush = new SolidBrush(Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(143)))), ((int)(((byte)(215))))))){//Point[] trianglePoints = new Point[]//{//    new Point(_thumbRect.Left, _thumbRect.Top- _thumbHeight+3), // 左下角//    new Point(_thumbRect.Left + _thumbWidth / 2, _thumbRect.Bottom-18), // 顶部中点//    new Point(_thumbRect.Right, _thumbRect.Top- _thumbHeight+3) // 右下角//};Point[] trianglePoints = new Point[]{new Point(_thumbRect.Left, _thumbRect.Top- _thumbHeight+3), // 左下角new Point(_thumbRect.Left + _thumbWidth / 2, _thumbRect.Bottom-18), // 顶部中点new Point(_thumbRect.Right, _thumbRect.Top- _thumbHeight+3) // 右下角};e.Graphics.FillPolygon(brush, trianglePoints);}}protected override void OnMouseDown(MouseEventArgs e){base.OnMouseDown(e);//if (_thumbRect.Contains(e.Location))//{//    _isDragging = true;//}if (_thumbRect1.Contains(e.Location)){_isDragging = true;}}protected override void OnMouseMove(MouseEventArgs e){base.OnMouseMove(e);if (_isDragging){//double newValue = (e.X - _thumbWidth / 2) * (_max - _min) / (this.Width - _thumbWidth) + _min;//Value = newValue;float newValue = (e.X - _thumbWidth / 2) * (_max - _min) / (this.Width - _thumbWidth) + _min;Value = (float)Math.Round(newValue, 2); // 精确到两位小数}}protected override void OnMouseUp(MouseEventArgs e){base.OnMouseUp(e);_isDragging = false;}private void UpdateThumbPosition(){double x = (_value - _min) * (this.Width - _thumbWidth) / (_max - _min);_thumbRect = new Rectangle((int)x, (this.Height - _thumbHeight) / 2, _thumbWidth, _thumbHeight);//_thumbRect = new Rectangle((int)x, (this.Height - 40) / 2, _thumbWidth, _thumbHeight);_thumbRect1 = new Rectangle((int)x, (this.Height - 40) / 2 - 10, _thumbWidth, _thumbHeight);}}
}
namespace PSS_PXIe_DOA_IS_SOFT.UcControl
{partial class ucTrackBar{/// <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(){components = new System.ComponentModel.Container();//this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;}#endregion}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace PSS_PXIe_DOA_IS_SOFT.UcControl
{/// <summary>/// 信号灯控件 控件拖到界面上后尺寸改为20x20才会正常显示/// </summary>[Localizable(true)]public partial class ucCircle : UserControl{public ucCircle(){InitializeComponent();brush_lantern_background = new SolidBrush(color_lantern_background);}#region Private Member// 按钮的背景颜色,包括边线颜色private Color color_lantern_background = Color.LimeGreen;// 按钮的背景画刷              private Brush brush_lantern_background = null;#endregion#region Public Member/// <summary>/// 获取或设置开关按钮的背景色/// </summary>[Browsable(true)][Description("获取或设置信号灯的背景色")][Category("自定义设置")][DefaultValue(typeof(Color), "LimeGreen")][Localizable(true)]public Color LanternBackground{get{return color_lantern_background;}set{color_lantern_background = value;brush_lantern_background?.Dispose();brush_lantern_background = new SolidBrush(color_lantern_background);Invalidate();}}#endregion#region Private Methodprivate Point GetCenterPoint(){if (Height > Width){return new Point((Width - 1) / 2, (Width - 1) / 2);}else{return new Point((Height - 1) / 2, (Height - 1) / 2);}}#endregionprivate void ucCircle_Paint(object sender, PaintEventArgs e){e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;Point center = GetCenterPoint();e.Graphics.TranslateTransform(center.X, center.Y);int radius = (center.X - 5);if (radius < 4) return;//Rectangle rectangle = new Rectangle(-radius, -radius, 2 * radius, 2 * radius);Rectangle rectangle = new Rectangle(-radius-2, -radius-2, 3 * radius, 3 * radius);e.Graphics.FillEllipse(brush_lantern_background, rectangle);}}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using PSS_PXIe_DOA_IS_SOFT.Properties;namespace PSS_PXIe_DOA_IS_SOFT.UcControl
{public partial class ucDoaTest : UserControl{private int _cornerRadius = 50;public ucDoaTest(){InitializeComponent();this.DoubleBuffered = true;}public string lbCurrModeText{get { return lbCurrMode.Text; }set { lbCurrMode.Text = value; }}public string lbWaveLengthText{get { return lbWaveLength.Text; }set { lbWaveLength.Text = value; }}public Color ucCircle1Color{get { return ucCircle1.LanternBackground; }set { ucCircle1.LanternBackground = value; }}public string lbAttenText{get { return lbAtten.Text; }set { lbAtten.Text = value; }}//public string lbOutputPowerText//{//    get { return lbOutputPower.Text; }//    set { lbOutputPower.Text = value; }//}//public int cbModeIndex//{//    get { return cbMode1.SelectedIndex; }//    set { cbMode1.SelectedIndex = value; }//}public int cbModeIndex{get { return cbMode.SelectedIndex; }set { cbMode.SelectedIndex = value; }}//public string cbModeText//{//    get { return cbMode1.Text; }//    set { cbMode1.Text = value; }//}public string cbModeText{get { return cbMode.Text; }set { cbMode.Text = value; }}public string cbWaveLengthText{//get { return cbWaveLength1.Text; }//set { cbWaveLength1.Text = value; }get { return cbWaveLength.Text; }set { cbWaveLength.Text = value; }}public string txtAttenText{get { return txtAtten.Text; }set { txtAtten.Text = value; }}public string txtOutputPowerText{get { return txtOutputPower.Text; }set { txtOutputPower.Text = value; }}public bool AttenVisible{get { return panel_Atten.Visible; }set { panel_Atten.Visible = value; }}public bool OutputPowerVisible{get { return panel_OutputPower.Visible; }set { panel_OutputPower.Visible = value; }}public void UpdatecbbWaveLengthItem(string[] newTtems){//cbWaveLength1.Items.Clear();//cbWaveLength1.Items.AddRange(newTtems);cbWaveLength.Items.Clear();cbWaveLength.Items.AddRange(newTtems);}public Image DoaPicType{get { return picdoatype.Image; }set { picdoatype.Image = value; }}public void SetMonitorBtnStatus(bool monitorStatus){//btnStartGetData.Enabled = enableStatus;//btnStopGetData.Enabled = !enableStatus;if (monitorStatus){btnMonitorOnOff.Image = PSS_PXIe_DOA_IS_SOFT.Properties.Resources.开_1_;}else{btnMonitorOnOff.Image = PSS_PXIe_DOA_IS_SOFT.Properties.Resources.关_1_;}}public void SetLightBtnStatus(bool lightStatus){//btnStartGetData.Enabled = enableStatus;//btnStopGetData.Enabled = !enableStatus;if (lightStatus){btnLightOnOff.Image = PSS_PXIe_DOA_IS_SOFT.Properties.Resources.关_1_;}else{btnLightOnOff.Image = PSS_PXIe_DOA_IS_SOFT.Properties.Resources.开_1_;}}//public void ChangePic(string type)//{//    if (type.Contains("6111"))//    {//        picdoatype.Image = Resources.单模;//    }//    else if(type.Contains("6121"))//    {//        picdoatype.Image = Resources.多模;//    }//}private void cbMode_SelectedIndexChanged(object sender, EventArgs e){//if (cbMode1.SelectedIndex == 0)//{//    panel_Atten.Visible = true;//    panel_OutputPower.Visible = false;//    //lbCurrMode.Text = "直衰";//}//else if (cbMode1.SelectedIndex == 1)//{//    panel_OutputPower.Visible = true;//    panel_Atten.Visible = false;//    //lbCurrMode.Text = "定功率";//}if (cbMode.SelectedIndex == 0){panel_Atten.Visible = true;panel_OutputPower.Visible = false;}else if (cbMode.SelectedIndex == 1){panel_OutputPower.Visible = true;panel_Atten.Visible = false;}}public event EventHandler btnSetZeroClick;private void btnSetZero_Click(object sender, EventArgs e){btnSetZeroClick?.Invoke(this, e);}public event EventHandler tnStartGetDataClick;private void btnStartGetData_Click(object sender, EventArgs e){tnStartGetDataClick?.Invoke(this, e);}public event EventHandler btnStopGetDataClick;private void btnStopGetData_Click(object sender, EventArgs e){btnStopGetDataClick?.Invoke(this, e);}public event EventHandler btnOpenLightClick;private void btnOpenLight_Click(object sender, EventArgs e){btnOpenLightClick?.Invoke(this, e);}public event EventHandler btnCloseLightClick;private void btnCloseLight_Click(object sender, EventArgs e){btnCloseLightClick?.Invoke(this, e);}private void ucDoaTest_Paint(object sender, PaintEventArgs e){base.OnPaint(e);}private void btnOpenLight_ucCircleTextClick(object sender, EventArgs e){btnOpenLightClick?.Invoke(this, e);}private void btnCloseLight_ucCircleTextClick(object sender, EventArgs e){btnCloseLightClick?.Invoke(this, e);}private void picLeft_Click(object sender, EventArgs e){ucTrackBar1.Value -= 0.01F;}private void picRight_Click(object sender, EventArgs e){ucTrackBar1.Value += 0.01F;}private void ucTrackBar1_ValueChanged(object sender, EventArgs e){this.txtAtten.Text = Convert.ToString((float)Math.Round(ucTrackBar1.Value, 2));}private void txtAtten_TextChanged(object sender, EventArgs e){if (string.IsNullOrWhiteSpace(txtAtten.Text)){return;}ucTrackBar1.Value = (float)Math.Round(Convert.ToDouble(txtAtten.Text), 2);}public event EventHandler btnSetModeClick;private void picMode_Click(object sender, EventArgs e){btnSetModeClick?.Invoke(this, e);}public event EventHandler btnSetWaveLengthClick;private void picSetWaveLen_Click(object sender, EventArgs e){btnSetWaveLengthClick?.Invoke(this, e);}public event EventHandler btnSetAttenClick;private void picSetAtten_Click(object sender, EventArgs e){btnSetAttenClick?.Invoke(this, e);}public event EventHandler btnSetOutputPowerClick;private void picOutputPower_Click(object sender, EventArgs e){btnSetOutputPowerClick?.Invoke(this, e);}private void picLeft_MouseLeave(object sender, EventArgs e){PictureBox picBox = sender as PictureBox;picBox.Image = Resources._24_左;}private void picLeft_MouseMove(object sender, MouseEventArgs e){PictureBox picBox = sender as PictureBox;picBox.Image = Resources.左_选中;}private void picRight_MouseLeave(object sender, EventArgs e){PictureBox picBox = sender as PictureBox;picBox.Image = Resources._24_右;}private void picRight_MouseMove(object sender, MouseEventArgs e){PictureBox picBox = sender as PictureBox;picBox.Image = Resources.右_选中;}private void picMode_MouseLeave(object sender, EventArgs e){PictureBox picBox = sender as PictureBox;picBox.Image = Resources.设置1;}private void picMode_MouseMove(object sender, MouseEventArgs e){PictureBox picBox = sender as PictureBox;picBox.Image = Resources.设置_选中;}private void btnOpenLight_ucCircleTextMove(object sender, EventArgs e){ucCircleText circleText = sender as ucCircleText;circleText.BorderStyle = BorderStyle.FixedSingle;}private void btnOpenLight_ucCircleTextLeave(object sender, EventArgs e){ucCircleText circleText = sender as ucCircleText;circleText.BorderStyle = BorderStyle.None;}public event EventHandler btnLightOnOffClick;private void btnLightOnOff_Click(object sender, EventArgs e){btnLightOnOffClick?.Invoke(this, e);}public event EventHandler btnMonitorOnOffClick;private void btnMonitorOnOff_Click(object sender, EventArgs e){btnMonitorOnOffClick?.Invoke(this, e);}}
}

在这里插入图片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace PSS_PXIe_DOA_IS_SOFT.UcControl
{public partial class ucTrigedge : UserControl{public ucTrigedge(){InitializeComponent();}//开状态颜色private Color trigColor = Color.Green;public Color TrigColor{get { return trigColor; }set { trigColor = value; Invalidate(); }}private TrigType trigtype = TrigType.RisingEdge;[Description("显示类型"), Category("自定义")]public TrigType TrigType{get { return trigtype; }set{trigtype = value;Refresh();}}protected override void OnPaint(PaintEventArgs pe){base.OnPaint(pe);Graphics g = pe.Graphics;//设置呈现质量g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//框框//上平坦g.DrawLine(new Pen(Color.Black, 2), new PointF(this.Width / 4 * 1, this.Height / 5), new PointF(this.Width / 4 * 3, this.Height / 5));//下左平坦g.DrawLine(new Pen(Color.Black, 2), new PointF(0, this.Height / 5 * 4), new PointF(this.Width / 4 * 1, this.Height / 5 * 4));//下右平坦g.DrawLine(new Pen(Color.Black, 2), new PointF(this.Width / 4 * 3, this.Height / 5 * 4), new PointF(this.Width, this.Height / 5 * 4));if (trigtype == TrigType.RisingEdge){//左上升沿g.DrawLine(new Pen(Color.Red, 2), new PointF(this.Width / 4 * 1, this.Height / 5), new PointF(this.Width / 4 * 1, this.Height / 5 * 4));//右下降沿g.DrawLine(new Pen(Color.Black, 2), new PointF(this.Width / 4 * 3, this.Height / 5), new PointF(this.Width / 4 * 3, this.Height / 5 * 4));//绘制箭头using (SolidBrush brush = new SolidBrush(Color.Red)){int padding = 10;PointF[] arrowPoints = new PointF[]{new PointF(this.Width / 4 * 1, this.Height/2-10),new PointF(this.Width / 4 * 1+5, this.Height/2+2),new PointF(this.Width / 4 * 1-5, this.Height/2+2)};g.FillPolygon(brush, arrowPoints);}}else if (trigtype == TrigType.FallingEdge){//左上升沿g.DrawLine(new Pen(Color.Black, 2), new PointF(this.Width / 4 * 1, this.Height / 5), new PointF(this.Width / 4 * 1, this.Height / 5 * 4));//右下降沿g.DrawLine(new Pen(Color.Red, 2), new PointF(this.Width / 4 * 3, this.Height / 5), new PointF(this.Width / 4 * 3, this.Height / 5 * 4));//绘制箭头using (SolidBrush brush = new SolidBrush(Color.Red)){int padding = 10;PointF[] arrowPoints = new PointF[]{new PointF(this.Width / 4 * 3, this.Height/2+8),new PointF(this.Width / 4 * 3+5, this.Height/2-4),new PointF(this.Width / 4 * 3-5, this.Height/2-4)};g.FillPolygon(brush, arrowPoints);}}}}public enum TrigType{/// <summary>/// 上升沿/// </summary>RisingEdge,/// <summary>/// 下降沿/// </summary>FallingEdge}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace PSS_PXIe_DOA_IS_SOFT.UcControl
{/// <summary>/// 信号灯控件 控件拖到界面上后尺寸改为20x20才会正常显示/// </summary>[Localizable(true)]public partial class ucCircle : UserControl{public ucCircle(){InitializeComponent();brush_lantern_background = new SolidBrush(color_lantern_background);}#region Private Member// 按钮的背景颜色,包括边线颜色private Color color_lantern_background = Color.LimeGreen;// 按钮的背景画刷              private Brush brush_lantern_background = null;#endregion#region Public Member/// <summary>/// 获取或设置开关按钮的背景色/// </summary>[Browsable(true)][Description("获取或设置信号灯的背景色")][Category("自定义设置")][DefaultValue(typeof(Color), "LimeGreen")][Localizable(true)]public Color LanternBackground{get{return color_lantern_background;}set{color_lantern_background = value;brush_lantern_background?.Dispose();brush_lantern_background = new SolidBrush(color_lantern_background);Invalidate();}}#endregion#region Private Methodprivate Point GetCenterPoint(){if (Height > Width){return new Point((Width - 1) / 2, (Width - 1) / 2);}else{return new Point((Height - 1) / 2, (Height - 1) / 2);}}#endregionprivate void ucCircle_Paint(object sender, PaintEventArgs e){e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;Point center = GetCenterPoint();e.Graphics.TranslateTransform(center.X, center.Y);int radius = (center.X - 5);if (radius < 4) return;//Rectangle rectangle = new Rectangle(-radius, -radius, 2 * radius, 2 * radius);Rectangle rectangle = new Rectangle(-radius-2, -radius-2, 3 * radius, 3 * radius);e.Graphics.FillEllipse(brush_lantern_background, rectangle);}}
}

文章转载自:

http://fQRqnzpG.dtrcL.cn
http://nfwwQyIN.dtrcL.cn
http://O25oIbNU.dtrcL.cn
http://P9phj1xq.dtrcL.cn
http://hYT8nOcM.dtrcL.cn
http://MJWdk1ZW.dtrcL.cn
http://43R9jgSA.dtrcL.cn
http://P0ukrKKp.dtrcL.cn
http://1YxPCzYp.dtrcL.cn
http://rsyvg98u.dtrcL.cn
http://2PRZRSvZ.dtrcL.cn
http://Y2r9jze1.dtrcL.cn
http://CxOWAIsY.dtrcL.cn
http://iSNuMTjD.dtrcL.cn
http://CxTayrqP.dtrcL.cn
http://f02giSut.dtrcL.cn
http://zlYbSG10.dtrcL.cn
http://xNt6cDIt.dtrcL.cn
http://qZU4oJeY.dtrcL.cn
http://p2x5uqKv.dtrcL.cn
http://13N1FlEo.dtrcL.cn
http://Ia6NSL69.dtrcL.cn
http://RXtymkrE.dtrcL.cn
http://oLkdSgOp.dtrcL.cn
http://yOJvzMlh.dtrcL.cn
http://5fUkLCbu.dtrcL.cn
http://QQkek1Gb.dtrcL.cn
http://6HlovSvw.dtrcL.cn
http://9q3s5x4D.dtrcL.cn
http://WZb0jP77.dtrcL.cn
http://www.dtcms.com/a/376014.html

相关文章:

  • 9月9日星期二今日早报简报微语报早读
  • Python快速入门专业版(十五):数据类型实战:用户信息录入程序(整合变量、输入与类型转换)
  • GEO与SEO,GEO 是什麼?SEO + AI = GEO 生成式搜尋引擎優化 全解析
  • Asp .Net Core 系列:Asp .Net Core 集成 Hangfire+MySQL
  • 如果服务端有数据更新,浏览器缓存同时也没有过期,如何直接使用最新的数据
  • 使用java编写一个基础的彩票抽奖程序
  • 算法题 Day5---String类
  • 【靶场练习】--DVWA第二关Command Injection(命令执行)全难度分析
  • 什么是Adobe Analytics?数据驱动营销的关键工具​
  • 使用Docker搭建MaxKB智能体平台
  • 【链表】3.重排链表(medium)
  • 免费!离线!免安装!Windows文件夹隐藏工具
  • 联邦学习及其相关创新SCI辅导
  • 466章:Python Web爬虫入门:使用Requests和BeautifulSoup
  • ES8集群部署与使用-zookeeper集群部署与使用
  • Nginx 优化与防盗链配置指南
  • 【数据结构】栈详解
  • 力扣周赛困难-3677. 统计二进制回文数字的数目(需要一定推理的经典二分)
  • 【硬件-笔试面试题-77】硬件/电子工程师,笔试面试题(知识点:滤波电路中截止频率的计算)
  • CUDA编程13 - 测量每个Block的执行时间
  • 仓颉编程语言青少年基础教程:特殊数据类型Unit类型和Nothing类型)
  • AFSim2.9.0学习笔记 —— 3、Wizard平台类型与ArkSIM平台介绍
  • 基于LTE标准的MIMO-OFDM仿真程序
  • 814章:Python Web爬虫入门:使用Requests和BeautifulSoup
  • 硬件开发(5)—ARM汇编
  • leetcode16(盛最多水的容器)
  • 《面向高速三维表面成像的微型深度学习轮廓术》论文总结
  • 基于Java的图书管理系统的设计与实现
  • 【Qt跬步积累】—— 初识Qt
  • 第十九章 使用LAMP架构部署动态网站环境