1、使用C# 创建 客户端程序;
2、程序能够实现创建如上图带有x轴和Y轴的坐标,能够读取ACCESS中坐标系中8个点的坐标位置初始值,并绘制8个点连成的曲线(如上图示)。
3、要求能够用鼠标左键来调整坐标中的点,来改变曲线的形态,同时修改点在坐标中的位置,并将修改后的点的位置保存到本地ACCESS中。
4、对话框中的坐标能够随着对话框的放大、缩小,同比例变化 。
1、使用C# 创建 客户端程序;
2、程序能够实现创建如上图带有x轴和Y轴的坐标,能够读取ACCESS中坐标系中8个点的坐标位置初始值,并绘制8个点连成的曲线(如上图示)。
3、要求能够用鼠标左键来调整坐标中的点,来改变曲线的形态,同时修改点在坐标中的位置,并将修改后的点的位置保存到本地ACCESS中。
4、对话框中的坐标能够随着对话框的放大、缩小,同比例变化 。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Q1055754
{
public partial class Form1 : Form
{
private List<DataPoint> Points = new List<DataPoint>();
private int SelectedPointID = -1;
private bool isInsert = false;
public Form1()
{
InitializeComponent();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.FillRectangle(Brushes.White, pictureBox1.ClientRectangle);
var pts = Points.OrderBy(x => x.X).ToList();
if (pts.Count > 0)
{
e.Graphics.DrawEllipse(pts[0].ID == SelectedPointID ? Pens.Red : Pens.Black,
(int)(pts[0].X * pictureBox1.ClientSize.Width) - 5,
(int)(pts[0].Y * pictureBox1.ClientSize.Height) - 5,
10, 10);
for (int i = 1; i < pts.Count; i++)
{
e.Graphics.DrawLine(Pens.Blue,
(float)(pts[i - 1].X * pictureBox1.ClientSize.Width),
(float)(pts[i - 1].Y * pictureBox1.ClientSize.Height),
(float)(pts[i].X * pictureBox1.ClientSize.Width),
(float)(pts[i].Y * pictureBox1.ClientSize.Height));
e.Graphics.DrawEllipse(pts[i].ID == SelectedPointID ? Pens.Red : Pens.Black,
(int)(pts[i].X * pictureBox1.ClientSize.Width) - 5,
(int)(pts[i].Y * pictureBox1.ClientSize.Height) - 5,
10, 10);
}
}
}
private void button2_Click(object sender, EventArgs e)
{
isInsert = true;
}
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
if (isInsert)
Cursor = Cursors.Cross;
else
Cursor = Cursors.Arrow;
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
Cursor = Cursors.Arrow;
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
if (isInsert)
{
int newid = Points.Count == 0 ? 0 : Points.Select(x => x.ID).Max() + 1;
var point = new DataPoint()
{
ID = newid,
X = e.X / (double)pictureBox1.ClientSize.Width,
Y = e.Y / (double)pictureBox1.ClientSize.Height
};
Points.Add(point);
SelectedPointID = newid;
isInsert = false;
Cursor = Cursors.Arrow;
pictureBox1.Refresh();
}
else
{
var point = Points.Where(x => Math.Abs(x.X * (double)pictureBox1.ClientSize.Width - e.X) < 5 &&
Math.Abs(x.Y * (double)pictureBox1.ClientSize.Height - e.Y) < 5).FirstOrDefault();
if (point == null) SelectedPointID = -1; else SelectedPointID = point.ID;
pictureBox1.Refresh();
}
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (!isInsert) Cursor = Cursors.Arrow;
if (e.Button == System.Windows.Forms.MouseButtons.Left && !isInsert)
{
var point = Points.FirstOrDefault(x => x.ID == SelectedPointID);
if (point != null)
{
point.X = e.X / (double)pictureBox1.ClientSize.Width;
point.Y = e.Y / (double)pictureBox1.ClientSize.Height;
pictureBox1.Refresh();
}
}
}
private void button3_Click(object sender, EventArgs e)
{
var point = Points.FirstOrDefault(x => x.ID == SelectedPointID);
if (point != null)
Points.Remove(point);
SelectedPointID = -1;
pictureBox1.Refresh();
}
private void pictureBox1_Resize(object sender, EventArgs e)
{
pictureBox1.Refresh();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofn = new OpenFileDialog();
if (ofn.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=""" + ofn.FileName + @"""");
conn.Open();
OleDbCommand cmd = new OleDbCommand("select * from tb_Points", conn);
var reader = cmd.ExecuteReader();
Points.Clear();
SelectedPointID = -1;
while (reader.Read())
{
Points.Add(new DataPoint()
{
ID = (int)reader["ID"],
X = (double)reader["X"],
Y = (double)reader["Y"]
});
}
pictureBox1.Refresh();
reader.Close();
conn.Close();
}
}
private void button4_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=""" + sfd.FileName + @"""");
conn.Open();
OleDbCommand cmd = new OleDbCommand("delete * from tb_Points", conn);
cmd.ExecuteNonQuery();
foreach (var item in Points)
{
cmd.CommandText = string.Format("insert into tb_Points(id, x, y) values({0}, {1}, {2})", item.ID, item.X, item.Y);
cmd.ExecuteNonQuery();
}
conn.Close();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Q1055754
{
class DataPoint
{
public int ID { get; set; }
public double X { get; set; }
public double Y { get; set; }
}
}