使用C#鼠标绘图的方法
这是本人第一次在blog上发一个小代码,水平有限,慢慢努力
这是用C#编写的关于用鼠标绘图代码,代码及讲解如下
usingSystem;
usingSystem.Drawing;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Windows.Forms;
namespaceTest
{
publicclassForm3:System.Windows.Forms.Form
{
privateSystem.ComponentModel.Containercomponents=null;
privateSystem.Windows.Forms.PictureBoxpictureBox1;
privateSystem.Drawing.PointstartPoint;
privateSystem.Drawing.Bitmapbitmap;
privateboolCanMove=false;
publicForm3()
{
InitializeComponent();
//创建缓冲
this.SetStyle(ControlStyles.UserPaint,true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint,true);
this.SetStyle(ControlStyles.DoubleBuffer,true);
bitmap=newBitmap(this.pictureBox1.Width,this.pictureBox1.Height);
this.pictureBox1.Image=bitmap;
}
protectedoverridevoidDispose(booldisposing)
{
if(disposing)
{
if(components!=null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
Windows窗体设计器生成的代码
privatevoidpictureBox1_MouseDown(objectsender,System.Windows.Forms.MouseEventArgse)
{
this.CanMove=true;
this.startPoint=newPoint(e.X,e.Y);
}
privatevoidpictureBox1_MouseMove(objectsender,System.Windows.Forms.MouseEventArgse)
{
if(this.CanMove==true)
{
this.pictureBox1.Image=(Bitmap)this.bitmap.Clone();
using(Graphicsgraphics=Graphics.FromImage(this.pictureBox1.Image))
{
graphics.Clear(Color.Transparent);//清除
graphics.DrawLine(newPen(Color.Red,1),this.startPoint,newPoint(e.X,e.Y));//重绘
graphics.DrawImage(this.bitmap,this.pictureBox1.Location.X,this.pictureBox1.Location.Y,this.pictureBox1.Width,this.pictureBox1.Height);
}
}
}
privatevoidpictureBox1_MouseUp(objectsender,System.Windows.Forms.MouseEventArgse)
{
this.pictureBox1.Image=this.bitmap;
using(Graphicsgraphics=Graphics.FromImage(this.pictureBox1.Image))
{
graphics.DrawLine(newPen(Color.Red,1),this.startPoint,newPoint(e.X,e.Y));
}
this.CanMove=false;
}
}
}
this.SetStyle(ControlStyles.UserPaint,true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint,true);
this.SetStyle(ControlStyles.DoubleBuffer,true);
bitmap=newBitmap(this.pictureBox1.Width,this.pictureBox1.Height);
this.pictureBox1.Image=bitmap;
}
protectedoverridevoidDispose(booldisposing)
{
if(disposing)
{
if(components!=null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
Windows窗体设计器生成的代码
privatevoidpictureBox1_MouseDown(objectsender,System.Windows.Forms.MouseEventArgse)
{
this.CanMove=true;
this.startPoint=newPoint(e.X,e.Y);
}
privatevoidpictureBox1_MouseMove(objectsender,System.Windows.Forms.MouseEventArgse)
{
if(this.CanMove==true)
{
this.pictureBox1.Image=(Bitmap)this.bitmap.Clone();
using(Graphicsgraphics=Graphics.FromImage(this.pictureBox1.Image))
{
graphics.Clear(Color.Transparent);//清除
graphics.DrawLine(newPen(Color.Red,1),this.startPoint,newPoint(e.X,e.Y));//重绘
graphics.DrawImage(this.bitmap,this.pictureBox1.Location.X,this.pictureBox1.Location.Y,this.pictureBox1.Width,this.pictureBox1.Height);
}
}
}
privatevoidpictureBox1_MouseUp(objectsender,System.Windows.Forms.MouseEventArgse)
{
this.pictureBox1.Image=this.bitmap;
using(Graphicsgraphics=Graphics.FromImage(this.pictureBox1.Image))
{
graphics.DrawLine(newPen(Color.Red,1),this.startPoint,newPoint(e.X,e.Y));
}
this.CanMove=false;
}
}
}
参考:
http://www.codeproject.com/dotnet/rubberbandline.asp
本文地址:http://www.45fan.com/dnjc/70292.html