日期:2008-11-04  浏览次数:20516 次

前些日子在论坛上发了个帖子,100分寻求“橡皮条”法绘图的代码。效果不是很好,于是自己参照网友给的代码重新写了一个,解决了绘图与重绘的问题。由于只写了部分,所以功能有限,同时可能算法不是很好,希望大家指点!!窗体中仅包含一个pictrueBox1,先将代码付诸于下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;

namespace GDI_练习
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
public System.Windows.Forms.PictureBox pictureBox1;
private Point p1 = Point.Empty, p2 = Point.Empty;
private bool isMouseDown = false, isMouseUp = false;

ArrayList addArray = new ArrayList();
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;
}

}

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

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.pictureBox1 = new System.Windows.Forms.PictureBox();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.BackColor = System.Drawing.Color.White;
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(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);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(432, 397);
this.Controls.Add(this.pictureBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

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

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 )
g.DrawEl