在C#用鼠标绘图笔记(一)我写了关于画线的代码实现,下面发一下我基于那个代码实现的用鼠标绘制多段线的方法,因为目前网上关于这种绘制多段线的方法的代码很少,所以,自己发一下,时间仓促,因此,可能有些bug,希望大家和我一起探讨
using
System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace Test
{
public class Form4 : System.Windows.Forms.Form
{
private System.Windows.Forms.PictureBox pictureBox1;
private System.Drawing.Point start ;
private bool CanMove = false ;
private Bitmap bitmap;
private System.ComponentModel.Container components = null ;
public Form4()
{
InitializeComponent();
this .SetStyle(ControlStyles.UserPaint, true );
this .SetStyle(ControlStyles.AllPaintingInWmPaint, true );
this .SetStyle(ControlStyles.DoubleBuffer, true );
bitmap = new Bitmap( this .pictureBox1.Width, this .pictureBox1.Height);
this .pictureBox1.Image = bitmap;
}
[STAThread]
static void Main()
{
Application.Run( new Form4());
}
protected override void Dispose( bool disposing )
{
if ( disposing )
{
if (components != null )
{
components.Dispose();
}
}
base .Dispose( disposing );
}
private void pictureBox1_MouseDown( object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if ( this .start.IsEmpty)
{
this .start = new Point(e.X,e.Y);
this .CanMove = true ;
}
else
{
this .pictureBox1.Image = this .bitmap;
Graphics g = Graphics.FromImage( this .pictureBox1.Image);
g.DrawLine( new Pen(Color.Red, 3 ), this .start, new Point(e.X,e.Y));
g.Dispose();
this .start = new Point(e.X,e.Y);
this .pictureBox1.Invalidate();
}
}
}
private void pictureBox1_MouseMove( object sender, System.Windows.Forms.MouseEventArgs e)
{
if ( this .CanMove == true )
{
this .pictureBox1.Image = (Bitmap) this .bitmap.Clone();
Graphics g = Graphics.FromImage( this .pictureBox1.Image);
Graphics.FromImage( this .pictureBox1.Image).Clear(Color.Transparent); // 清除
g.DrawLine( new Pen(Color.Red, 1 ), this .start, new Point(e.X,e.Y)); // 重绘
g.DrawImage( this .bitmap, this .pictureBox1.Location.X, this .pictureBox1.Location.Y, this .pictureBox1.Width, this .pictureBox1.Height);
g.Dispose();
}
}
private void pictureBox1_MouseUp( object sender, System.Windows.Forms.MouseEventArgs e)
{
this .pictureBox1.Image = this .bitmap;
if (e.Button == MouseButtons.Left)
{
Graphics g = Graphics.FromImage( this .pictureBox1.Image);
g.DrawLine( new Pen(Color.Red, 3 ), this .start, new Point(e.X,e.Y));
g.Dispose();
this .start = new Point(e.X,e.Y);
}
else
{
this .start = new Point();
this .CanMove = false ;
}
this .pictureBox1.Invalidate();
}
private void InitializeComponent()
{
this .pictureBox1 = new System.Windows.Forms.PictureBox();
this .SuspendLayout();
this .pictureBox1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this .pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this .pictureBox1.Location = new System.Drawing.Point( 0 , 0 );
this .pictureBox1.Name = " pictureBox1 " ;
this .pictureBox1.Size = new System.Drawing.Size( 464 , 413 );
this .pictureBox1.TabIndex = 0 ;
this .pictureBox1.TabStop = false ;
this .pictureBox1.MouseUp += new System.Windows.Forms.MouseEventHandler( this .pictureBox1_MouseUp);
this .pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler( this .pictureBox1_MouseMove);
this .pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler( this .pictureBox1_MouseDown);
this .AutoScaleBaseSize = new System.Drawing.Size( 6 , 14 );
this .ClientSize = new System.Drawing.Size( 464 , 413 );
this .Controls.Add( this .pictureBox1);
this .Name = " Form4 " ;
this .Text = " Form4 " ;
this .ResumeLayout( false );
}
}
}
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace Test
{
public class Form4 : System.Windows.Forms.Form
{
private System.Windows.Forms.PictureBox pictureBox1;
private System.Drawing.Point start ;
private bool CanMove = false ;
private Bitmap bitmap;
private System.ComponentModel.Container components = null ;
public Form4()
{
InitializeComponent();
this .SetStyle(ControlStyles.UserPaint, true );
this .SetStyle(ControlStyles.AllPaintingInWmPaint, true );
this .SetStyle(ControlStyles.DoubleBuffer, true );
bitmap = new Bitmap( this .pictureBox1.Width, this .pictureBox1.Height);
this .pictureBox1.Image = bitmap;
}
[STAThread]
static void Main()
{
Application.Run( new Form4());
}
protected override void Dispose( bool disposing )
{
if ( disposing )
{
if (components != null )
{
components.Dispose();
}
}
base .Dispose( disposing );
}
private void pictureBox1_MouseDown( object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if ( this .start.IsEmpty)
{
this .start = new Point(e.X,e.Y);
this .CanMove = true ;
}
else
{
this .pictureBox1.Image = this .bitmap;
Graphics g = Graphics.FromImage( this .pictureBox1.Image);
g.DrawLine( new Pen(Color.Red, 3 ), this .start, new Point(e.X,e.Y));
g.Dispose();
this .start = new Point(e.X,e.Y);
this .pictureBox1.Invalidate();
}
}
}
private void pictureBox1_MouseMove( object sender, System.Windows.Forms.MouseEventArgs e)
{
if ( this .CanMove == true )
{
this .pictureBox1.Image = (Bitmap) this .bitmap.Clone();
Graphics g = Graphics.FromImage( this .pictureBox1.Image);
Graphics.FromImage( this .pictureBox1.Image).Clear(Color.Transparent); // 清除
g.DrawLine( new Pen(Color.Red, 1 ), this .start, new Point(e.X,e.Y)); // 重绘
g.DrawImage( this .bitmap, this .pictureBox1.Location.X, this .pictureBox1.Location.Y, this .pictureBox1.Width, this .pictureBox1.Height);
g.Dispose();
}
}
private void pictureBox1_MouseUp( object sender, System.Windows.Forms.MouseEventArgs e)
{
this .pictureBox1.Image = this .bitmap;
if (e.Button == MouseButtons.Left)
{
Graphics g = Graphics.FromImage( this .pictureBox1.Image);
g.DrawLine( new Pen(Color.Red, 3 ), this .start, new Point(e.X,e.Y));
g.Dispose();
this .start = new Point(e.X,e.Y);
}
else
{
this .start = new Point();
this .CanMove = false ;
}
this .pictureBox1.Invalidate();
}
private void InitializeComponent()
{
this .pictureBox1 = new System.Windows.Forms.PictureBox();
this .SuspendLayout();
this .pictureBox1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this .pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this .pictureBox1.Location = new System.Drawing.Point( 0 , 0 );
this .pictureBox1.Name = " pictureBox1 " ;
this .pictureBox1.Size = new System.Drawing.Size( 464 , 413 );
this .pictureBox1.TabIndex = 0 ;
this .pictureBox1.TabStop = false ;
this .pictureBox1.MouseUp += new System.Windows.Forms.MouseEventHandler( this .pictureBox1_MouseUp);
this .pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler( this .pictureBox1_MouseMove);
this .pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler( this .pictureBox1_MouseDown);
this .AutoScaleBaseSize = new System.Drawing.Size( 6 , 14 );
this .ClientSize = new System.Drawing.Size( 464 , 413 );
this .Controls.Add( this .pictureBox1);
this .Name = " Form4 " ;
this .Text = " Form4 " ;
this .ResumeLayout( false );
}
}
}