关于GDI+的问题。我的paint 事件哪里错了呢?为什么最小化以后,所绘的图像还是消失了?
程序还没有写完。为什么每次最小化以后,以前所绘的图像都消失了,是paint事件错了吗?using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Collections;
namespace shixun
{
public partial class Form1 : Form
{
public int n; //图形形状
Point p1 = Point.Empty, p2 = Point.Empty;
Color A=Color.Black, B; //线条颜色,填充颜色
bool moused = false; //记下鼠标是否按下
public ArrayList array = new ArrayList(); //重绘
public int Xt=1; //线条粗细
int k;
public Form1()
{
InitializeComponent();
}
private void radioButLine_CheckedChanged(object sender, EventArgs e)
{
n = 1;
}
private void radioButRecetangle_CheckedChanged(object sender, EventArgs e)
{
n = 2;
}
private void radioButEllipse_CheckedChanged(object sender, EventArgs e)
{
n = 3;
}
private void radioButRece_CheckedChanged(object sender, EventArgs e)
{
n = 4;
}
private void radioButEllip_CheckedChanged(object sender, EventArgs e)
{
n = 5;
}
private void 线条颜色ToolStripMenuItem_Click(object sender, EventArgs e)
{
ColorDialog Cdh = new ColorDialog();
Cdh.ShowDialog();
A = Cdh.Color;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
switch (n)
{
case 1:
if (moused)
{
Graphics g = this.pictureBox1.CreateGraphics();
Pen pen = new Pen(Color.White, Xt);
g.DrawLine(pen, p1, p2); //清除前一根直线
p2.X = e.X; p2.Y = e.Y;
pen = new Pen(A, 1);
g.DrawLine(pen, p1, p2);
g.Dispose();
}
break;
case 2:
if (moused)
{
Graphics g1 = this.pictureBox1.CreateGraphics();
Pen pen1 = new Pen(Color.White, Xt);
g1.DrawRectangle(pen1, p1.X, p1.Y, p2.X - p1.X, p2.Y - p1.Y);
p2.X = e.X; p2.Y = e.Y;
float b, c;
b = p2.X - p1.X;
c = p2.Y - p1.Y;
Pen pen2 = new Pen(A, 1);
g1.DrawRectangle(pen2, p1.X, p1.Y, b, c);
g1.Dispose();
}
break;
case 3:
if (moused)
{
Graphics g1 = this.pictureBox1.CreateGraphics();
Pen pen3 = new Pen(Color.White, Xt);
g1.DrawRectangle(pen3, p1.X, p1.Y, p2.X - p1.X, p2.Y - p1.Y);
p2.X = e.X; p2.Y = e.Y;
float b, c;
b = p2.X - p1.X;
c = p2.Y - p1.Y;
Pen pen4 = new Pen(A, Xt);
g1.DrawRectangle(pen4, p1.X, p1.Y, b, c);
g1.Dispose();
}
break;
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
Cline ob = new Cline();
Pen pen = new Pen(A);
pen.Width = Xt;
Graphics g = this.pictureBox1.CreateGraphics();
switch (n)
{
case 1:
p2.X = e.X;
p2.Y = e.Y;
g.DrawLine(pen, p1, p2);
moused = false;
ob.x1 = p1.X; ob.y1 = p1.Y; ob.x2 = p2.X; ob.y2 = p2.Y; ob.s = pen.Width; ob.c = A; ob.k = 1;
array.Add(ob);
break;
case 2:
p2.X = e.X; p2.Y = e.Y;
float Rectangleb, Rectanglec;
Rectangleb = p2.X - p1.X;
Rectanglec = p2.Y - p1.Y;
g.DrawRectangle(pen, p1.X, p1.Y, Rectangleb, Rectanglec);
moused = false;
ob.x1 = p1.X; ob.y1 = p1.Y; ob.x2 = p2.X; ob.y2 = p2.Y; ob.s = pen.Width; ob.c = A; ob.k = 2;
array.Add(ob);
break;
case 3:
p2.X = e.X; p2.Y = e.Y;
float Roundb, Roundc;
Roundb = p2.X - p1.X;
Roundc = p2.Y - p1.Y;
g.DrawEllipse(pen, p1.X, p1.Y, Roundb, Roundc);
moused = false;
ob.x1 = p1.X; ob.y1 = p1.Y; ob.x2 = p2.X; ob.y2 = p2.Y; ob.s = pen.Width; ob.c = A; ob.k = 3;
array.Add(ob);
break;
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
p1.X = e.X;
p1.Y = e.Y;
moused = true;
}
private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
{
array.Clear();
this.Invalidate();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics g = this.pictureBox1.CreateGraphics();
Cline ob;
for (int i = 0; i < array.Count; i++)
{
ob = (Cline)array[i];
Pen pen = new Pen(ob.c);
k = ob.k;
pen.Width = ob.s;
if (k == 1)
{
g.DrawLine(pen, new Point(ob.x1, ob.y1), new Point(ob.x2, ob.y2));
}
if (k == 2)
{
g.DrawRectangle(pen,
ob.x1, ob.y1,
ob.x2 - ob.x1, ob.y2 - ob.y1);
}
if (k == 3)
{
g.DrawEllipse(pen,
ob.x1, ob.y1,
ob.x2 - ob.x1, ob.y2 - ob.y1);
}
}
}
}
[Serializable()]
public class Cline //记录所绘图形
{
public Color c;
public float s;
public int k;
public int x1;
public int y1;
public int x2;
public int y2;
}
}