首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C#做的画板 有关问题请问!

2012-05-28 
C#做的画板 问题请教!!想用C#做个画板的小程序 不知道为什么不能画出画来 是我的鼠标事件没写对么 没有报

C#做的画板 问题请教!!
想用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);
  }
  }
  }





  }
}



[解决办法]
http://www.cnblogs.com/zhjzwl/archive/2009/02/27/1399505.html

to:代码够长的
[解决办法]
,,,,代码看着好乱,空件怎么不改名,我刚学C#,做过一个画板,你的代码,可以优化一下的,同类的按钮调用一个单击事件,在根据同类不同的按钮有不同的实现,不要每个按钮单击都对应一个事件
[解决办法]
你不应该用CreateGraphics,而应该在Paint事件中绘图
[解决办法]
直接使用OnPaint中的e.Graphics,其他绘图函数传入这个参数,鼠标动作中要调用Invaliedate,去进行重绘

热点排行