可拖动的滑块
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 Method
private Point GetCenterPoint()
{
if (Height > Width)
{
return new Point((Width - 1) / 2, (Width - 1) / 2);
}
else
{
return new Point((Height - 1) / 2, (Height - 1) / 2);
}
}
#endregion
private 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 Method
private Point GetCenterPoint()
{
if (Height > Width)
{
return new Point((Width - 1) / 2, (Width - 1) / 2);
}
else
{
return new Point((Height - 1) / 2, (Height - 1) / 2);
}
}
#endregion
private 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);
}
}
}