[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Tab;
namespace DrawCurve
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int MaxX = 200;//设置x的最大坐标
private int MaxY = 230;//设置y的最大坐标
private Graphics graXY;//创建图板
private Graphics graMove;//创建图板
private Pen Dpen = new Pen(Brushes.Black, 1);//设置画笔
private Font Dfont = new Font("宋体", 8f);//设置字体格式
private int xx = 0;//设置坐标
private int yy = 0;//设置运动原坐标
private Point MovePoint1;//动点1
private Point MovePoint2;//动点2
int a = 0;
public void DrawXY()
{
Bitmap bt = new Bitmap(400, 400);
graXY= Graphics.FromImage(bt);
//画出x轴和y轴
graXY = panel1.CreateGraphics();//在panel1上创建画图
//MaxX = System.Convert.ToInt32(MaxX.Text);
//绘制x轴
Point px1 = new Point(5, MaxY);//坐标系x轴起点
Point px2 = new Point(MaxX + 5, MaxY);//坐标系x轴终点
graXY.DrawLine(new Pen(Brushes.Black, 1), px1, px2);//连接两点确定坐标轴x轴
//绘制y轴
Point py1 = new Point(5, MaxY);//坐标系y轴起点
Point py2 = new Point(5, 0);//坐标系y轴终点
graXY.DrawLine(Dpen, py1, py2);//连接两点确定坐标轴y轴
//x轴的刻度线
for (int i = 0; i <= 20; i++)//做循环,20等分x轴,标记每个等分线
{
Point pxx1 = new Point(5 + MaxX * i / 20, MaxY);//刻度线起点
Point pxx2 = new Point(5 + MaxX * i / 20, MaxY - 5);//刻度线终点
string sx = null;//新建string类型 sy
sx = (MaxY * i / 20).ToString();//sy为每个刻度的值
graXY.DrawLine(Dpen, pxx1, pxx2);//画出刻度线
if (i % 4 == 0)
{
graXY.DrawString(sx, Dfont, Brushes.Black, new Point(MaxX * i / 20 + 5, MaxY));
//对部分等距标记坐标
}
}
graXY.DrawString("x轴", Dfont, Brushes.Black, new Point(MaxX + 5, MaxY + 10));//标记x轴
//y轴分度线
for (int i = 0; i <= 20; i++)//20等分y轴
{
Point pyy1 = new Point(5, MaxY - MaxY * i / 20);//刻度线的起点
Point pyy2 = new Point(10, MaxY - MaxY * i / 20);//刻度线的终点
string sy = null;//新建sy存储当前位置刻度
sy = (MaxY * i / 20).ToString();
graXY.DrawLine(Dpen, pyy1, pyy2);//连接画出刻度线
if (i % 4 == 0)
{
graXY.DrawString(sy, Dfont, Brushes.Black, new PointF(5, MaxY - MaxY * i / 20));
//标记部分刻度
}
}
graXY.DrawString("y轴", Dfont, Brushes.Black, new Point(20, 0));//标记y轴
this.CreateGraphics().DrawImage(bt, 0, 0);
}
public class PanelEx : Panel
{
public PanelEx()
{
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
SetStyle(ControlStyles.DoubleBuffer, true);
}
}
private void OnTime(object sender,EventArgs e)
{
//graMove = Graphics.FromImage(bt);
Pen LinePen = new Pen(Brushes.Red, 2);//运动轨迹线条格式
this.MovePoint2.X = xx+5;//将当前坐标的x值赋值到点2中,并转化到当前坐标系坐标
this.MovePoint2.Y = MaxY - yy;//将当前坐标的y值赋值到点2中,并转化到当前坐标系
graMove.DrawLine(LinePen, MovePoint1, MovePoint2);//连接点1点2
MovePoint1.X = this.MovePoint2.X;//将点2的值赋给点1
MovePoint1.Y = this.MovePoint2.Y;
this.Text="("+xx.ToString()+" , "+yy.ToString()+")";//当前窗体文本显示当前机械位置坐标
if (a %2== 1)
{//循环增加xy值
xx++;
yy++;
}
else {
timer1.Enabled = false;
}
}
private void Form1_Load(object sender, EventArgs e)
{
this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
this.UpdateStyles();
//panel1.BackgroundImage = bmp;
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
DrawXY();
}
private void button1_Click(object sender, EventArgs e)
{
DrawXY();//调用画xy
//graMove.Clear(Color.White);
//Refresh();
}
private void button2_Click(object sender, EventArgs e)
{
MovePoint1.X = xx + 5;//将坐标点的x值转化到当前坐标系
MovePoint1.Y = MaxY - yy;//将坐标点的y值转化到当前坐标系
graMove = panel1.CreateGraphics();//在panel1上创建画图
DrawXY();//调用画xy轴
timer1.Interval = 50;//调整响应时间
timer1.Enabled = true;//时钟可以触发
timer1.Tick += new EventHandler(OnTime);//循环触发时钟,并保留循环结果
// DrawXY();
a++;
}
private void button3_Click(object sender, EventArgs e)
{
//graMove.Clear(Color.White);
Refresh();
DrawXY();//调用画xy轴
}
}
}