日期:2014-05-20  浏览次数:21269 次

gdi+ 画线问题 ?MouseMove时 怎么能实时看到画线的情况呢?
MouseDown   时   获取线的起点  

MouseUp   时   获取线的终点

Invalidate()

画出一条线

可是MouseMove时   怎么能实时看到画线的情况呢?
还需要用到什么?
我不是简单设一个起点设一个终点
而后画条线段
而是再按着左键不放移动过程中
实时看到画线的情况
就像windows中画图程序
画线段那样


------解决方案--------------------
楼主参考如下的在窗体上画线的代码(VS2005):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DoubleBufferDraw
{
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);
}
}
public partial class Form2 : Form
{
private Point m_startPoint = Point.Empty;
List <LineObj> lineList = new List <LineObj> ();
public Form2()
{
InitializeComponent();
}
private void drawCircle(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 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.drawCircle(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);
}
}
}
------解决方案--------------------
mousemove方法中获得当前x,y,然后调用drawline就可以了