|
楼主 |
发表于 2009-8-1 09:10:11
|
显示全部楼层
用C#写很简单,自定义一个用户控制,写上代码如下,然后加一个串口控制和自定的的这个控制,接收数据显示就行:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace UartLine
{
public partial class Curve : UserControl
{
private int[] nodes = new int[900];
private int pNodes = 899;
private Size grid = new Size(20,20); //栅格大小
public Curve()
{
InitializeComponent();
}
public Size Grid
{
get
{
return grid;
}
set
{
if ((value.Width > 0) && (value.Width < 100)
&& (value.Height > 0) && (value.Height < 100))
{
grid = value;
}
}
}
//绘制背景栅格并清屏
public void DrawGrid(Graphics g)
{
Pen pen = new Pen(Color.DarkSlateBlue);
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
//清屏
g.FillRectangle(new SolidBrush(Color.Black), this.ClientRectangle);
//绘栅格
for (int i = this.Height; i >0; )
{
g.DrawLine(pen, new Point(0, i), new Point(this.Width, i));
i -= grid.Height;
}
for (int j = 0; j < this.Width ;)
{
g.DrawLine(pen, new Point(j,this.Height), new Point(j,0));
j += grid.Width;
}
}
//绘制曲线
public void DrawLine(Graphics g)
{
Pen pen = new Pen(Color.White);
for (int i = 0; i < pNodes; i++)
{
g.DrawLine(pen, new Point(i, this.Height - nodes), new Point(i + 1, this.Height - nodes[i + 1]));
}
}
//绘制节点
public void DrawNode(Graphics g,int p)
{
Pen pen = new Pen(Color.Red);
Font drawFont = new Font("Arial", 12);
SolidBrush drawBrush = new SolidBrush(Color.Red);
g.DrawLine(pen, new Point(p, Height), new Point(p, 0));
g.DrawLine(pen, new Point(0, Height - nodes[p]), new Point(Width, Height - nodes[p]));
Point strPos=new Point(p+10,Height - nodes[p]);
if (strPos.X > this.Width - 100)
{
strPos.X -= 100;
}
if (strPos.Y > this.Height - 40)
{
strPos.Y -= 20;
}
g.DrawString(String.Format("X:{0} Y:{1}", p, nodes[p]), drawFont, drawBrush, strPos);
}
//重绘
private void OnPaint(object sender, PaintEventArgs e)
{
DrawGrid(e.Graphics);
DrawLine(e.Graphics);
}
//装载
private void Curve_Load(object sender, EventArgs e)
{
}
//点击关键点
private void OnClick(object sender, MouseEventArgs e)
{
Graphics g = this.CreateGraphics();
this.Refresh();
if (e.Button == MouseButtons.Left)
{
DrawNode(g, e.X);
}
}
//增加节点
public void AddNode(int p)
{
Graphics g = this.CreateGraphics();
Pen pen = new Pen(Color.White);
if (pNodes < 899)
{
pNodes++;
nodes[pNodes] = p;
g.DrawLine(pen, new Point(pNodes - 1, Height - nodes[pNodes - 1]),
new Point(pNodes, Height - nodes[pNodes]));
}
else
{
pNodes = 0;
DrawGrid(g);
DrawLine(g);
}
}
}
} |
|