在窗体上画一条线 [英] Drawing a Line on a Form

查看:148
本文介绍了在窗体上画一条线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用c#在窗体上画一条线?

当我单击一点时,我想画一条线到另一点,然后在那条线上移动.

(例如Windows中的油漆)

感谢您的帮助.

How can I Draw a line on a Form using c#?

When I click on point I want to draw a line to another point and move the line around.

(like paint in windows)

thanks for any help.

推荐答案

您将必须处理MouseDown,MouseMove和MouseUp事件.
1.在MouseDown中,您将获得直线的起点.
2.在MouseMove中,绘制线条以具有橡皮筋效果.
3.在MouseUp中,您将获得直线的终点.
4.在绘画"事件中,从起点到终点重新绘制线条.

答复之一已经有代码来划清界限.

我建议在面板或其他合适的容器上画线,而不是直接在表格上画线.
You will have to handle the MouseDown, MouseMove and MouseUp events.
1. In MouseDown, you will get the start point of line.
2. In MouseMove, draw the line to have rubberband effect.
3. In MouseUp you will get the end point of line.
4. In the Paint event, redraw the line from start point to endpoint.

One of the replies already has code to draw the line.

I would suggest to draw the line on a Panel or other suitable container rather than directly on the form.


作为Yusuf回答的补充,您必须结合使用窗体上的Mousedown和Mouseup处理程序.我不会帮助您,因为我认为您可以从提示中找出答案.
您需要获取一个起点并保持在起点附近.当用户释放按钮时,您将需要在表单上永久画线.在Yusuf的答案中已经很好地描述了实际绘制线条的过程,因此在这里我将不再重复.
As an addition to Yusuf''s answer, You would have to incorporate the use of the Mousedown and Mouseup handlers on the form. I won''t lead you by the hand because I think you would benefit from figuring this out from the hints.
You need to get a starting point and keep it around. When the user releases the button you will need to permanently draw the line on the form. The process of actually drawing the line is well described in Yusuf''s answer so I won''t repeat that part here.


请看
Look at this MSDN sample code.

System.Drawing.Pen myPen;
myPen = new System.Drawing.Pen(System.Drawing.Color.Red);
System.Drawing.Graphics formGraphics = this.CreateGraphics();
formGraphics.DrawLine(myPen, 0, 0, 200, 200);
myPen.Dispose();
formGraphics.Dispose();



来源:请参见上面的链接



Source: See link above


这篇关于在窗体上画一条线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆