有个问题请教一下大家
我刚学c#,做了一个简单画板,用画笔在画板上滑了一些内容,但是一旦最小化或者内容被遮挡就会消失。请问这是怎么回事呢。附上源代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//画板内容消失
//三角形画不出来;
//很多需要优化的地方
//还想实现插入图片
namespace WindowsHuaBu
{
public partial class Form1 : Form
{
Point start, end;
Bitmap bmp;
public Form1()
{
InitializeComponent();
}
private void btnSave_Click(object sender, EventArgs e)
{
SaveFileDialog dlg = new SaveFileDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
bmp.Save(dlg.FileName);
}
}
private void btnAgain_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.ShowDialog();
}
List<Paint> paintList = new List<Paint>();
private void Form1_MouseDown_1(object sender, MouseEventArgs e)
{
start = new Point(e.X, e.Y);
}
Shape shape = Shape.铅笔;
Pen pen = new Pen(Color.Black, 2);
private void Form1_MouseUp_1(object sender, MouseEventArgs e)
{
end = new Point(e.X, e.Y);
points.Clear();
end = e.Location;
Graphics g = this.CreateGraphics();
switch (shape)
{
case Shape.线:
g.DrawLine(pen, start, end);
break;
case Shape.矩形:
g.DrawRectangle(pen, start.X, start.Y, end.X - start.X, end.Y - start.Y);
break;
case Shape.圆:
g.DrawEllipse(pen, start.X, start.Y, end.X - start.X, end.Y - start.Y);
break;
case Shape.三角形:
break;
case Shape.铅笔:
pen.Color = Color.Black;
pen.Width = 2;
break;
default:
break;
}
paintList.Add(new Paint(start, end, pen, shape));
}
private void btnPen_Click(object sender, EventArgs e)
{
shape = Shape.铅笔;
}
private void btnLine_Click(object sender, EventArgs e)
{
shape = Shape.线;
}
private void btnElli_Click(object sender, EventArgs e)
{
shape = Shape.圆;
}
private void btnRect_Click(object sender, EventArgs e)
{
shape = Shape.矩形;
}
private void btnRect_Click_1(object sender, EventArgs e)
{
shape = Shape.三角形;
}
List<Point> points=new List<Point>();
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (shape == Shape.铅笔)
{
if (e.Button == MouseButtons.Left)
{
if (points.Count == 0)
{
points.Add(start);
}
Graphics g = this.CreateGraphics();
g.DrawLine(pen, points[points.Count - 1], new Point(e.X, e.Y));
points.Add(new Point(e.X, e.Y));
}
}
}
private void btnMore_Click(object sender, EventArgs e)
{
ColorDialog MyDiolog = new ColorDialog();
MyDiolog.AllowFullOpen = false;
MyDiolog.ShowHelp = true;
MyDiolog.Color = btnMore.BackColor;
if (MyDiolog.ShowDialog() == DialogResult.OK)
{
btnMore.BackColor = MyDiolog.Color;
}
pen.Color = btnMore.BackColor;
}
List<Paint> paintlist=new List<Paint>();
private void Form1_Load(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
foreach (Paint p in paintList)
{
g.DrawLine(p.p, p.start, p.end);
}
}
private void btnBlack_Click(object sender, EventArgs e)
{
pen.Color = Color.Black;
}
private void btnYel_Click(object sender, EventArgs e)
{
pen.Color = Color.Yellow;
}
private void btnRed_Click(object sender, EventArgs e)
{
pen.Color = Color.Red;
}
private void btnBro_Click(object sender, EventArgs e)
{
pen.Color = Color.RosyBrown;
}
private void btnBlue_Click(object sender, EventArgs e)
{
pen.Color = Color.Blue;
}
private void btnFuch_Click(object sender, EventArgs e)
{
pen.Color = Color.Fuchsia;
}
private void btnGre_Click(object sender, EventArgs e)
{
pen.Color = Color.Green;
}
private void btnClear_Click(object sender, EventArgs e)
{
pen.Color = Color.White;
pen.Width = 15;
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
}
}