Protected Overrides Sub OnPaint(e As PaintEventArgs) Dim myPen As New Pen(Color.Red, 3) Dim myGraphics As Graphics = e.Graphics myGraphics.DrawLine(myPen, 20, 10, 200, 100) End Sub 'OnPaint End Class 'PlainForm [C#] class PlainForm : Form { protected override void OnPaint(PaintEventArgs e) { Pen myPen = new Pen(Color.Red, 3); Graphics myGraphics = e.Graphics; myGraphics.DrawLine(myPen, 20, 10, 200, 100); } }怎么写成控件就不晓得了
可以使用GDI+绘图函数
取鼠标两次点击的坐标然后绘线使用 GDI+ 和托管类接口绘制线条
下面的示例是用 C# 和 Visual Basic 编写的,但用任何能产生托管代码的语言都可以编写这些示例。要使用 GDI+ 和托管类接口绘制线条,您需要 Graphics 对象和 Pen 对象。获取 Graphics 对象引用的一种方法是通过窗体的 OnPaint 方法。OnPaint 方法的一个参数是类型 PaintEventArgs 的结构,它将 Graphics 对象作为其成员。
绘制线条涉及调用 Graphics 类的 DrawLine 方法。DrawLine 方法的第一个参数是 Pen 对象。与前面 GDI 示例中显示的技术(将钢笔选入设备上下文)相比,这是一个更加简单而灵活的方案。
[Visual Basic]
Class PlainForm
Inherits Form
Protected Overrides Sub OnPaint(e As PaintEventArgs)
Dim myPen As New Pen(Color.Red, 3)
Dim myGraphics As Graphics = e.Graphics
myGraphics.DrawLine(myPen, 20, 10, 200, 100)
End Sub 'OnPaint
End Class 'PlainForm
[C#]
class PlainForm : Form
{
protected override void OnPaint(PaintEventArgs e)
{
Pen myPen = new Pen(Color.Red, 3);
Graphics myGraphics = e.Graphics;
myGraphics.DrawLine(myPen, 20, 10, 200, 100);
}
}怎么写成控件就不晓得了
在控件里加上鼠标事件就行了
每画一条线就记录一下线两端的坐标
鼠标点击时判断是否点在线上或线的端点
可以在线的不同部位实现不同的动作
比如旋转、拖动、改变方向等等
WinForm我写的少,只是一个想法
上次我查得有这个控件
这次找不到了