在txt文件里写入坐标,绘制图形,例如x1,y1,x2,y2,x3,y3。
鼠标位于坐标系内,显示2条红色十字线,和坐标值,离开坐标系,2者消失。
源码:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace TEST
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string resultFile;
private Point mouseOffset;
bool flag=false;
ToolTip tooltip = new ToolTip();
int[] bufx=new int[1024];
int[] bufy = new int[1024];
int drawflag=0;
int readlen = 0;
// x1,y1为鼠标移动的坐标,画十字线用,xbuf,ybuf绘制点坐标数组
void draw(int x1,int y1,int[] xbuf,int[] ybuf)
{
//定义画布大小
int height = 350, width = 800;
//创建位图
System.Drawing.Bitmap image = new System.Drawing.Bitmap(width, height);
//创建Graphics类对象
Graphics g = Graphics.FromImage(image);
//清空图片背景色
g.Clear(Color.White);
Font font = new System.Drawing.Font("Arial", 9, FontStyle.Regular);
Font font1 = new System.Drawing.Font("宋体", 20, FontStyle.Regular);
Font font2 = new System.Drawing.Font("Arial", 8, FontStyle.Regular);
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.Blue, 1.2f, true);
g.FillRectangle(Brushes.SeaGreen, 0, 0, width, height);
Brush brush1 = new SolidBrush(Color.Blue);
Brush brush2 = new SolidBrush(Color.SaddleBrown);
//画图片的边框线
g.DrawRectangle(new Pen(Color.Black), 40, 40, 720, 270);
g.DrawRectangle(new Pen(Color.Black), 40, 35, 720, 275);
System.Drawing.Pen mypen = new Pen(brush, 1);
System.Drawing.Pen mypen2 = new Pen(Color.Red, 2);
System.Drawing.Pen mypen3 = new Pen(Color.Yellow,1);
//绘制线条
//绘制纵向线条
int x = 80;
for (int i = 0; i < 17; i++)
{
g.DrawLine(mypen, x, 40, x, 310);
x = x + 40;
}
//Pen mypen1 = new Pen(Color.Blue, 2);
//g.DrawLine(mypen1, x - 480, 80, x - 480, 340);
//绘制横向线条
int y = 70;
for (int i = 0; i < 8; i++)
{
g.DrawLine(mypen, 40, y, 760, y);
y = y + 30;
}
//x轴上对应的标记
String[] n = { " 10", " 20", " 30", " 40", " 50", " 60", " 70",
" 80", " 90", " 100", "110", "120", "130", "140", "150", "160", "170"};
x = 70;
for (int i = 0; i < 17; i++)
{
g.DrawString(n[i].ToString(), font, Brushes.Red, x, 315); //设置文字内容及输出位置
x = x + 40;
}
//y轴上对应的标记
String[] m = {"40", "35", "30", "25", "20", "15", "10",
" 5"};
y = 60;
for (int i = 0; i < 8; i++)
{
g.DrawString(m[i].ToString(), font, Brushes.Red, 16, y); //设置文字内容及输出位置
y = y + 30;
}
if (drawflag == 1)
{
for (int i = 0; i 2; i++)
{
g.DrawEllipse(mypen2, xbuf[i] - 2, ybuf[i] - 2, 4, 4);
if (i < readlen / 2 - 1)
g.DrawLine(mypen3,xbuf[i], ybuf[i] ,xbuf[i + 1],ybuf[i+1]);
}
}
if (flag == true)
{
g.DrawLine(mypen2, x1, 41, x1, 310);
g.DrawLine(mypen2, 41, y1, 760, y1);
}
g.Dispose();
this.pictureBox1.Image = image;
}
private void Form1_Load(object sender, EventArgs e)
{
label1.Visible = false;
label1.Parent = pictureBox1;
draw(1, 1, bufx, bufy);
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
}
private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 f=new Form2();
f.Show();
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
int ex = e.X;
int ey = e.Y;
mouseOffset.X= (e.X-40)/4;
mouseOffset.Y=(310- e.Y)/6;
if ((ex > 40 && ex < 760) && (ey > 40 && ey < 310))
{
flag = true;
label1.BackColor = Color.FromArgb(80, Color.White);
label1.Visible = true;
label1.Text = ("坐标值:" + "\n" + "x=" + mouseOffset.X + "\n" + "y=" + mouseOffset.Y);
label1.Location = new Point(ex+20, ey+10);
// this.Cursor = Cursors.Cross;
//tooltip.Show("x=" + e.X + ";y=" + e.Y, this, new Point(MousePosition.X, MousePosition.Y));
}
else
{
flag = false;
label1.Visible = false;
// this.Cursor = Cursors.Arrow;
}
draw(ex, ey, bufx, bufy);
}
private void buttonFile_Click(object sender, EventArgs e)
{
OpenFileDialog OpenFile = new OpenFileDialog();
// OpenFile.InitialDirectory = "E:\\1work\\FILE";//初始目录
OpenFile.Filter = "All files(*.*)|*.*|txt files(*.txt)|*.txt";//文本筛选
OpenFile.FilterIndex = 1;//文本筛选器索引,选择第一项就是1
OpenFile.RestoreDirectory = true;
if (OpenFile.ShowDialog() == DialogResult.OK)
resultFile = OpenFile.FileName;
textBoxFile.Text = resultFile;
}
private void buttonDraw_Click(object sender, EventArgs e)
{
if (resultFile == null)
{
MessageBox.Show("请选取要绘图的文件!", "错误",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
else
{
StreamReader fileRead = new StreamReader(File.Open(textBoxFile.Text, FileMode.Open), System.Text.Encoding.Default);
string txtLine = fileRead.ReadLine();
readlen = 0;
while (true)
{
txtLine = fileRead.ReadLine();
if (txtLine == null)
break;
if (txtLine.Length >= 3)
{
String[] date = txtLine.Split(',');
int len = date.Length;
//label2.Text =Convert.ToString(date[11]);
int j = 0;
for (int i = 0; i < len / 2; i++)
{//x:70 ,4* x=70+4*d;
//y:310,*6 y=310-6*d
bufx[readlen/2 + i] = Convert.ToInt32(date[j]) * 4 + 40;
bufy[readlen/2 + i] = 310 - Convert.ToInt32(date[j + 1]) * 6;
j += 2;
}
readlen += len;
}
}
fileRead.Close();
drawflag = 1;
draw(1, 1, bufx, bufy);
label2.Text = Convert.ToString("输出点个数:"+readlen/2);
}
}
private void buttonClear_Click(object sender, EventArgs e)
{
drawflag = 0;
draw(1, 1, bufx, bufy);
label2.Text = Convert.ToString("输出点个数:");
}
}
}