想用C#做个画板的小程序 不知道为什么不能画出画来 是我的鼠标事件没写对么 没有报错 但是鼠标不能画画
下面是form.cs 请各位帮忙看看 感激不尽
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
private Shape current;
private bool isDown;
private Bitmap bmp, bmp1;
private List <Shape >shapes=new List<Shape> ();
private ShapeType type = ShapeType.Line;
private bool isTrail;
private FileInfo currentFile;
private static string openFileName = "";
private static int lineWidth = 1; public Form1()
{
InitializeComponent();
bmp = new Bitmap(pictureBox1 .Width ,pictureBox1 .Height );
bmp1 = new Bitmap(pictureBox1 .Width ,pictureBox1 .Height );
current = new MyLine();
this.button12.BackColor = Color.Black;
} private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics grfx = e.Graphics;
grfx.DrawImage(bmp ,0,0);
}
private void button1_Click(object sender, EventArgs e)
{
isTrail = true;
type = ShapeType.Line;
} private void button2_Click(object sender, EventArgs e)
{
isTrail = false ;
type = ShapeType.Line; } private void button3_Click(object sender, EventArgs e)
{
isTrail = false;
type = ShapeType.Rect ;
} private void button5_Click(object sender, EventArgs e)
{
isTrail = false;
type = ShapeType.Ellipse ;
} private void button4_Click(object sender, EventArgs e)
{
isTrail = false;
type = ShapeType.FilledRect ;
} private void button6_Click(object sender, EventArgs e)
{
isTrail = false;
type = ShapeType.FilledEllipse ;
} private void button7_Click(object sender, EventArgs e)
{
isTrail = false;
type = ShapeType.CircleRect;
} private void button13_Click(object sender, EventArgs e)
{
this.button12.BackColor = this.button13.BackColor;
} private void button14_Click(object sender, EventArgs e)
{
this.button12.BackColor = this.button14.BackColor;
} private void button15_Click(object sender, EventArgs e)
{
this.button12.BackColor = this.button15.BackColor;
} private void button16_Click(object sender, EventArgs e)
{
this.button12.BackColor = this.button16.BackColor;
} private void button17_Click(object sender, EventArgs e)
{
this.button12.BackColor = this.button17.BackColor;
} private void button18_Click(object sender, EventArgs e)
{
this.button12.BackColor = this.button18.BackColor;
} private void button19_Click(object sender, EventArgs e)
{
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
this.button12.BackColor = colorDialog1.Color;
}
} private void button9_Click(object sender, EventArgs e)
{
lineWidth = 1;
} private void button10_Click(object sender, EventArgs e)
{
lineWidth = 2;
} private void button11_Click(object sender, EventArgs e)
{
lineWidth = 3;
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isDown = true;
current = ShapeFactory.CreateShape(type);
current.First = e.Location;
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (isDown )
{
if (!isTrail)
{
DrawShape(e.Location);
}
else
{
DrawTrail(e.Location);
}
Graphics grfx = pictureBox1.CreateGraphics();
if (shapes.Count != 0)
{
grfx.DrawImage(bmp, 0, 0);
}
grfx.Dispose();
}
}
private void DrawTrail(Point point)
{
Graphics grfx = pictureBox1.CreateGraphics();
current.Last = point;
current.ForeColor = this.button12.BackColor;
current.PenWidth = lineWidth;
current.Draw(grfx );
Graphics img = Graphics.FromImage(bmp);
current.Draw(img) ; shapes.Add(current);
current = ShapeFactory.CreateShape(type );
current.First = point; grfx.Dispose();
img.Dispose();
}
private void DrawShape(Point point)
{
Graphics grfx = pictureBox1.CreateGraphics();
if (!current.Last.Equals(Point.Empty))
{
current.ForeColor = pictureBox1.BackColor;
current.PenWidth = lineWidth;
current.Draw(grfx);
} current.Last = point;
current.ForeColor = this.button12.BackColor;
current.PenWidth = lineWidth;
current.Draw(grfx ); grfx.Dispose(); }
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
isDown = false;
if (!current.Last.Equals(Point.Empty))
{
Graphics img = Graphics.FromImage(bmp);
current.Draw(img);
shapes.Add(current);
}
current = null;
} private void button8_Click(object sender, EventArgs e)
{
Graphics g = pictureBox1.CreateGraphics();
g.Clear(pictureBox1 .BackColor );
g.Dispose(); bmp = new Bitmap(pictureBox1 .Width ,pictureBox1 .Height );
} private void Form1_Load(object sender, EventArgs e)
{ } private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{ } private void pictureBox1_Click(object sender, EventArgs e)
{ } private void 文件保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (File.Exists(openFileName))
{
currentFile = new FileInfo(openFileName);
bmp.Save(currentFile.FullName);
}
else
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
currentFile = new FileInfo(saveFileDialog1 .FileName );
bmp.Save(currentFile.FullName);
}
}
} }
}
下面是form.cs 请各位帮忙看看 感激不尽
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
private Shape current;
private bool isDown;
private Bitmap bmp, bmp1;
private List <Shape >shapes=new List<Shape> ();
private ShapeType type = ShapeType.Line;
private bool isTrail;
private FileInfo currentFile;
private static string openFileName = "";
private static int lineWidth = 1; public Form1()
{
InitializeComponent();
bmp = new Bitmap(pictureBox1 .Width ,pictureBox1 .Height );
bmp1 = new Bitmap(pictureBox1 .Width ,pictureBox1 .Height );
current = new MyLine();
this.button12.BackColor = Color.Black;
} private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics grfx = e.Graphics;
grfx.DrawImage(bmp ,0,0);
}
private void button1_Click(object sender, EventArgs e)
{
isTrail = true;
type = ShapeType.Line;
} private void button2_Click(object sender, EventArgs e)
{
isTrail = false ;
type = ShapeType.Line; } private void button3_Click(object sender, EventArgs e)
{
isTrail = false;
type = ShapeType.Rect ;
} private void button5_Click(object sender, EventArgs e)
{
isTrail = false;
type = ShapeType.Ellipse ;
} private void button4_Click(object sender, EventArgs e)
{
isTrail = false;
type = ShapeType.FilledRect ;
} private void button6_Click(object sender, EventArgs e)
{
isTrail = false;
type = ShapeType.FilledEllipse ;
} private void button7_Click(object sender, EventArgs e)
{
isTrail = false;
type = ShapeType.CircleRect;
} private void button13_Click(object sender, EventArgs e)
{
this.button12.BackColor = this.button13.BackColor;
} private void button14_Click(object sender, EventArgs e)
{
this.button12.BackColor = this.button14.BackColor;
} private void button15_Click(object sender, EventArgs e)
{
this.button12.BackColor = this.button15.BackColor;
} private void button16_Click(object sender, EventArgs e)
{
this.button12.BackColor = this.button16.BackColor;
} private void button17_Click(object sender, EventArgs e)
{
this.button12.BackColor = this.button17.BackColor;
} private void button18_Click(object sender, EventArgs e)
{
this.button12.BackColor = this.button18.BackColor;
} private void button19_Click(object sender, EventArgs e)
{
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
this.button12.BackColor = colorDialog1.Color;
}
} private void button9_Click(object sender, EventArgs e)
{
lineWidth = 1;
} private void button10_Click(object sender, EventArgs e)
{
lineWidth = 2;
} private void button11_Click(object sender, EventArgs e)
{
lineWidth = 3;
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isDown = true;
current = ShapeFactory.CreateShape(type);
current.First = e.Location;
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (isDown )
{
if (!isTrail)
{
DrawShape(e.Location);
}
else
{
DrawTrail(e.Location);
}
Graphics grfx = pictureBox1.CreateGraphics();
if (shapes.Count != 0)
{
grfx.DrawImage(bmp, 0, 0);
}
grfx.Dispose();
}
}
private void DrawTrail(Point point)
{
Graphics grfx = pictureBox1.CreateGraphics();
current.Last = point;
current.ForeColor = this.button12.BackColor;
current.PenWidth = lineWidth;
current.Draw(grfx );
Graphics img = Graphics.FromImage(bmp);
current.Draw(img) ; shapes.Add(current);
current = ShapeFactory.CreateShape(type );
current.First = point; grfx.Dispose();
img.Dispose();
}
private void DrawShape(Point point)
{
Graphics grfx = pictureBox1.CreateGraphics();
if (!current.Last.Equals(Point.Empty))
{
current.ForeColor = pictureBox1.BackColor;
current.PenWidth = lineWidth;
current.Draw(grfx);
} current.Last = point;
current.ForeColor = this.button12.BackColor;
current.PenWidth = lineWidth;
current.Draw(grfx ); grfx.Dispose(); }
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
isDown = false;
if (!current.Last.Equals(Point.Empty))
{
Graphics img = Graphics.FromImage(bmp);
current.Draw(img);
shapes.Add(current);
}
current = null;
} private void button8_Click(object sender, EventArgs e)
{
Graphics g = pictureBox1.CreateGraphics();
g.Clear(pictureBox1 .BackColor );
g.Dispose(); bmp = new Bitmap(pictureBox1 .Width ,pictureBox1 .Height );
} private void Form1_Load(object sender, EventArgs e)
{ } private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{ } private void pictureBox1_Click(object sender, EventArgs e)
{ } private void 文件保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (File.Exists(openFileName))
{
currentFile = new FileInfo(openFileName);
bmp.Save(currentFile.FullName);
}
else
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
currentFile = new FileInfo(saveFileDialog1 .FileName );
bmp.Save(currentFile.FullName);
}
}
} }
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货