程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C#中用“橡皮條”法繪圖和重繪

C#中用“橡皮條”法繪圖和重繪

編輯:關於C語言
前些日子在論壇上發了個帖子,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.DrawEllipse( Pens.White, p1.X, p1.Y, Math.Abs( p1.X - p2.X ), Math.Abs( p1.Y - p2.Y ) );


if( isMouseDown && ! isMouseUp )
{
p2 = new Point( e.X, e.Y );
g.DrawEllipse( Pens.Black, p1.X, p1.Y, Math.Abs( p1.X - p2.X ), Math.Abs( p1.Y - p2.Y ) );

}
foreach( SharpType type in addArray )
{
g.DrawEllipse( Pens.Black, type.p1.X, type.p1.Y, Math.Abs( type.p1.X - type.p2.X ), Math.Abs( type.p1.Y - type.p2.Y ) );

}
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();
g.DrawEllipse( Pens.Black, p1.X, p1.Y, Math.Abs( p1.X - p2.X ), Math.Abs( p1.Y - p2.Y ) );
addArray.Add( new SharpType( "a", p1, p2, Color.Black, Color.Empty, Brushes.Black ) );
p1 = Point.Empty;
p2 = Point.Empty;
g.Dispose();

}

private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
foreach( SharpType type in addArray )
{
e.Graphics.DrawEllipse( Pens.Black, type.p1.X, type.p1.Y, Math.Abs( type.p1.X - type.p2.X ), Math.Abs( type.p1.Y - type.p2.Y ) );

}
}



}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved