e

本文介绍了一个简单的绘图应用程序实现,用户可以通过点击按钮选择绘制矩形、椭圆、直线或曲线,并支持颜色选择和画布清除功能。文章通过C#代码展示了如何响应鼠标事件来绘制不同的图形。

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

using System.Data;

namespace WindowsApptest9_21
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form3 : System.Windows.Forms.Form
 {
  public System.Windows.Forms.PictureBox pictureBox1;
  private Point p1 = Point.Empty, p2 = Point.Empty;
  private Point p3=Point.Empty;
  private bool isMouseDown = false, isMouseUp = false;
 
  ArrayList addArray = new ArrayList();
  ArrayList aaaArray = new ArrayList();
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.Button button2;
  public string shape;
  private System.Windows.Forms.Button button3;
  private System.Windows.Forms.Button button4;
  private Color color=Color.Black;
  private System.Windows.Forms.Button button5;
  private System.Windows.Forms.Button button6;
  //  private Pen pen;
 
  //      Pen pen=new Pen(col,2);
 
  public struct SharpType
  {
   public string type;
   public Point p1, p2;
   public Color foreColor, backColor;
   public Brush brush;
   public SharpType(string type, Point p1, Point p2, Color foreColor, Color backColor, Brush brush )
   {
    this.type = type;
    this.p1 = p1;
    this.p2 = p2;
    this.foreColor = foreColor;
    this.backColor = backColor;
    this.brush = brush;
   }
   //   public Point[]points=new Point[];

  }

  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form3()
  {
   //
   // 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.pictureBox1 = new System.Windows.Forms.PictureBox();
   this.button1 = new System.Windows.Forms.Button();
   this.button2 = new System.Windows.Forms.Button();
   this.button3 = new System.Windows.Forms.Button();
   this.button4 = new System.Windows.Forms.Button();
   this.button5 = new System.Windows.Forms.Button();
   this.button6 = new System.Windows.Forms.Button();
   this.SuspendLayout();
   //
   // pictureBox1
   //
   this.pictureBox1.BackColor = System.Drawing.Color.White;
   this.pictureBox1.Cursor = System.Windows.Forms.Cursors.Cross;
   this.pictureBox1.Location = new System.Drawing.Point(0, 0);
   this.pictureBox1.Name = "pictureBox1";
   this.pictureBox1.Size = new System.Drawing.Size(432, 397);
   this.pictureBox1.TabIndex = 0;
   this.pictureBox1.TabStop = false;
   this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
   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);
   //
   // button1
   //
   this.button1.Location = new System.Drawing.Point(448, 72);
   this.button1.Name = "button1";
   this.button1.TabIndex = 1;
   this.button1.Text = "矩形";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // button2
   //
   this.button2.Location = new System.Drawing.Point(448, 112);
   this.button2.Name = "button2";
   this.button2.TabIndex = 2;
   this.button2.Text = "椭圆";
   this.button2.Click += new System.EventHandler(this.button2_Click);
   //
   // button3
   //
   this.button3.Location = new System.Drawing.Point(448, 144);
   this.button3.Name = "button3";
   this.button3.TabIndex = 3;
   this.button3.Text = "直线";
   this.button3.Click += new System.EventHandler(this.button3_Click);
   //
   // button4
   //
   this.button4.Location = new System.Drawing.Point(448, 224);
   this.button4.Name = "button4";
   this.button4.TabIndex = 4;
   this.button4.Text = "选择颜色";
   this.button4.Click += new System.EventHandler(this.button4_Click);
   //
   // button5
   //
   this.button5.Location = new System.Drawing.Point(448, 184);
   this.button5.Name = "button5";
   this.button5.TabIndex = 5;
   this.button5.Text = "清除";
   this.button5.Click += new System.EventHandler(this.button5_Click);
   //
   // button6
   //
   this.button6.Location = new System.Drawing.Point(448, 32);
   this.button6.Name = "button6";
   this.button6.TabIndex = 6;
   this.button6.Text = "曲线";
   this.button6.Click += new System.EventHandler(this.button6_Click);
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(552, 397);
   this.Controls.Add(this.button6);
   this.Controls.Add(this.button5);
   this.Controls.Add(this.button4);
   this.Controls.Add(this.button3);
   this.Controls.Add(this.button2);
   this.Controls.Add(this.button1);
   this.Controls.Add(this.pictureBox1);
   this.Name = "Form1";
   this.Text = "Form1";
   this.ResumeLayout(false);

  }
  #endregion

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

  private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
  {
   if( ! isMouseUp )
   {
    this.isMouseDown = true;
    this.p1 = new Point( e.X, e.Y );
   }
 
  }

  private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
  {
   Graphics g = this.pictureBox1.CreateGraphics();
   if( isMouseDown && p2 != Point.Empty )
   {
    if(this.shape=="DrawEllipse")
    {
     g.DrawEllipse( Pens.White, p1.X, p1.Y, Math.Abs( p1.X - p2.X ), Math.Abs( p1.Y - p2.Y ) );
    }
    if(this.shape=="DrawRectangle")
    {
     g.DrawRectangle( Pens.White, p1.X, p1.Y, Math.Abs( p1.X - p2.X ), Math.Abs( p1.Y - p2.Y ) );
    }
    if(this.shape=="DrawLine")
    {
     g.DrawLine(Pens.White,this.p1,this.p2);
    }
   
   }

   if( isMouseDown && ! isMouseUp )
   {
    p2 = new Point( e.X, e.Y );
    if(this.shape=="DrawEllipse")
    {
    
     g.DrawEllipse(new Pen(color,1), p1.X, p1.Y, Math.Abs( p1.X - p2.X ), Math.Abs( p1.Y - p2.Y ) );
    }
    if(this.shape=="DrawRectangle")
    {
     //     g.DrawRectangle( Pens.Black, p1.X, p1.Y, Math.Abs( p1.X - p2.X ), Math.Abs( p1.Y - p2.Y ) );
     g.DrawRectangle( new Pen(color,1), p1.X, p1.Y, Math.Abs( p1.X - p2.X ), Math.Abs( p1.Y - p2.Y ) );
    }
    if(this.shape=="DrawLine")
    {
     g.DrawLine(new Pen(color,1),this.p1,this.p2);
    }
    if(this.shape=="DrawCurve")
    {

     g.DrawLine(new Pen(color,1),this.p1,this.p2);
     addArray.Add( new SharpType("DrawCurve", p1, p2, color, Color.Empty, Brushes.Black ) );
     this.p1=this.p2;
     this.p2=this.p3;

    
    }
  

   }
   foreach( SharpType type in addArray )
   {
    if(type.type=="DrawEllipse")
    {
     g.DrawEllipse( new Pen(type.foreColor,1), type.p1.X, type.p1.Y, Math.Abs( type.p1.X -  type.p2.X ), Math.Abs( type.p1.Y - type.p2.Y ) );

    }
    if(type.type=="DrawRectangle")
    {
     g.DrawRectangle(new Pen(type.foreColor,1), type.p1.X, type.p1.Y, Math.Abs( type.p1.X -  type.p2.X ), Math.Abs( type.p1.Y - type.p2.Y ) );
    }
    if(type.type=="DrawLine")
    {
     g.DrawLine(new Pen(type.foreColor,1),type.p1,type.p2);
    }
  
    if(type.type=="DrawCurve")
    {
    
     g.DrawLine(new Pen(type.foreColor,1),type.p1,type.p2);

    }
   }
   g.Dispose();
  
  }

  private void pictureBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
  {
   this.isMouseDown = false;
   p2 = new Point( e.X, e.Y );
   Graphics g = this.pictureBox1.CreateGraphics();
   if(this.shape=="DrawEllipse")
   {
    g.DrawEllipse( new Pen(color,1), p1.X, p1.Y, Math.Abs( p1.X - p2.X ), Math.Abs( p1.Y - p2.Y ) );
    addArray.Add( new SharpType( "DrawEllipse", p1, p2, color, Color.Empty, Brushes.Black ) );
   }
   if(this.shape=="DrawRectangle")
   {

    g.DrawRectangle(new Pen(color,1), p1.X, p1.Y, Math.Abs( p1.X - p2.X ), Math.Abs( p1.Y - p2.Y ) );
    //    addArray.Add( new SharpType("DrawRectangle", p1, p2, Color.Black, Color.Empty, Brushes.Black ) );
    addArray.Add( new SharpType("DrawRectangle", p1, p2, color, Color.Empty, Brushes.Black ) );
   }
   if(this.shape=="DrawLine")
   {
    g.DrawLine(new Pen(color,1),this.p1,this.p2);
    addArray.Add( new SharpType("DrawLine", p1, p2, color, Color.Empty, Brushes.Black ) );
   }
   if(this.shape=="DrawCurve")
   {
    g.DrawLine(new Pen(color,1),this.p1,this.p2);
    addArray.Add( new SharpType("DrawCurve", p1, p2, color, Color.Empty, Brushes.Black ) );
    //    this.p1=this.p2;
    //    this.p2=this.p3;
   
   }

   p1 = Point.Empty;
   p2 = Point.Empty;
   g.Dispose();
 
  }

  private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
  {
   foreach( SharpType type in addArray )
   {
    if(type.type=="DrawEllipse")
    {
     e.Graphics.DrawEllipse(new Pen(type.foreColor,1), type.p1.X, type.p1.Y, Math.Abs( type.p1.X -  type.p2.X ), Math.Abs( type.p1.Y - type.p2.Y ) );

    }
    if(type.type=="DrawRectangle")
    {
     e.Graphics.DrawRectangle( new Pen(type.foreColor,1), type.p1.X, type.p1.Y, Math.Abs( type.p1.X -  type.p2.X ), Math.Abs( type.p1.Y - type.p2.Y ) );
    }
    if(type.type=="DrawLine")
    {
     e.Graphics.DrawLine(new Pen(type.foreColor,1),type.p1,type.p2);
    }

 
    if(type.type=="DrawCurve")
    {
    
     e.Graphics.DrawLine(new Pen(type.foreColor,1),type.p1,type.p2);
   
    }
  
   }
  }

  private void button1_Click(object sender, System.EventArgs e)
  {
   shape="DrawRectangle";
  }

  private void button2_Click(object sender, System.EventArgs e)
  {
   shape="DrawEllipse";
   //   this.color=Color.Black;
  }

  private void button3_Click(object sender, System.EventArgs e)
  {
   shape="DrawLine";
  }

  private void button4_Click(object sender, System.EventArgs e)
  {
   ColorDialog ColorDialog1 = new ColorDialog ( ) ;
   ColorDialog1.AllowFullOpen = true  ;
   ColorDialog1.FullOpen = true ;
   //设定此颜色对话框存在"帮助"按钮,缺省是没有的
   ColorDialog1.ShowHelp = true  ;
   // 设定此颜色对话框的初始颜色,所以如果在对话框中选择"取消",则此对话框会重新此颜色
   ColorDialog1.Color = Color.Black ;
   if ( ColorDialog1.ShowDialog ( ) != DialogResult.Cancel )
   {
    this.color = ColorDialog1.Color ;
    //    showInfo ( ) ;
   }
   else
   {
    this.color=Color.Black;
   }
  }

  private void button5_Click(object sender, System.EventArgs e)
  {
   this.addArray.Clear();
   this.pictureBox1.Refresh();
  }

  private void button6_Click(object sender, System.EventArgs e)
  {
   shape="DrawCurve";
  }


 
 }
}


zhuan http://xiaoliang1982.blog.hexun.com/5723850_d.html

转载于:https://www.cnblogs.com/wull66/archive/2007/02/09/646518.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值