怎样用C#写一个工业仪表盘控件
来源:互联网 发布:淘宝联盟优惠卷 编辑:程序博客网 时间:2024/05/16 11:36
如何使用C#开发一个如上的工业仪表盘控件呢?
1写一个类继承自UserControl,我们给它起名为Dial
2定义他的刻度属性,
public int V//当前刻度1,省略VV(刻度2)
{
get
{
return mV;
}
set
{
if(mV==value)return;
mV=value;
Refresh();
}
}
public int MaxMain//最大刻度 省略MaxM(刻度二的最大值)
{
get
{
return mMaxMain;
}
set
{
if(mMaxMain==value)return;
mMaxMain=value;
Refresh();
}
}
3在它的绘图事件下写下绘制表盘的代码(呵呵,这是重点呃)
private void Dial_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Rectangle rect1=this.ClientRectangle;
rect1.Y+=3;
rect1.Height=rect1.Width;
e.Graphics.DrawArc(Pens.DarkGray,rect1,180+30,120);//画表盘圆弧1
Rectangle rect2=this.ClientRectangle;
rect2.Y+=3;
rect2.X+=15;
rect2.Y+=15;
rect2.Width-=30;
rect2.Height=rect2.Width;
e.Graphics.DrawArc(Pens.DarkGray,rect2,180+30,120);//画表盘圆弧2
Rectangle rect3=rect2;
rect3.X+=50;
rect3.Y+=50;
rect3.Width-=100;
rect3.Height-=100;
e.Graphics.DrawArc(Pens.DarkGray,rect3,180+30,120);//画表盘圆弧3
Rectangle rect4=rect2;
rect4.X+=10;
rect4.Y+=10;
rect4.Width-=20;
rect4.Height-=20;
e.Graphics.DrawArc(Pens.DarkGray,rect4,180+30,120);//画表盘圆弧4
int ox=rect1.Width/2+rect1.Left;
int oy=rect1.Height/2+rect1.Top;
double rad=0;
Point pt1=new Point(0,0);
Point pt2=new Point(0,0);
for(int i=0;i<120/10;i++)//画大刻度
{
double _rad=PEI/180*10;
rad=(_rad*i+(PEI/180*30));
pt1=Trans(rect1.Width/2,rad,ox,oy);
pt2=Trans(rect2.Width/2,rad,ox,oy);
e.Graphics.DrawLine(Pens.DarkGray,pt1,pt2);
pt1=Trans(rect2.Width/2,rad,ox,oy);
pt2=Trans(rect4.Width/2,rad,ox,oy);
e.Graphics.DrawLine(Pens.DarkGray,pt1,pt2);
}
for(int i=0;i<120/2;i++)//画细刻度
{
double _rad=PEI/180*2;
rad=(_rad*i+(PEI/180*30));
pt1=Trans(rect1.Width/2,rad,ox,oy);
pt2=Trans(rect1.Width/2-5,rad,ox,oy);
e.Graphics.DrawLine(Pens.DarkGray,pt1,pt2);
pt1=Trans(rect2.Width/2,rad,ox,oy);
pt2=Trans(rect2.Width/2-5,rad,ox,oy);
e.Graphics.DrawLine(Pens.DarkGray,pt1,pt2);
}
pt1=Trans(rect1.Width/2,PEI/180*30,ox,oy);
pt2=Trans(rect3.Width/2,PEI/180*30,ox,oy);
e.Graphics.DrawLine(Pens.DarkGray,pt1,pt2);//画表盘直边1
pt1=Trans(rect1.Width/2,PEI/180*150,ox,oy);
pt2=Trans(rect3.Width/2,PEI/180*150,ox,oy);
e.Graphics.DrawLine(Pens.DarkGray,pt1,pt2);//画表盘直边2
double v=120;
v/=MaxMain;
v*=V;
v=(PEI/180*v+PEI/180*30);
pt1=Trans(rect1.Width/2,v,ox,oy);
pt2=Trans(rect2.Width/2,v,ox,oy);
e.Graphics.DrawLine(Pens.Red,pt1,pt2);//画指针1
v=120;
v/=MaxMain;
v*=VV;
v=(PEI/180*v+PEI/180*30);
pt1=Trans(rect2.Width/2,v,ox,oy);
pt2=Trans(rect4.Width/2,v,ox,oy);
e.Graphics.DrawLine(Pens.Red,pt1,pt2);//画指针2
pt1=Trans(rect3.Width/2-5,PEI/180*50,ox,oy);
pt2=Trans(rect3.Width/2-5,PEI/180*130,ox,oy);
Rectangle tr=new Rectangle(pt2,new Size(pt1.X-pt2.X,15));
FontFamily fontFamily = new FontFamily("Arial");
Font font = new Font(
fontFamily,
9,
FontStyle.Regular,
GraphicsUnit.Point);
e.Graphics.DrawRectangle(Pens.DarkBlue,tr);
e.Graphics.DrawString(V.ToString()+"."+VV.ToString(),font,Brushes.Red,tr);//绘制数字窗口
font.Dispose();
}
好像一切都挺顺利,Trans(rect2.Width/2,v,ox,oy);这个东东干什么的?
这其实正是核心,让我们一起看看:
private Point Trans(int r,double rad,int ox,int oy)
{
Point pt=new Point(0,0);
int xx=(int)(r*Math.Cos(rad));
int yy=(int)(r*Math.Sin(rad));
pt.Y=-yy;
pt.X=xx;
pt.X+=ox;
pt.Y+=oy;
return pt;
}
其实它是求以ox,oy为坐标原点的一个极坐标系中的点所对应的系统中的点的。
x=r×cos(rad)
y=r×sin(rad)
很熟悉吧?
因为我们和圆弧打交道用极坐标很方便和强大,所以在Dial_Paint()中坐标在计算时都是用极坐标来描述的,在绘图时用Trans()得到GDI的直角坐标!
- 怎样用C#写一个工业仪表盘控件
- 工业仪表盘控件Iocomp常见问题(FAQ)
- Iocomp 工业仪表盘控件包控件详细介绍及下载
- 怎样用C语言写操作系统
- 怎样用C语言写操作系统
- 怎样用Java写一个简单的计算器?
- 怎样用angular写一个倒计时10秒
- 工业仪表盘控件Iocomp ActiveX/VCL更新至v4.0.8 SP4
- Iocomp工业仪表盘:Iocomp ActiveX/VCL
- canvas写仪表盘
- 怎样用Python 写一个爬图片的程序?
- 怎样用c++语言写这样一个倒置的九九乘数表
- 怎样用Java写一个动态的小时钟?
- 怎样用Python3 写一个爬图片的程序?
- 怎样用java写一个简单的文件复制程序
- android一款仪表盘控件
- Flex实现仪表盘控件
- android 仪表盘控件
- 用CG写自己的光照程序
- 下定决心
- 负载均衡--大型在线系统实现的关键(上篇)(再谈QQ游戏百万人在线的技术实现)
- 如何建立DNS服务器
- 关于举办上海地区Dotnet技术交流会的建议
- 怎样用C#写一个工业仪表盘控件
- Top 15 Ant Best Practices
- 白云山天然岩壁介绍
- Ant实践
- 移动流媒体技术及其应用发展方向(转载)
- 今天帮同学买了个笔记本
- [翻译]抓屏的各种方法
- 一篇有关软件架构设计的文章(转自软件工程专家网,注意其中有关性能的内容)
- 终于考完G了