一个画线的问题,请大家帮帮忙!
比如我有两个picturebox,我现在左边的picturebox1上确定一个点,然后又在右边的picturebox2上确定另一个点,目的是绘制一个直线。但是在确定第一个点后,鼠标在移动向第二点时,鼠标和第一点间的轨迹连线也要显示。 这样怎么实现? 请大家帮帮忙!
[最优解释]
用可逆线。只要注意一下可逆线是相对于屏幕坐标就可以了。简单的写了几句:
Public Class Form1
Dim WithEvents p1 As New Panel
Dim WithEvents p2 As New Panel
Dim EmptyPoint As New Point(-1, -1)
Dim StartPoint As Point
Dim EndPoint As Point
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
p1.Location = New Point(0, 0)
p1.Size = New Size(50, 50)
p1.BackColor = Color.Blue
p2.Location = New Point(100, 0)
p2.Size = New Size(50, 50)
p2.BackColor = Color.Red
Me.Controls.Add(p1)
Me.Controls.Add(p2)
Me.Size = New Size(200, 100)
StartPoint = New Point(EmptyPoint)
' EndPoint = New Point(EmptyPoint)
End Sub
Private Sub p1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles p1.MouseDown
StartPoint = p1.PointToScreen(e.Location)
EndPoint = StartPoint
End Sub
Private Sub p1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles p1.MouseMove
If StartPoint <> EmptyPoint Then
Dim tmpPoint As Point = p1.PointToScreen(e.Location)
ControlPaint.DrawReversibleLine(StartPoint, EndPoint, Color.Black)
EndPoint = tmpPoint
ControlPaint.DrawReversibleLine(StartPoint, EndPoint, Color.Black)
End If
End Sub
Private Sub p1_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles p1.MouseUp
ControlPaint.DrawReversibleLine(StartPoint, EndPoint, Color.Black)
StartPoint = New Point(EmptyPoint)
Dim PointOffP2 As Point = p2.PointToClient(p1.PointToScreen(e.Location))
If p2.ClientRectangle.Contains(PointOffP2.X, PointOffP2.Y) Then
MsgBox("鼠标在p2内抬起。坐标为: " & PointOffP2.ToString)
End If
End Sub
End Class
其实所有鼠标事件都发生在P1,和P2没什么关系。
[其他解释]
我想到的思路,在pic上点击或移动时时,可以将坐标点转换(PointToClient,PointToScreen),得到对应form上的坐标,在form(就算picture的父控件)的OnPaint中画两点间的线,picture上也要画,这样不就可以在两个pic间显示了吗
[其他解释]
1.当pic1 的moucedown的时候记下坐标1
2.当pic2的moucedown的时候记下坐标2
并且判断坐标1有没值。有值手动调用强行绘制。
3.在绘制重载事件里将不为空的两坐标绘制。
4.其他什么什么动作的时候清空2坐标。
[其他解释]
我们也在学习这个!!很难啊
[其他解释]
这个很好,虽然和我要的还有点出入,但是已经很接近了。谢谢大家~~