HreoWinGauge2.0 试用版下载地址:http://download.csdn.net/source/6896899
(10)使用速度表
在TestSpeedMeter.cs文件头部添加HeroWinGauge命名空间的引用:“using HeroWinGauge;”
在TestSpeedMeter类中添加三个私有变量:private CircularPointer cp;private CircularScale cs;private double angle 。并添加“TestSpeedMeter”窗体的Load事件响应函数。
在“TestSpeedMeter”窗体中添加如下三个控件:
添加一个Button控件,更改属性如下:“Name”设为“btnStartStop”;“Text”设为“Start/Stop”。并添加Click事件响应函数。
添加一个Button控件,更改属性如下:“Name”设为“btnClear”;“Text”设为“Clear”。并添加Click事件响应函数。
添加一个Timer(定时器)控件,更改属性如下:“Name”设为“timer1”;“Interval”设为“50”。并添加Tick事件响应函数。
在“TestSpeedMeter”窗体中调整好两个Button控件的位置,效果见图7.1-14
图7.1-14 “TestSpeedMeter”窗体7
最后向各事件响应函数添加程序,完整的程序代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using HeroWinGauge;
namespace HeroWinGaugeSamples
{
public partial class TestSpeedMeter : Form
{
private CircularPointer cp;
private CircularScale cs;
private double angle;
public TestSpeedMeter()
{
InitializeComponent();
}
private void TestSpeedMeter_Load(object sender, EventArgs e)
{
cp = (HeroWinGauge.CircularPointer)gaugeContainer1.GaugeComponents["GaugeComponent0"].MiddleLayerParts["CircularPointer0"];
cs = (HeroWinGauge.CircularScale)gaugeContainer1.GaugeComponents["GaugeComponent0"].BottomLayerParts["CircularScale0"];
angle = 270;
cp.Value = cs.StartValue;
}
private void btnStartStop_Click(object sender, EventArgs e)
{
if (this.timer1.Enabled)
{
this.timer1.Stop();
}
else
{
this.timer1.Start();
}
}
private void btnClear_Click(object sender, EventArgs e)
{
cp.Value = cs.StartValue;
angle = 270;
}
private void timer1_Tick(object sender, EventArgs e)
{
angle = angle + 1;
if (angle >= 360)
angle = 0;
double d = (cs.EndValue - cs.StartValue) / 2;
cp.Value = cs.StartValue + d* (1 + Math.Sin(angle * Math.PI / 180));
}
}
}
注意:在以上代码的TestSpeedMeter_Load函数中,速度表的指针对象引用cp和刻度对象引用cs付值的代码都比较长,使用者可能会觉得比较麻烦。如何取得速度表中指针对象和刻度对象的引用?HeroWinGauge已考虑到这个问题,并已具有快速获得内部对象引用的功能,且操作非常简单。用户只须在“TestSpeedMeter”窗体中直接点击所需引用的对象,使该对象出现矩形选择框,然后点击右上方的三角形智能标记,在弹出的智能操作面板,点击“Task for selectded object” 选项卡中的“Information”选项,此时打开一个“Selected Object Information”对话框(见图7.1-15),在其中的“Program Symbol”项中显示的内容就是对该对象引用的程序代码,用户可点击对话框右上方的“Copy program symbol to clipboard”字符串,就可将此对象引用的程序代码复制到Windows的剪贴板中,然后使用者就可将其粘贴到程序中。
图7.1-15 “Selected Object Information”对话框
(11)运行程序
到此,一个完整的速度表演示程序已全部完成,可以编译运行。运行此程序时,可按“Start/Stop”按钮启动或停止速度表,按“Clear”按钮清零速度表。