欢迎光临
我们一直在努力

C#中用“橡皮条”法绘图和重绘-.NET教程,数据库应用

建站超值云服务器,限时71元/月

前些日子在论坛上发了个帖子,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 ) );

}

}

}

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » C#中用“橡皮条”法绘图和重绘-.NET教程,数据库应用
分享到: 更多 (0)

相关推荐

  • 暂无文章