Commit 41f076dc HZH

add Trackbar

1 个父辈 08a3caa0
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
{
public class UCTrackBar : Control
{
[Description("值改变事件"), Category("自定义")]
public event EventHandler ValueChanged;
private int dcimalDigits = 0;
[Description("值小数精确位数"), Category("自定义")]
public int DcimalDigits
{
get { return dcimalDigits; }
set { dcimalDigits = value; }
}
private float lineWidth = 10;
[Description("线宽度"), Category("自定义")]
public float LineWidth
{
get { return lineWidth; }
set { lineWidth = value; }
}
private float minValue = 0;
[Description("最小值"), Category("自定义")]
public float MinValue
{
get { return minValue; }
set
{
if (minValue > m_value)
return;
minValue = value;
this.Refresh();
}
}
private float maxValue = 100;
[Description("最大值"), Category("自定义")]
public float MaxValue
{
get { return maxValue; }
set
{
if (value < m_value)
return;
maxValue = value;
this.Refresh();
}
}
private float m_value = 0;
[Description("值"), Category("自定义")]
public float Value
{
get { return this.m_value; }
set
{
if (value > maxValue || value < minValue)
return;
var v = (float)Math.Round((double)value, dcimalDigits);
if (value == v)
return;
this.m_value = v;
this.Refresh();
if (ValueChanged != null)
{
ValueChanged(this, null);
}
}
}
private Color m_lineColor = Color.FromArgb(255, 77, 59);
[Description("线颜色"), Category("自定义")]
public Color LineColor
{
get { return m_lineColor; }
set
{
m_lineColor = value;
this.Refresh();
}
}
RectangleF m_lineRectangle;
RectangleF m_trackRectangle;
public UCTrackBar()
{
this.Size = new Size(250, 30);
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.MouseDown += UCTrackBar_MouseDown;
this.MouseMove += UCTrackBar_MouseMove;
this.MouseUp += UCTrackBar_MouseUp;
}
bool blnDown = false;
void UCTrackBar_MouseDown(object sender, MouseEventArgs e)
{
if (m_lineRectangle.Contains(e.Location) || m_trackRectangle.Contains(e.Location))
{
blnDown = true;
Value = ((float)e.Location.X / (float)this.Width) * (maxValue - minValue);
}
}
void UCTrackBar_MouseMove(object sender, MouseEventArgs e)
{
if (blnDown)
{
Value = ((float)e.Location.X / (float)this.Width) * (maxValue - minValue);
}
}
void UCTrackBar_MouseUp(object sender, MouseEventArgs e)
{
blnDown = false;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
g.SetGDIHigh();
m_lineRectangle = new RectangleF(lineWidth, (this.Size.Height - lineWidth) / 2, this.Size.Width - lineWidth * 2, lineWidth);
GraphicsPath pathLine = ControlHelper.CreateRoundedRectanglePath(m_lineRectangle, 5);
g.FillPath(new SolidBrush(m_lineColor), pathLine);
m_trackRectangle = new RectangleF(m_lineRectangle.Left - lineWidth + (((float)m_value / (float)(maxValue - minValue)) * (this.Size.Width - lineWidth * 2)), (this.Size.Height - lineWidth * 2) / 2, lineWidth * 2, lineWidth * 2);
g.FillEllipse(new SolidBrush(m_lineColor), m_trackRectangle);
g.FillEllipse(Brushes.White, new RectangleF(m_trackRectangle.X + m_trackRectangle.Width / 4, m_trackRectangle.Y + m_trackRectangle.Height / 4, m_trackRectangle.Width / 2, m_trackRectangle.Height / 2));
}
}
}
...@@ -185,6 +185,9 @@ ...@@ -185,6 +185,9 @@
<Compile Include="Controls\Tab\TabControlExt.cs"> <Compile Include="Controls\Tab\TabControlExt.cs">
<SubType>Component</SubType> <SubType>Component</SubType>
</Compile> </Compile>
<Compile Include="Controls\TrackBar\UCTrackBar.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Controls\Wave\UCWaveChart.cs"> <Compile Include="Controls\Wave\UCWaveChart.cs">
<SubType>UserControl</SubType> <SubType>UserControl</SubType>
</Compile> </Compile>
......
...@@ -663,5 +663,20 @@ namespace HZH_Controls ...@@ -663,5 +663,20 @@ namespace HZH_Controls
roundedRect.CloseFigure(); roundedRect.CloseFigure();
return roundedRect; return roundedRect;
} }
public static GraphicsPath CreateRoundedRectanglePath(RectangleF rect, int cornerRadius)
{
GraphicsPath roundedRect = new GraphicsPath();
roundedRect.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
roundedRect.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);
roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
roundedRect.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);
roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
roundedRect.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);
roundedRect.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
roundedRect.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);
roundedRect.CloseFigure();
return roundedRect;
}
} }
} }
...@@ -37,6 +37,7 @@ ...@@ -37,6 +37,7 @@
this.label2 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label();
this.trackBar3 = new System.Windows.Forms.TrackBar(); this.trackBar3 = new System.Windows.Forms.TrackBar();
this.label3 = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label();
this.ucTrackBar1 = new HZH_Controls.Controls.UCTrackBar();
this.ucProcessWave2 = new HZH_Controls.Controls.UCProcessWave(); this.ucProcessWave2 = new HZH_Controls.Controls.UCProcessWave();
this.ucProcessWave1 = new HZH_Controls.Controls.UCProcessWave(); this.ucProcessWave1 = new HZH_Controls.Controls.UCProcessWave();
this.ucWaveWithSource1 = new HZH_Controls.Controls.UCWaveWithSource(); this.ucWaveWithSource1 = new HZH_Controls.Controls.UCWaveWithSource();
...@@ -135,6 +136,18 @@ ...@@ -135,6 +136,18 @@
this.label3.TabIndex = 9; this.label3.TabIndex = 9;
this.label3.Text = "波宽"; this.label3.Text = "波宽";
// //
// ucTrackBar1
//
this.ucTrackBar1.LineWidth = 10;
this.ucTrackBar1.Location = new System.Drawing.Point(833, 403);
this.ucTrackBar1.MaxValue = 100;
this.ucTrackBar1.MinValue = 0;
this.ucTrackBar1.Name = "ucTrackBar1";
this.ucTrackBar1.Size = new System.Drawing.Size(212, 32);
this.ucTrackBar1.TabIndex = 12;
this.ucTrackBar1.Text = "ucTrackBar1";
this.ucTrackBar1.Value = 100;
//
// ucProcessWave2 // ucProcessWave2
// //
this.ucProcessWave2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(197)))), ((int)(((byte)(229)))), ((int)(((byte)(250))))); this.ucProcessWave2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(197)))), ((int)(((byte)(229)))), ((int)(((byte)(250)))));
...@@ -497,6 +510,7 @@ ...@@ -497,6 +510,7 @@
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.BackColor = System.Drawing.Color.White; this.BackColor = System.Drawing.Color.White;
this.ClientSize = new System.Drawing.Size(1438, 594); this.ClientSize = new System.Drawing.Size(1438, 594);
this.Controls.Add(this.ucTrackBar1);
this.Controls.Add(this.ucProcessWave2); this.Controls.Add(this.ucProcessWave2);
this.Controls.Add(this.ucProcessWave1); this.Controls.Add(this.ucProcessWave1);
this.Controls.Add(this.ucWaveWithSource1); this.Controls.Add(this.ucWaveWithSource1);
...@@ -567,6 +581,7 @@ ...@@ -567,6 +581,7 @@
private HZH_Controls.Controls.UCWaveWithSource ucWaveWithSource1; private HZH_Controls.Controls.UCWaveWithSource ucWaveWithSource1;
private HZH_Controls.Controls.UCProcessWave ucProcessWave1; private HZH_Controls.Controls.UCProcessWave ucProcessWave1;
private HZH_Controls.Controls.UCProcessWave ucProcessWave2; private HZH_Controls.Controls.UCProcessWave ucProcessWave2;
private HZH_Controls.Controls.UCTrackBar ucTrackBar1;
} }
} }
\ No newline at end of file \ No newline at end of file
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!