C# 允许用户在WinForms应用程序中的窗体上绘制一条线
如何使用户能够在表单中绘制线条C# 允许用户在WinForms应用程序中的窗体上绘制一条线,c#,winforms,drawing,C#,Winforms,Drawing,如何使用户能够在表单中绘制线条 用鼠标左键画图,用鼠标右键擦除。没那么复杂,一个简单的例子。。。我这里没有包括支票 Graphics g = null; // initialize in Form_Load with this.CreateGraphics() Point lastPoint; private void Form1_MouseDown(object sender, MouseEventArgs e) { lastPoint = e
用鼠标左键画图,用鼠标右键擦除。没那么复杂,一个简单的例子。。。我这里没有包括支票
Graphics g = null; // initialize in Form_Load with this.CreateGraphics()
Point lastPoint;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
lastPoint = e.Location;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
g.DrawLine(Pens.Blue, lastPoint, e.Location);
lastPoint = e.Location;
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
}
上面是一个例子,展示了如何在表单上绘制。理想情况下,应该将所有直线坐标放在一个集合中,并使用DrawLines()绘制该集合。然后使用表单::OnPaint中的Graphics::DrawLines()。单击鼠标右键时,只需清除点集合并强制重新绘制。没有那么复杂,一个快速示例。。。我这里没有包括支票
Graphics g = null; // initialize in Form_Load with this.CreateGraphics()
Point lastPoint;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
lastPoint = e.Location;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
g.DrawLine(Pens.Blue, lastPoint, e.Location);
lastPoint = e.Location;
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
}
上面是一个例子,展示了如何在表单上绘制。理想情况下,应该将所有直线坐标放在一个集合中,并使用DrawLines()绘制该集合。然后使用表单::OnPaint中的Graphics::DrawLines()。单击鼠标右键时,只需清除点集合并强制重新绘制。最终,您可能希望将所有绘制/擦除操作包含在特定控件中,并一致地管理其重新绘制/无效状态,但A9S6的回答肯定会让您开始并享受一些GDI+绘制;) 最终,您可能希望将所有绘图/擦除操作包含在特定控件中,并协调地管理其重画/无效状态,但A9S6的回答肯定会让您开始并享受一些GDI+绘图;) 绘制线并使用适当位置进行绘制的示例:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<Point> points = new List<Point>();
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
points.Add(e.Location);
Invalidate();
}
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
if (points.Count > 2)
{
e.Graphics.DrawLines(Pens.Black, points.ToArray());
}
}
}
公共部分类表单1:表单
{
公共表格1()
{
初始化组件();
}
列表点=新列表();
私有void Form1\u MouseMove(对象发送方,MouseEventArgs e)
{
if(e.Button==MouseButtons.Left)
{
添加点(如位置);
使无效();
}
}
私有void Form1_Paint(对象发送器、PaintEventArgs e)
{
如果(点数>2)
{
e、 图形.绘图线(钢笔.黑色,圆点.ToArray());
}
}
}
当列表变长时,ToArray()不是很好。绘制线并使用适当位置进行绘制的示例:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<Point> points = new List<Point>();
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
points.Add(e.Location);
Invalidate();
}
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
if (points.Count > 2)
{
e.Graphics.DrawLines(Pens.Black, points.ToArray());
}
}
}
公共部分类表单1:表单
{
公共表格1()
{
初始化组件();
}
列表点=新列表();
私有void Form1\u MouseMove(对象发送方,MouseEventArgs e)
{
if(e.Button==MouseButtons.Left)
{
添加点(如位置);
使无效();
}
}
私有void Form1_Paint(对象发送器、PaintEventArgs e)
{
如果(点数>2)
{
e、 图形.绘图线(钢笔.黑色,圆点.ToArray());
}
}
}
当列表变长时,ToArray()不是很好。检查此线程中的示例代码:检查此线程中的示例代码: