}
private void Form1_Load(object sender, EventArgs e) {
Cline ob = new Cline(); fs2 = new MemoryStream();
formatter = new BinaryFormatter(); formatter.Serialize(fs2, ob);// buffer = fs2.ToArray();
//sizeofCline = buffer.Length; fs2.Close();
/*//////////////计算直线对象的大小///////////////////// fs2 = new MemoryStream();
formatter = new BinaryFormatter(); formatter.Serialize(fs2, ob); buffer = fs2.ToArray();
sizeofcline = buffer.Length; fs2.Close();
//////////////计算矩形对象的大小/////////////////// fs2 = new MemoryStream();
CRectangle ob2 = new CRectangle(); formatter.Serialize(fs2, ob2); buffer = fs2.ToArray();
sizeofCRectangle = buffer.Length;
MessageBox.Show(sizeofcline.ToString() + \ */ }
private void Form1_MouseDown(object sender, MouseEventArgs e) {//选中直线
Cline ob = new Cline(); x1 = e.X; y1 = e.Y;
index = -1;//
double L, L1, L2, d; double alfa = 4.5;
for (int i = 0; i < arrline.Count; i++) {
ob = (Cline)arrline[i];//
L = Math.Sqrt((ob.x1 - ob.x2) * (ob.x1 - ob.x2) + (ob.y1 - ob.y2) * (ob.y1 - ob.y2));
L2 = Math.Sqrt((x1 - ob.x2) * (x1 - ob.x2) + (y1 - ob.y2) * (y1 - ob.y2)); L1 = Math.Sqrt((x1 - ob.x1) * (x1 - ob.x1) + (y1 - ob.y1) * (y1 - ob.y1)); d = L2 + L1 - L;// if (d < alfa) {
index = i;
SLb3.Text = index.ToString(); return; } } }
private void Form1_MouseMove(object sender, MouseEventArgs e) {//显示鼠标坐标
SLb1.Text = e.X.ToString(); SLb2.Text = e.Y.ToString(); }
private void Form1_MouseUp(object sender, MouseEventArgs e) {
x2 = e.X; y2 = e.Y;
Graphics g = this.CreateGraphics(); Pen pen = new Pen(C, w);
//w = int.Parse(tTxtBox1.Text); //Cline ob = new Cline();
if (flag == 1) {//画线
g.DrawLine(pen, new Point(x1, y1), new Point(x2, y2)); Cline ob = new Cline();
ob.x1 = x1; ob.y1 = y1; ob.x2 = x2; ob.y2 = y2;//// ob.c = C; ob.width = w; arrline.Add(ob); }
if (flag == 2) // 画矩形 {
g.DrawRectangle(pen, new Rectangle(x1, y1, Math.Abs(x2 - x1), Math.Abs(y2 - y1))); Cline ob = new Cline();
ob.x1 = x1; ob.y1 = y1; ob.x2 = x2; ob.y2 = y2; ob.c = C; ob.width = w; arrRectangle.Add(ob); }
if (flag == 3) {
g.DrawEllipse(pen, x1, y1, x2 - x1, y2 - y1);//画椭圆 Cline ob = new Cline();
ob.x1 = x1; ob.y1 = y1; ob.x2 = x2; ob.y2 = y2; ob.c = C; ob.width = w; arrEllipse.Add(ob); }
if (flag == 100 && index >= 0) {//移动
Cline ob = new Cline(); ob = (Cline)arrline[index]; ob.x1 += x2 - x1; ob.y1 += y2 - y1; ob.x2 += x2 - x1; ob.y2 += y2 - y1; Invalidate();//
}
}
private void Form1_Paint(object sender, PaintEventArgs e)
{//重绘3个循环 Cline ob;
for (int i = 0; i < arrline.Count; i++) {
ob = (Cline)arrline[i];
Graphics g = this.CreateGraphics(); Pen pen = new Pen(ob.c ,ob.width );
// ob = new Cline();
//pen.Color = ob.c; pen.Width = ob.width;
g.DrawLine(pen, new Point(ob.x1, ob.y1), new Point(ob.x2, ob.y2));
}
for (int i = 0; i < arrRectangle.Count; i++) {
ob = (Cline)arrRectangle[i];
Graphics g = this.CreateGraphics(); Pen pen = new Pen(ob.c, ob.width );
// pen.Color = ob.c; pen.Width = ob.width;
g.DrawRectangle(pen, new Rectangle(ob.x1, ob.y1, Math.Abs(ob.x2 - ob.x1), Math.Abs(ob.y2 - ob.y1)));
}
for (int i = 0; i < arrEllipse.Count; i++) {
ob = (Cline)arrEllipse[i];
Graphics g = this.CreateGraphics(); Pen pen = new Pen(ob.c, ob.width);
g.DrawEllipse(pen, ob.x1, ob.y1, Math.Abs(ob.x2 - ob.x1), Math.Abs(ob.y2 - ob.y1)); } }
private void MOpen_Click(object sender, EventArgs e) {//文件打开
OpenFileDialog dlg = new OpenFileDialog();
if (dlg.ShowDialog() != DialogResult.OK) return;
FileStream fs = new FileStream(dlg.FileName, FileMode.Open);// BinaryReader reader = new BinaryReader(fs);
int n = 0; int m = 0; int x = 0;
arrline.Clear();
arrRectangle.Clear(); arrEllipse.Clear(); n = reader.ReadInt32(); m = reader.ReadInt32(); x = reader.ReadInt32(); Cline ob, ob1, ob2;
for (int i = 0; i < n; i++) {
ob = new Cline();
reader.Read(buffer, 0, buffer.Length); fs2 = new MemoryStream(buffer);
ob = (Cline)formatter.Deserialize(fs2); arrline.Add(ob); }
for (int i = 0; i < m; i++) {
ob1 = new Cline();
reader.Read(buffer, 0, buffer.Length); fs2 = new MemoryStream(buffer);
ob1 = (Cline)formatter.Deserialize(fs2); arrEllipse.Add(ob1); }
for (int i = 0; i < x; i++) {
ob2 = new Cline();
reader.Read(buffer, 0, buffer.Length); fs2 = new MemoryStream(buffer);
ob2 = (Cline)formatter.Deserialize(fs2); arrRectangle.Add(ob2); }
reader.Close(); fs.Close();
this.Invalidate();//刷新
}
private void MColor_Click(object sender, EventArgs e) {
ColorDialog dlg = new ColorDialog();
if (dlg.ShowDialog() != DialogResult.OK) return; C = dlg.Color; }
private void MSave_Click(object sender, EventArgs e) {//文件保存
SaveFileDialog dlg = new SaveFileDialog();
if (dlg.ShowDialog() != DialogResult.OK) return;
FileStream fs = new FileStream(dlg.FileName, FileMode.Create); BinaryWriter writer = new BinaryWriter(fs);
int n = arrline.Count; int m = arrEllipse.Count; int x = arrRectangle.Count; writer.Write(n); Cline ob, ob1, ob2;
for (int i = 0; i < n; i++) {
ob = (Cline)arrline[i]; fs2 = new MemoryStream();
formatter.Serialize(fs2, ob); buffer = fs2.ToArray();
writer.Write(buffer, 0, buffer.Length);
}
for (int i = 0; i < m; i++) {
ob1 = (Cline)arrEllipse[i]; fs2 = new MemoryStream();
formatter.Serialize(fs2, ob1); buffer = fs2.ToArray();
writer.Write(buffer, 0, buffer.Length); }
for (int i = 0; i < x; i++) {
ob2 = (Cline)arrRectangle[i]; fs2 = new MemoryStream();
formatter.Serialize(fs2, ob2); buffer = fs2.ToArray();
writer.Write(buffer, 0, buffer.Length); }
writer.Close(); fs.Close();
}
private void MExit_Click(object sender, EventArgs e) {
this.Close(); }
private void Line_Click(object sender, EventArgs e) {
flag = 1; }
private void Rectangle_Click(object sender, EventArgs e) {
flag = 2; }
private void Ellipse_Click(object sender, EventArgs e) {
flag = 3; }
private void Move_Click(object sender, EventArgs e) {
flag = 100; }
}
[Serializable()] //系列化3,加到类Cline头上 public class Cline {
public int x1, x2, y1, y2; public Color c; public int width;