在面板上用鼠标画一条线 [英] Drawing a line by mouse in a panel
问题描述
我想用鼠标(交互式地)绘制一条线,我使用了C#和WinForm,该线应该在从起点(当在面板上按下鼠标时)到鼠标的当前位置的任何时间出现就像在 Paint 程序中画一条线。
I want to draw a line by mouse(interactively) , I used C# and WinForm, the line should appear at any time from the starting point(when the mouse press on the panel) to the current position of the mouse, exactly like drawing a line in Paint program.
但是代码产生很多行,我知道为什么,但我不知道如何克服这个问题
but the code produces a lot of lines, i know why but i don't know how to overcome this problem
这是我的代码:
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Graphics g;
Pen myPen = new Pen(Color.Red);
Point p = new Point();
bool flag = false;
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
flag = true;
p = e.Location;
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (flag)
{
g = panel1.CreateGraphics();
myPen.Width = 3;
Point p2 = new Point();
p2 = e.Location;
g.DrawLine(myPen, p, p2);
}
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
flag = false;
}
}}
有帮助吗?我想画很多线并使代码尽可能简单!
Any Help? i want to draw many lines and keep the code simple as possible!
推荐答案
您将需要更好地管理绘图。一些指针:
You will need to better manage the drawing. Some pointers:
- 不要使用
CreateGraphics
。而是使用控件已提供的Paint
事件。 - 在您自己的继承类中绘制图形。除非您在表单上绘制 ,否则不要绘制
Form
类。
- Don't use
CreateGraphics
. Instead, use thePaint
event already provided by the control. - Do your drawing in an inherited class of your own. Don't draw in the
Form
class unless you're drawing on the form.
这是一个示例类。它是从 Panel
继承的。只需将其添加到窗体中,例如使用 this.Controls.Add(new PanelWithMouseDraw());
。
Here's an example class. It's inherited from Panel
. Simply add this to a form, such as in the Form's constructor using something like this.Controls.Add(new PanelWithMouseDraw());
.
注意:这使用了元组
,我认为它需要.NET 4.0或更高版本。您可以用其他内容替换此结构,如果需要...,您只需要保存 Point
对的列表。
Note: this uses Tuple
which I believe requires .NET 4.0 or above. You could replace this structure with something else, if need be...you just need to keep a list of Point
pairs.
namespace WindowsFormsApplication1
{
public class PanelWithMouseDraw : Panel
{
private Point _origin = Point.Empty;
private Point _terminus = Point.Empty;
private Boolean _draw = false;
private List<Tuple<Point, Point>> _lines = new List<Tuple<Point, Point>>();
public PanelWithMouseDraw()
{
Dock = DockStyle.Fill;
DoubleBuffered = true;
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (e.Button == MouseButtons.Left)
{
_draw = true;
_origin = e.Location;
}
else
{
_draw = false;
_origin = Point.Empty;
}
_terminus = Point.Empty;
Invalidate();
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
if (_draw && !_origin.IsEmpty && !_terminus.IsEmpty)
_lines.Add(new Tuple<Point, Point>(_origin, _terminus));
_draw = false;
_origin = Point.Empty;
_terminus = Point.Empty;
Invalidate();
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (e.Button == MouseButtons.Left)
_terminus = e.Location;
Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
foreach (var line in _lines)
e.Graphics.DrawLine(Pens.Blue, line.Item1, line.Item2);
if (!_origin.IsEmpty && !_terminus.IsEmpty)
e.Graphics.DrawLine(Pens.Red, _origin, _terminus);
}
}
}
这篇关于在面板上用鼠标画一条线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!