📄 usercontrol1.cs
字号:
this.nl.Width=(this.Width-60)/3;
this.nl.Height=(this.Height-60)/6;
this.nl.Location=new Point((60-this.Width)/6+this.Width/2,this.Height/2+(this.Height-60)/11+5);
this.nl.ForeColor=namecolor;
this.nl.Font=namefont;
this.nl.Text=metername;
//仪表的量纲
this.dl.Width=(this.Width-60)/2;
this.dl.Height=(this.Height-60)/10;
this.dl.Location=new Point(this.Width/2,this.Height/2+(this.Height-60)/4+10);
this.dl.ForeColor=dimentioncolor;
this.dl.Font=dimentionfont;
this.dl.Text=dimention;
//仪表的值数字显示
this.vl.Width=(this.Width-60)/2;
this.vl.Height=(this.Height-60)/10;
this.vl.Location=new Point(this.Width/2+(60-this.Width)/2,this.Height/2+(this.Height-60)/4+10);
this.vl.ForeColor=valuecolor;
this.Font=valuefont;
this.vl.Text=currentvalue.ToString();
//将坐标移至中心
graphics.TranslateTransform(this.Width/2,this.Height/2);
//画边框
Pen bp=new Pen(bordercolor,2);
graphics.DrawRectangle(bp,1-this.Width/2,1-this.Height/2,this.Width-1,this.Height-1);
//画背景
SolidBrush eb1=new SolidBrush(backcirclecolor);
graphics.FillEllipse(eb1,30-this.Width/2,30-this.Height/2,this.Width-60,this.Height-60);
//画刻度
Pen lp1=new Pen(linecolor,2);//主刻度画笔
Pen lp2=new Pen(linecolor,1);//副刻度画笔
SolidBrush yb=new SolidBrush(backcirclecolor);
SolidBrush tb=new SolidBrush(textcolor);//刻度值的颜色
SolidBrush atb=new SolidBrush(alarmcolor);//报警值的颜色
int cvalue=this.minvalue;//刻度值
int vinterval=(maxvalue-minvalue)/(mainscales-1);//每一大格量程值
float dit=240f/((minorscales+1)*(mainscales-1));//每一小格角度值
float td=150;//初始角度
graphics.RotateTransform(90);
RectangleF textrect=new RectangleF(-5,(60-this.Width)/2-20,35,20);
graphics.RotateTransform(-90);
graphics.RotateTransform(td);
for(int i=0;i<mainscales-1;i++)
{
graphics.DrawLine(lp1,0,0,(this.Width-60)/2,0);
graphics.RotateTransform(90);
if(isalarm&&cvalue==alarmvalue)
graphics.DrawString(cvalue.ToString(),textfont,atb,textrect);
else
graphics.DrawString(cvalue.ToString(),textfont,tb,textrect);
cvalue=cvalue+vinterval;
graphics.RotateTransform(-90);
for(int j=0;j<minorscales+1;j++)
{
graphics.DrawLine(lp2,0,0,(this.Width-60)/2,0);
graphics.RotateTransform(dit);
td=td+dit;
}
}
graphics.DrawLine(lp1,0,0,(this.Width-60)/2,0);
graphics.RotateTransform(90);
if(isalarm&&cvalue==alarmvalue)
graphics.DrawString(cvalue.ToString(),textfont,atb,textrect);
else
graphics.DrawString(cvalue.ToString(),textfont,tb,textrect);
graphics.RotateTransform(-90);
graphics.RotateTransform(-390);
graphics.FillEllipse(yb,37-this.Width/2,37-this.Height/2,this.Width-74,this.Height-74);
//画边框
Pen cbp=new Pen(circlebordercolor,3);
graphics.DrawEllipse(cbp,29-this.Width/2,29-this.Height/2,this.Width-58,this.Height-58);
//剪切表盘的下半部
SolidBrush eb2=new SolidBrush(this.BackColor);
graphics.FillRectangle(eb2,1.732f*(60-this.Width)/4,(this.Width-60)/4+5,1.732f*(this.Width-60)/2,(this.Width-60)/4+5);
//初始化指针的位置
SolidBrush pb=new SolidBrush(pointcolor);
SolidBrush rb=new SolidBrush(rotatepointcolor);
PointF[] points={point1,point2,point3,point4};
cdegree=((this.CurrentValue-this.MinValue)/(this.MaxValue-this.MinValue))*240+150;
// cdegree=270;
graphics.RotateTransform(cdegree);
graphics.FillPolygon(pb,points);
graphics.FillEllipse(rb,-4,-4,8,8);
}
private void UserControl1_Resize(object sender, EventArgs e)
{
this.Width=this.Height;
this.Invalidate();
}
[Description("报警值"),Category("参数设置")]
public int AlarmValue
{
get
{
return alarmvalue;
}
set
{
alarmvalue=value;
this.Invalidate();
}
}
[Description("指针的当前值"),Category("参数设置")]
public float CurrentValue
{
get
{
return currentvalue;
}
set
{
currentvalue=value;
DrawPoint(cdegree);
this.vl.Text=currentvalue.ToString();
if(currentvalue>alarmvalue&&isalarm&&!isdefaultalarm)
OnAlarmEvent(EventArgs.Empty);
}
}
private void DrawPoint(float degree)
{
try
{
PointF[] points={point1,point2,point3,point4};
float ddegree;
Graphics graphics=this.CreateGraphics();
SolidBrush ycb=new SolidBrush(backcirclecolor);
SolidBrush yrc=new SolidBrush(rotatepointcolor);
graphics.TranslateTransform(this.Width/2,this.Height/2);
graphics.RotateTransform(degree);
graphics.FillPolygon(ycb,points);
graphics.FillEllipse(yrc,-4,-4,8,8);
SolidBrush pb=new SolidBrush(pointcolor);
cdegree=((currentvalue-minvalue)/(maxvalue-minvalue))*240+150;
// cdegree=(currentvalue/(maxvalue-minvalue))*240+270;
ddegree=cdegree-degree;
graphics.RotateTransform(ddegree);
graphics.FillPolygon(pb,points);
graphics.FillEllipse(yrc,-4,-4,8,8);
}
catch
{
}
}
public Font TextFont
{
get
{
return textfont;
}
set
{
textfont=value;
this.Invalidate();
}
}
[Description("值的颜色"),Category("外观")]
public Color ValueColor
{
get
{
return valuecolor;
}
set
{
valuecolor=value;
this.Invalidate();
}
}
[Description("名称的颜色"),Category("外观")]
public Color NameColor
{
get
{
return namecolor;
}
set
{
namecolor=value;
this.Invalidate();
}
}
[Description("量纲的颜色"),Category("外观")]
public Color DimentionColor
{
get
{
return dimentioncolor;
}
set
{
dimentioncolor=value;
this.Invalidate();
}
}
[Description("值的字体"),Category("外观")]
public Font ValueFont
{
get
{
return valuefont;
}
set
{
valuefont=value;
this.Invalidate();
}
}
[Description("名称的字体"),Category("外观")]
public Font NameFont
{
get
{
return namefont;
}
set
{
namefont=value;
this.Invalidate();
}
}
[Description("量纲的字体"),Category("外观")]
public Font DimentionFont
{
get
{
return dimentionfont;
}
set
{
dimentionfont=value;
this.Invalidate();
}
}
[Description("仪表的名称"),Category("外观")]
public string MeterName
{
get
{
return metername;
}
set
{
metername=value;
this.Invalidate();
}
}
[Description("仪表的量纲"),Category("外观")]
public string Dimention
{
get
{
return dimention;
}
set
{
dimention=value;
this.Invalidate();
}
}
[Description("内边的颜色"),Category("外观")]
public Color CicleBorderColor
{
get
{
return circlebordercolor;
}
set
{
circlebordercolor=value;
this.Invalidate();
}
}
[Description("边框的颜色"),Category("外观")]
public Color BorderColor
{
get
{
return bordercolor;
}
set
{
bordercolor=value;
this.Invalidate();
}
}
[DllImport("kernel32.dll")]
public static extern bool Beep(int frequency, int duration);
public void Alarm()
{
for(int i=0;i<1;)
{
if(currentvalue>=alarmvalue)
Beep(2000,500);
Thread.Sleep(500);
}
}
private void BaoJing()//默认报警函数
{
if(isalarm)
{
if(isdefaultalarm)
{
if(alarmthread.IsAlive==false)
alarmthread.Start();
else
alarmthread.Resume();
}
}
else
{
if(alarmthread.IsAlive)
alarmthread.Suspend();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -