C#用鼠标绘图笔记(二)

        在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 );
        }
    }
}

 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值