新手求助,弄了个画图的。。发现线是连着的

Shazam9 2016-09-25 04:54:10
如图,只能画一条线,如何让它们不连着?从而达到画多条线写字的效果。。





using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 写字板
{
public partial class Form1 : Form
{
bool drawflag = false;
bool down = false;
int X1;
int Y1;
List<Point> list;
public Form1()
{
InitializeComponent();
list = new List<Point>();
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen p = new Pen(Color.Red, 4);
if (list.Count > 2)
{
g.DrawLines(p, list.ToArray());
}
}

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
drawflag = true;
down = true;
}

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (drawflag == false)
return;
else if (down == true)
{
X1 = e.X;
Y1 = e.Y;
list.Add(new Point(e.X, e.Y));
}
this.Invalidate();
}

private void Form1_MouseUp(object sender, MouseEventArgs e)
{
drawflag = false;
}
}
}
...全文
137 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Forty2 2016-09-25
  • 打赏
  • 举报
回复
首先,要分成多个折线。 List<Point> list; List<Point[]> lists = new List<Point[]>(); private void Form1_MouseUp(object sender, MouseEventArgs e) { lists.Add(list.ToArray()); list.Clear(); } 其次,画图的时候,逐个画折线。 foreach(var points in lists) { if (...) g.DrawLines(points); } 最后你可能要进行抽样优化,直接追逐鼠标轨迹可能带来太多的点。
xuzuning 2016-09-25
  • 打赏
  • 举报
回复
你总是 list.Add(new Point(e.X, e.Y)); 如何能断的开?

110,549

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧