×

C#画橡皮条折线问题

Kalet Kalet 发表于2009-03-20 12:00:14 浏览206 评论0

抢沙发发表评论

近做毕业设计遇到一个问题,就是C#画橡皮条折线问题,在GDI+中做xor运算会发现达不到我们所需要的效果,也可以这么说GDI+不支持xor运算,我在网上也找了一些相关资料,可是发现和自个需要的效果差远了,最后想了几天发现双缓重画可以实现自个所需要的功能,通过mousedown,mousemove 二个事件就可以实现所需求的功能,通过在mousedown中做switch事件来做记录点的事件和画线事件。原代码如下:


C#画橡皮条折线问题
using System;
using System.Collections.Specialized ;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization;
using System.Collections;

namespace huaxian
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  #region 私有成员和控件
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  private Graphics _graphics;  //绘制空间
  private Bitmap _buffer;   //绘图缓冲区
  private Graphics _bggraphics; //缓冲空间
  
  private Bitmap _buffer_2 ;
        private Graphics _graphics_2 ;

  private Point startPoint = Point.Empty ;
  private System.Windows.Forms.StatusBar statusBar1;
  private System.Windows.Forms.StatusBarPanel statusBarPanel1;
  private Point secondPoint = Point.Empty ;
  private Point endPoint = Point.Empty ;

  private int sp = 1 ;
  private bool move = false ;
  private bool two_move = false ;

  //private ArrayList shapes=new ArrayList();
  #endregion

  #region 构造函数和设计器代码
  public Form1()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.statusBar1 = new System.Windows.Forms.StatusBar();
   this.statusBarPanel1 = new System.Windows.Forms.StatusBarPanel();
   ((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).BeginInit();
   this.SuspendLayout();
   //
   // statusBar1
   //
   this.statusBar1.Location = new System.Drawing.Point(0, 336);
   this.statusBar1.Name = "statusBar1";
   this.statusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
                        this.statusBarPanel1});
   this.statusBar1.Size = new System.Drawing.Size(480, 22);
   this.statusBar1.TabIndex = 0;
   this.statusBar1.Text = "statusBar1";
   //
   // statusBarPanel1
   //
   this.statusBarPanel1.Text = "statusBarPanel1";
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(480, 358);
   this.Controls.Add(this.statusBar1);
   this.Name = "Form1";
   this.Text = "Form1";
   this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
   this.Load += new System.EventHandler(this.Form1_Load);
   this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp);
   this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);
   ((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).EndInit();
   this.ResumeLayout(false);
C#画橡皮条折线问题
  }
  #endregion
  #endregion

  #region 应用程序入口
  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }
  #endregion

    #region 事件
  private void Form1_Load(object sender, System.EventArgs e)
  {
   //设置双缓冲

   this._graphics = this.CreateGraphics();
   
   this._buffer = new Bitmap("pic.jpg");   //Bitmap(this.Width,this.Height);
   this._buffer_2 = new Bitmap("pic.jpg");
 
  }

  private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
  {
   

   switch(sp)
   {
    case 1:  
     
     startPoint.X = e.X ;
     startPoint.Y = e.Y ;
     move = true ;
        two_move = false ;
     break ;

    case 2:
      
     this._graphics.DrawImageUnscaled(this._buffer,0,0);   //从缓冲区还原背景
        this._graphics.DrawLine(System.Drawing.Pens.Navy,startPoint,secondPoint);  //绘制橡皮线 
               
     move = false ;
        two_move = true ;
   
      break ;
    case 3:

     this._graphics.DrawImageUnscaled(this._buffer_2,0,0);   //从缓冲区还原背景
    
     this._graphics.DrawLine(System.Drawing.Pens.Navy,startPoint,secondPoint);  //绘制橡皮线 
      
     this._graphics.DrawLine(System.Drawing.Pens.Navy,secondPoint,endPoint);  //绘制橡皮线 
          
     two_move = false ;
                  
     sp = 1 ;
     
     break ;
   }
   
  }

  private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
  {
   
   if(move == true)
   {
     sp=2 ;
 
    if (e.Button ==System.Windows.Forms.MouseButtons.None)
    {
     secondPoint.X = e.X ;
     secondPoint.Y = e.Y ;

     this._graphics.DrawImageUnscaled(this._buffer,0,0);   //从缓冲区还原背景
    
     this._graphics.DrawLine(System.Drawing.Pens.Navy,startPoint,secondPoint);  //绘制橡皮线   
   
    }
   }
   if(two_move == true)
   {
    sp = 3 ;

    if (e.Button ==System.Windows.Forms.MouseButtons.None)
    {
     endPoint.X = e.X ;
     endPoint.Y = e.Y ;

     this._graphics.DrawImageUnscaled(this._buffer_2,0,0);   //从缓冲区还原背景
    
     this._graphics.DrawLine(System.Drawing.Pens.Navy,startPoint,secondPoint);  //绘制橡皮线 
    
     this._graphics.DrawLine(System.Drawing.Pens.Navy,secondPoint,endPoint);  //绘制橡皮线 
   
    }
   
   }
 

  }

   
  #endregion

  
 }
}

C#画橡皮条折线问题
代码是经过多次试验,有些代码是多余的,呵呵,请见谅:)


群贤毕至

访客