日期:2014-05-19  浏览次数:21037 次

C#中怎么样实现橡皮筋式画线的功能?
C#中怎么样实现橡皮筋式画线的功能?有没有这方面的资料或网页.

------解决方案--------------------
给你个画图的例子参考下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DoubleBufferDraw
{
public partial class DrawLine : Form
{
class LineObj
{
private Point m_start;
private Point m_end;
public LineObj(Point start, Point end)
{
this.m_start = start;
this.m_end = end;
}
public void Draw(Graphics g, Pen pen)
{
g.DrawLine(pen, m_start, m_end);
}
}

private Point m_startPoint = Point.Empty;
List <LineObj> lineList = new List <LineObj> ();
public DrawLine()
{
InitializeComponent();
}
private void drawLine(Graphics graphics, Point startPoint, Point endPoint)
{
BufferedGraphicsContext context = BufferedGraphicsManager.Current;
BufferedGraphics bg = context.Allocate(graphics, this.ClientRectangle);
bg.Graphics.Clear(this.BackColor);
foreach (LineObj line in this.lineList)
{
line.Draw(bg.Graphics, SystemPens.ControlText);
}
bg.Graphics.DrawLine(SystemPens.ControlText, startPoint, endPoint);
bg.Render();
bg.Dispose();
bg = null;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
foreach (LineObj line in this.lineList)
{
line.Draw(e.Graphics, SystemPens.ControlText);
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
this.m_startPoint = new Point(e.X, e.Y);
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (e.Button == MouseButtons.Left)
{
this.drawLine(this.CreateGraphics(), this.m_startPoint, new Point(e.X, e.Y));
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
LineObj line = new LineObj(this.m_startPoint, e.Location);
this.lineList.Add(line);
}
}
}