c\#Winform 사용자 정의 컨트롤-계기판 기능
35487 단어 c#Winform사용자 정의 컨트롤계기판
입 행 한 지 7,8 년 이 되 었 습 니 다.예 쁜 사용자 정의 컨트롤 을 하고 싶 어서 이 시리즈 의 글 이 있 습 니 다.
GitHub: https://github.com/kwwwvagaa/NetWinformControl
메타 클 라 우 드:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
NuGet
Install-Package HZH_Controls
목차https://www.cnblogs.com/bfyx/p/11364884.html
용도 및 효과
준비 작업
여전히 GDI+그림 을 사용 하고 있 습 니 다.모 르 면 바 이 두 를 해 보 세 요.
그리고 주로 삼각함수 가 사용 되 는데 모 르 면 중학교 수학 선생님 께 다시 물 어 봐 도 됩 니 다.
시작 하 다
클래스 UCMeter 계승 UserControl 추가
우선 제어 할 속성 을 추가 합 니 다.
private int splitCount = 10;
/// <summary>
/// Gets or sets the split count.
/// </summary>
/// <value>The split count.</value>
[Description(" ,>1"), Category(" ")]
public int SplitCount
{
get { return splitCount; }
set
{
if (value < 1)
return;
splitCount = value;
Refresh();
}
}
private int meterDegrees = 150;
/// <summary>
/// Gets or sets the meter degrees.
/// </summary>
/// <value>The meter degrees.</value>
[Description(" ,0-360"), Category(" ")]
public int MeterDegrees
{
get { return meterDegrees; }
set
{
if (value > 360 || value <= 0)
return;
meterDegrees = value;
Refresh();
}
}
private decimal minValue = 0;
/// <summary>
/// Gets or sets the minimum value.
/// </summary>
/// <value>The minimum value.</value>
[Description(" ,<MaxValue"), Category(" ")]
public decimal MinValue
{
get { return minValue; }
set
{
if (value >= maxValue)
return;
minValue = value;
Refresh();
}
}
private decimal maxValue = 100;
/// <summary>
/// Gets or sets the maximum value.
/// </summary>
/// <value>The maximum value.</value>
[Description(" ,>MinValue"), Category(" ")]
public decimal MaxValue
{
get { return maxValue; }
set
{
if (value <= minValue)
return;
maxValue = value;
Refresh();
}
}
/// <summary>
/// 。
/// </summary>
/// <value>The font.</value>
/// <PermissionSet>
/// <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
/// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
/// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
/// <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
/// </PermissionSet>
[Description(" "), Category(" ")]
public override Font Font
{
get
{
return base.Font;
}
set
{
base.Font = value;
Refresh();
}
}
private decimal m_value = 0;
/// <summary>
/// Gets or sets the value.
/// </summary>
/// <value>The value.</value>
[Description(" ,>=MinValue <=MaxValue"), Category(" ")]
public decimal Value
{
get { return m_value; }
set
{
if (value < minValue || value > maxValue)
return;
m_value = value;
Refresh();
}
}
private MeterTextLocation textLocation = MeterTextLocation.None;
/// <summary>
/// Gets or sets the text location.
/// </summary>
/// <value>The text location.</value>
[Description(" "), Category(" ")]
public MeterTextLocation TextLocation
{
get { return textLocation; }
set
{
textLocation = value;
Refresh();
}
}
private string fixedText;
/// <summary>
/// Gets or sets the fixed text.
/// </summary>
/// <value>The fixed text.</value>
[Description(" "), Category(" ")]
public string FixedText
{
get { return fixedText; }
set
{
fixedText = value;
Refresh();
}
}
private Font textFont = DefaultFont;
/// <summary>
/// Gets or sets the text font.
/// </summary>
/// <value>The text font.</value>
[Description(" "), Category(" ")]
public Font TextFont
{
get { return textFont; }
set
{
textFont = value;
Refresh();
}
}
private Color externalRoundColor = Color.FromArgb(255, 77, 59);
/// <summary>
/// Gets or sets the color of the external round.
/// </summary>
/// <value>The color of the external round.</value>
[Description(" "), Category(" ")]
public Color ExternalRoundColor
{
get { return externalRoundColor; }
set
{
externalRoundColor = value;
Refresh();
}
}
private Color insideRoundColor = Color.FromArgb(255, 77, 59);
/// <summary>
/// Gets or sets the color of the inside round.
/// </summary>
/// <value>The color of the inside round.</value>
[Description(" "), Category(" ")]
public Color InsideRoundColor
{
get { return insideRoundColor; }
set
{
insideRoundColor = value;
Refresh();
}
}
private Color boundaryLineColor = Color.FromArgb(255, 77, 59);
/// <summary>
/// Gets or sets the color of the boundary line.
/// </summary>
/// <value>The color of the boundary line.</value>
[Description(" "), Category(" ")]
public Color BoundaryLineColor
{
get { return boundaryLineColor; }
set
{
boundaryLineColor = value;
Refresh();
}
}
private Color scaleColor = Color.FromArgb(255, 77, 59);
/// <summary>
/// Gets or sets the color of the scale.
/// </summary>
/// <value>The color of the scale.</value>
[Description(" "), Category(" ")]
public Color ScaleColor
{
get { return scaleColor; }
set
{
scaleColor = value;
Refresh();
}
}
private Color scaleValueColor = Color.FromArgb(255, 77, 59);
/// <summary>
/// Gets or sets the color of the scale value.
/// </summary>
/// <value>The color of the scale value.</value>
[Description(" "), Category(" ")]
public Color ScaleValueColor
{
get { return scaleValueColor; }
set
{
scaleValueColor = value;
Refresh();
}
}
private Color pointerColor = Color.FromArgb(255, 77, 59);
/// <summary>
/// Gets or sets the color of the pointer.
/// </summary>
/// <value>The color of the pointer.</value>
[Description(" "), Category(" ")]
public Color PointerColor
{
get { return pointerColor; }
set
{
pointerColor = value;
Refresh();
}
}
private Color textColor = Color.FromArgb(255, 77, 59);
/// <summary>
/// Gets or sets the color of the text.
/// </summary>
/// <value>The color of the text.</value>
[Description(" "), Category(" ")]
public Color TextColor
{
get { return textColor; }
set
{
textColor = value;
Refresh();
}
}
Rectangle m_rectWorking;
다시 그리다
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var g = e.Graphics;
g.SetGDIHigh();
//
float fltStartAngle = -90 - (meterDegrees) / 2 + 360;
var r1 = new Rectangle(m_rectWorking.Location, new Size(m_rectWorking.Width, m_rectWorking.Width));
g.DrawArc(new Pen(new SolidBrush(externalRoundColor), 1), r1, fltStartAngle, meterDegrees);
//
var r2 = new Rectangle(m_rectWorking.Left + (m_rectWorking.Width - m_rectWorking.Width / 4) / 2, m_rectWorking.Top + (m_rectWorking.Width - m_rectWorking.Width / 4) / 2, m_rectWorking.Width / 4, m_rectWorking.Width / 4);
g.DrawArc(new Pen(new SolidBrush(insideRoundColor), 1), r2, fltStartAngle, meterDegrees);
//
if (meterDegrees != 360)
{
float fltAngle = fltStartAngle - 180;
float intY = (float)(m_rectWorking.Top + m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - m_rectWorking.Width / 8) * Math.Sin(Math.PI * (fltAngle / 180.00F))));
float intX = (float)(m_rectWorking.Left + (m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - m_rectWorking.Width / 8) * Math.Cos(Math.PI * (fltAngle / 180.00F)))));
float fltY1 = (float)(m_rectWorking.Top + m_rectWorking.Width / 2 - (m_rectWorking.Width / 8 * Math.Sin(Math.PI * (fltAngle / 180.00F))));
float fltX1 = (float)(m_rectWorking.Left + (m_rectWorking.Width / 2 - (m_rectWorking.Width / 8 * Math.Cos(Math.PI * (fltAngle / 180.00F)))));
g.DrawLine(new Pen(new SolidBrush(boundaryLineColor), 1), new PointF(intX, intY), new PointF(fltX1, fltY1));
g.DrawLine(new Pen(new SolidBrush(boundaryLineColor), 1), new PointF(m_rectWorking.Right - (fltX1 - m_rectWorking.Left), fltY1), new PointF(m_rectWorking.Right - (intX - m_rectWorking.Left), intY));
}
//
int _splitCount = splitCount * 2;
float fltSplitValue = (float)meterDegrees / (float)_splitCount;
for (int i = 0; i <= _splitCount; i++)
{
float fltAngle = (fltStartAngle + fltSplitValue * i - 180) % 360;
float fltY1 = (float)(m_rectWorking.Top + m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2) * Math.Sin(Math.PI * (fltAngle / 180.00F))));
float fltX1 = (float)(m_rectWorking.Left + (m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2) * Math.Cos(Math.PI * (fltAngle / 180.00F)))));
float fltY2 = 0;
float fltX2 = 0;
if (i % 2 == 0)
{
fltY2 = (float)(m_rectWorking.Top + m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - 10) * Math.Sin(Math.PI * (fltAngle / 180.00F))));
fltX2 = (float)(m_rectWorking.Left + (m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - 10) * Math.Cos(Math.PI * (fltAngle / 180.00F)))));
if (!(meterDegrees == 360 && i == _splitCount))
{
decimal decValue = minValue + (maxValue - minValue) / _splitCount * i;
var txtSize = g.MeasureString(decValue.ToString("0.##"), this.Font);
float fltFY1 = (float)(m_rectWorking.Top - txtSize.Height / 2 + m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - 20) * Math.Sin(Math.PI * (fltAngle / 180.00F))));
float fltFX1 = (float)(m_rectWorking.Left - txtSize.Width / 2 + (m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - 20) * Math.Cos(Math.PI * (fltAngle / 180.00F)))));
g.DrawString(decValue.ToString("0.##"), Font, new SolidBrush(scaleValueColor), fltFX1, fltFY1);
}
}
else
{
fltY2 = (float)(m_rectWorking.Top + m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - 5) * Math.Sin(Math.PI * (fltAngle / 180.00F))));
fltX2 = (float)(m_rectWorking.Left + (m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - 5) * Math.Cos(Math.PI * (fltAngle / 180.00F)))));
}
g.DrawLine(new Pen(new SolidBrush(scaleColor), i % 2 == 0 ? 2 : 1), new PointF(fltX1, fltY1), new PointF(fltX2, fltY2));
}
//
if (textLocation != MeterTextLocation.None)
{
string str = m_value.ToString("0.##");
var txtSize = g.MeasureString(str, textFont);
float fltY = m_rectWorking.Top + m_rectWorking.Width / 4 - txtSize.Height / 2;
float fltX = m_rectWorking.Left + m_rectWorking.Width / 2 - txtSize.Width / 2;
g.DrawString(str, textFont, new SolidBrush(textColor), new PointF(fltX, fltY));
if (!string.IsNullOrEmpty(fixedText))
{
str = fixedText;
txtSize = g.MeasureString(str, textFont);
fltY = m_rectWorking.Top + m_rectWorking.Width / 4 + txtSize.Height / 2;
fltX = m_rectWorking.Left + m_rectWorking.Width / 2 - txtSize.Width / 2;
g.DrawString(str, textFont, new SolidBrush(textColor), new PointF(fltX, fltY));
}
}
//
g.FillEllipse(new SolidBrush(Color.FromArgb(100, pointerColor.R, pointerColor.G, pointerColor.B)), new Rectangle(m_rectWorking.Left + m_rectWorking.Width / 2 - 10, m_rectWorking.Top + m_rectWorking.Width / 2 - 10, 20, 20));
g.FillEllipse(Brushes.Red, new Rectangle(m_rectWorking.Left + m_rectWorking.Width / 2 - 5, m_rectWorking.Top + m_rectWorking.Width / 2 - 5, 10, 10));
float fltValueAngle = (fltStartAngle + ((float)(m_value - minValue) / (float)(maxValue - minValue)) * (float)meterDegrees - 180) % 360;
float intValueY1 = (float)(m_rectWorking.Top + m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - 30) * Math.Sin(Math.PI * (fltValueAngle / 180.00F))));
float intValueX1 = (float)(m_rectWorking.Left + (m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - 30) * Math.Cos(Math.PI * (fltValueAngle / 180.00F)))));
g.DrawLine(new Pen(new SolidBrush(pointerColor), 3), intValueX1, intValueY1, m_rectWorking.Left + m_rectWorking.Width / 2, m_rectWorking.Top + m_rectWorking.Width / 2);
}
텍스트 위 치 를 표시 하 는 매 거 진 이 하나 더 있 습 니 다.
/// <summary>
/// Enum MeterTextLocation
/// </summary>
public enum MeterTextLocation
{
/// <summary>
/// The none
/// </summary>
None,
/// <summary>
/// The top
/// </summary>
Top,
/// <summary>
/// The bottom
/// </summary>
Bottom
}
코드 가 이렇게 많아 요.전체 코드 를 보 세 요.
// ***********************************************************************
// Assembly : HZH_Controls
// Created : 2019-09-03
//
// ***********************************************************************
// <copyright file="UCMeter.cs">
// Copyright by Huang Zhenghui( ) All, QQ group:568015492 QQ:623128629 Email:[email protected]
// </copyright>
//
// Blog: https://www.cnblogs.com/bfyx
// GitHub:https://github.com/kwwwvagaa/NetWinformControl
// gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
//
// If you use this code, please keep this note.
// ***********************************************************************
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
{
/// <summary>
/// Class UCMeter.
/// Implements the <see cref="System.Windows.Forms.UserControl" />
/// </summary>
/// <seealso cref="System.Windows.Forms.UserControl" />
public class UCMeter : UserControl
{
private int splitCount = 10;
/// <summary>
/// Gets or sets the split count.
/// </summary>
/// <value>The split count.</value>
[Description(" ,>1"), Category(" ")]
public int SplitCount
{
get { return splitCount; }
set
{
if (value < 1)
return;
splitCount = value;
Refresh();
}
}
private int meterDegrees = 150;
/// <summary>
/// Gets or sets the meter degrees.
/// </summary>
/// <value>The meter degrees.</value>
[Description(" ,0-360"), Category(" ")]
public int MeterDegrees
{
get { return meterDegrees; }
set
{
if (value > 360 || value <= 0)
return;
meterDegrees = value;
Refresh();
}
}
private decimal minValue = 0;
/// <summary>
/// Gets or sets the minimum value.
/// </summary>
/// <value>The minimum value.</value>
[Description(" ,<MaxValue"), Category(" ")]
public decimal MinValue
{
get { return minValue; }
set
{
if (value >= maxValue)
return;
minValue = value;
Refresh();
}
}
private decimal maxValue = 100;
/// <summary>
/// Gets or sets the maximum value.
/// </summary>
/// <value>The maximum value.</value>
[Description(" ,>MinValue"), Category(" ")]
public decimal MaxValue
{
get { return maxValue; }
set
{
if (value <= minValue)
return;
maxValue = value;
Refresh();
}
}
/// <summary>
/// 。
/// </summary>
/// <value>The font.</value>
/// <PermissionSet>
/// <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
/// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
/// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
/// <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
/// </PermissionSet>
[Description(" "), Category(" ")]
public override Font Font
{
get
{
return base.Font;
}
set
{
base.Font = value;
Refresh();
}
}
private decimal m_value = 0;
/// <summary>
/// Gets or sets the value.
/// </summary>
/// <value>The value.</value>
[Description(" ,>=MinValue <=MaxValue"), Category(" ")]
public decimal Value
{
get { return m_value; }
set
{
if (value < minValue || value > maxValue)
return;
m_value = value;
Refresh();
}
}
private MeterTextLocation textLocation = MeterTextLocation.None;
/// <summary>
/// Gets or sets the text location.
/// </summary>
/// <value>The text location.</value>
[Description(" "), Category(" ")]
public MeterTextLocation TextLocation
{
get { return textLocation; }
set
{
textLocation = value;
Refresh();
}
}
private string fixedText;
/// <summary>
/// Gets or sets the fixed text.
/// </summary>
/// <value>The fixed text.</value>
[Description(" "), Category(" ")]
public string FixedText
{
get { return fixedText; }
set
{
fixedText = value;
Refresh();
}
}
private Font textFont = DefaultFont;
/// <summary>
/// Gets or sets the text font.
/// </summary>
/// <value>The text font.</value>
[Description(" "), Category(" ")]
public Font TextFont
{
get { return textFont; }
set
{
textFont = value;
Refresh();
}
}
private Color externalRoundColor = Color.FromArgb(255, 77, 59);
/// <summary>
/// Gets or sets the color of the external round.
/// </summary>
/// <value>The color of the external round.</value>
[Description(" "), Category(" ")]
public Color ExternalRoundColor
{
get { return externalRoundColor; }
set
{
externalRoundColor = value;
Refresh();
}
}
private Color insideRoundColor = Color.FromArgb(255, 77, 59);
/// <summary>
/// Gets or sets the color of the inside round.
/// </summary>
/// <value>The color of the inside round.</value>
[Description(" "), Category(" ")]
public Color InsideRoundColor
{
get { return insideRoundColor; }
set
{
insideRoundColor = value;
Refresh();
}
}
private Color boundaryLineColor = Color.FromArgb(255, 77, 59);
/// <summary>
/// Gets or sets the color of the boundary line.
/// </summary>
/// <value>The color of the boundary line.</value>
[Description(" "), Category(" ")]
public Color BoundaryLineColor
{
get { return boundaryLineColor; }
set
{
boundaryLineColor = value;
Refresh();
}
}
private Color scaleColor = Color.FromArgb(255, 77, 59);
/// <summary>
/// Gets or sets the color of the scale.
/// </summary>
/// <value>The color of the scale.</value>
[Description(" "), Category(" ")]
public Color ScaleColor
{
get { return scaleColor; }
set
{
scaleColor = value;
Refresh();
}
}
private Color scaleValueColor = Color.FromArgb(255, 77, 59);
/// <summary>
/// Gets or sets the color of the scale value.
/// </summary>
/// <value>The color of the scale value.</value>
[Description(" "), Category(" ")]
public Color ScaleValueColor
{
get { return scaleValueColor; }
set
{
scaleValueColor = value;
Refresh();
}
}
private Color pointerColor = Color.FromArgb(255, 77, 59);
/// <summary>
/// Gets or sets the color of the pointer.
/// </summary>
/// <value>The color of the pointer.</value>
[Description(" "), Category(" ")]
public Color PointerColor
{
get { return pointerColor; }
set
{
pointerColor = value;
Refresh();
}
}
private Color textColor = Color.FromArgb(255, 77, 59);
/// <summary>
/// Gets or sets the color of the text.
/// </summary>
/// <value>The color of the text.</value>
[Description(" "), Category(" ")]
public Color TextColor
{
get { return textColor; }
set
{
textColor = value;
Refresh();
}
}
Rectangle m_rectWorking;
public UCMeter()
{
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.SizeChanged += UCMeter1_SizeChanged;
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.Size = new Size(350, 200);
}
void UCMeter1_SizeChanged(object sender, EventArgs e)
{
m_rectWorking = new Rectangle(10, 10, this.Width - 20, this.Height - 20);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var g = e.Graphics;
g.SetGDIHigh();
//
float fltStartAngle = -90 - (meterDegrees) / 2 + 360;
var r1 = new Rectangle(m_rectWorking.Location, new Size(m_rectWorking.Width, m_rectWorking.Width));
g.DrawArc(new Pen(new SolidBrush(externalRoundColor), 1), r1, fltStartAngle, meterDegrees);
//
var r2 = new Rectangle(m_rectWorking.Left + (m_rectWorking.Width - m_rectWorking.Width / 4) / 2, m_rectWorking.Top + (m_rectWorking.Width - m_rectWorking.Width / 4) / 2, m_rectWorking.Width / 4, m_rectWorking.Width / 4);
g.DrawArc(new Pen(new SolidBrush(insideRoundColor), 1), r2, fltStartAngle, meterDegrees);
//
if (meterDegrees != 360)
{
float fltAngle = fltStartAngle - 180;
float intY = (float)(m_rectWorking.Top + m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - m_rectWorking.Width / 8) * Math.Sin(Math.PI * (fltAngle / 180.00F))));
float intX = (float)(m_rectWorking.Left + (m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - m_rectWorking.Width / 8) * Math.Cos(Math.PI * (fltAngle / 180.00F)))));
float fltY1 = (float)(m_rectWorking.Top + m_rectWorking.Width / 2 - (m_rectWorking.Width / 8 * Math.Sin(Math.PI * (fltAngle / 180.00F))));
float fltX1 = (float)(m_rectWorking.Left + (m_rectWorking.Width / 2 - (m_rectWorking.Width / 8 * Math.Cos(Math.PI * (fltAngle / 180.00F)))));
g.DrawLine(new Pen(new SolidBrush(boundaryLineColor), 1), new PointF(intX, intY), new PointF(fltX1, fltY1));
g.DrawLine(new Pen(new SolidBrush(boundaryLineColor), 1), new PointF(m_rectWorking.Right - (fltX1 - m_rectWorking.Left), fltY1), new PointF(m_rectWorking.Right - (intX - m_rectWorking.Left), intY));
}
//
int _splitCount = splitCount * 2;
float fltSplitValue = (float)meterDegrees / (float)_splitCount;
for (int i = 0; i <= _splitCount; i++)
{
float fltAngle = (fltStartAngle + fltSplitValue * i - 180) % 360;
float fltY1 = (float)(m_rectWorking.Top + m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2) * Math.Sin(Math.PI * (fltAngle / 180.00F))));
float fltX1 = (float)(m_rectWorking.Left + (m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2) * Math.Cos(Math.PI * (fltAngle / 180.00F)))));
float fltY2 = 0;
float fltX2 = 0;
if (i % 2 == 0)
{
fltY2 = (float)(m_rectWorking.Top + m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - 10) * Math.Sin(Math.PI * (fltAngle / 180.00F))));
fltX2 = (float)(m_rectWorking.Left + (m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - 10) * Math.Cos(Math.PI * (fltAngle / 180.00F)))));
if (!(meterDegrees == 360 && i == _splitCount))
{
decimal decValue = minValue + (maxValue - minValue) / _splitCount * i;
var txtSize = g.MeasureString(decValue.ToString("0.##"), this.Font);
float fltFY1 = (float)(m_rectWorking.Top - txtSize.Height / 2 + m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - 20) * Math.Sin(Math.PI * (fltAngle / 180.00F))));
float fltFX1 = (float)(m_rectWorking.Left - txtSize.Width / 2 + (m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - 20) * Math.Cos(Math.PI * (fltAngle / 180.00F)))));
g.DrawString(decValue.ToString("0.##"), Font, new SolidBrush(scaleValueColor), fltFX1, fltFY1);
}
}
else
{
fltY2 = (float)(m_rectWorking.Top + m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - 5) * Math.Sin(Math.PI * (fltAngle / 180.00F))));
fltX2 = (float)(m_rectWorking.Left + (m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - 5) * Math.Cos(Math.PI * (fltAngle / 180.00F)))));
}
g.DrawLine(new Pen(new SolidBrush(scaleColor), i % 2 == 0 ? 2 : 1), new PointF(fltX1, fltY1), new PointF(fltX2, fltY2));
}
//
if (textLocation != MeterTextLocation.None)
{
string str = m_value.ToString("0.##");
var txtSize = g.MeasureString(str, textFont);
float fltY = m_rectWorking.Top + m_rectWorking.Width / 4 - txtSize.Height / 2;
float fltX = m_rectWorking.Left + m_rectWorking.Width / 2 - txtSize.Width / 2;
g.DrawString(str, textFont, new SolidBrush(textColor), new PointF(fltX, fltY));
if (!string.IsNullOrEmpty(fixedText))
{
str = fixedText;
txtSize = g.MeasureString(str, textFont);
fltY = m_rectWorking.Top + m_rectWorking.Width / 4 + txtSize.Height / 2;
fltX = m_rectWorking.Left + m_rectWorking.Width / 2 - txtSize.Width / 2;
g.DrawString(str, textFont, new SolidBrush(textColor), new PointF(fltX, fltY));
}
}
//
g.FillEllipse(new SolidBrush(Color.FromArgb(100, pointerColor.R, pointerColor.G, pointerColor.B)), new Rectangle(m_rectWorking.Left + m_rectWorking.Width / 2 - 10, m_rectWorking.Top + m_rectWorking.Width / 2 - 10, 20, 20));
g.FillEllipse(Brushes.Red, new Rectangle(m_rectWorking.Left + m_rectWorking.Width / 2 - 5, m_rectWorking.Top + m_rectWorking.Width / 2 - 5, 10, 10));
float fltValueAngle = (fltStartAngle + ((float)(m_value - minValue) / (float)(maxValue - minValue)) * (float)meterDegrees - 180) % 360;
float intValueY1 = (float)(m_rectWorking.Top + m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - 30) * Math.Sin(Math.PI * (fltValueAngle / 180.00F))));
float intValueX1 = (float)(m_rectWorking.Left + (m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - 30) * Math.Cos(Math.PI * (fltValueAngle / 180.00F)))));
g.DrawLine(new Pen(new SolidBrush(pointerColor), 3), intValueX1, intValueY1, m_rectWorking.Left + m_rectWorking.Width / 2, m_rectWorking.Top + m_rectWorking.Width / 2);
}
}
/// <summary>
/// Enum MeterTextLocation
/// </summary>
public enum MeterTextLocation
{
/// <summary>
/// The none
/// </summary>
None,
/// <summary>
/// The top
/// </summary>
Top,
/// <summary>
/// The bottom
/// </summary>
Bottom
}
}
마지막 말마음 에 드 시 면https://gitee.com/kwwwvagaa/net_winform_custom_control별 을 주문 하 세 요.
총결산
위 에서 말 한 것 은 소 편 이 소개 한 c\#Winform 사용자 정의 컨트롤-계기판 기능 입 니 다.도움 이 되 셨 으 면 좋 겠 습 니 다.궁금 한 점 이 있 으 시 면 메 시 지 를 남 겨 주세요.소 편 은 바로 답 해 드 리 겠 습 니 다.여기 서도 저희 사이트 에 대한 여러분 의 지지 에 감 사 드 립 니 다!
만약 당신 이 본문 이 당신 에 게 도움 이 된다 고 생각한다 면,전 재 를 환영 합 니 다.번 거 로 우 시 겠 지만 출처 를 밝 혀 주 십시오.감사합니다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
C#Task를 사용하여 비동기식 작업을 수행하는 방법라인이 완성된 후에 이 라인을 다시 시작할 수 없습니다.반대로 조인(Join)만 결합할 수 있습니다 (프로세스가 현재 라인을 막습니다). 임무는 조합할 수 있는 것이다. 연장을 사용하여 그것들을 한데 연결시키는 것이...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.