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

★☆★☆★ GDI+问题——如何在鼠标画直线时绘制参考线 ★☆★☆★
程序功能描述:当鼠标在一个图片框中移动时,需作出从原点O到鼠标当前点的一条虚线(同时还有显示当前坐标等一系列操作,功能较简单)。但GDI+似乎无此功能,这几天使出浑身解数还是搞不定。唉……
(设原点O为图片框上任一个Point)
      请高手赐教!为兼顾整个程序的流畅性,请尽量用简单几行代码实现,无需过多枝节。

------解决方案--------------------
mousemove事件中
Graphics g = picturebox.CreateGraphics;
g.DrawLine(new Pen(Color.Red,1),new Point(0,0),new Point(e.x,e.y));
------解决方案--------------------
private bool isDraw = false;
//定义原点
private Point origalPoint;

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
isDraw = true;
origalPoint = new Point(e.X, e.Y);
}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (isDraw == true)
{
Graphics g = pictureBox1.CreateGraphics();
Pen basePen = new Pen(Color.Black);
Point currentPoint = new Point(e.X,e.Y);
g.DrawLine(basePen, origalPoint, currentPoint);
origalPoint = currentPoint;
//pictureBox1.Refresh();
g.Dispose();
basePen.Dispose();
}
}

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
isDraw = false;
}