我有一个Windows窗体应用程序,我希望能够动态创建可以通过鼠标移动的矩形(如图片框或使用graphics.drawrectangle)(例如使用Mouse_Move)并相互捕捉重叠(它们应该能够在移动时重叠,但在移动时不能重叠)。预先设置矩形的大小并固定,因此不需要调整大小,矩形应限制在面板中。
这段代码将矩形限制在一个区域(在我的情况下是与面板相同的区域),当拖动它时,该区域宽400像素,高2500像素,以及在此期间创建的任何图片框运行时通过向它们添加mousemove add-handler。可能有更好的方法来实现这一目标(请随意改进!)但如果使用图片框,这似乎可以解决问题:
Private Sub PictureBox_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseMove
Dim picBox As PictureBox = DirectCast(sender, PictureBox)
Static mousePosX As Single
Static mousePosY As Single
Dim currentx As Single
Dim currenty As Single
If e.Button = 0 Then
mousePosX = e.X
mousePosY = e.Y
Else
currentx = picBox.Left + (e.X - mousePosX)
currenty = picBox.Top + (e.Y - mousePosY)
If Not (currentx < 0 Or currentx + picBox.Width > 400) Then
picBox.Left = currentx
End If
If Not (currenty < 0 Or currenty + picBox.Height > 2500) Then
picBox.Top = currenty
End If
End If
End Sub
在发布的代码中here是一种制作多个图片框的方法&#34; snap&#34;移动时的方式类似于我想象他们做的方式。我希望你能了解我所追求的目标。
作为额外的奖励,如果可能的话,我希望能够创建以同样的方式创建和移动所有形状的圆圈,能够将它们全部保存在一张大图中,独立或在PDF / Word页面中。
我还应该补充一点,我这样做是为了一个爱好,并且只阅读了一两本关于VB.Net的书,而不仅仅是我通过在论坛上阅读帖子而自我思考这些以及Microsoft Developer Network上的示例和指南。因此,为了能够学习,如果您能够解释正在发生的事情,我将非常感激!
我非常感谢你在这件事上的所有帮助!
此致 威廉
答案 0 :(得分:0)
感谢我原帖的评论部分提供的所有帮助。我设法创造了一些能够完全符合我想要的东西,也可以分享代码,希望有人发现它有用。以下是代码:
Private startX, startY As Integer
Private Shapes As New List(Of PictureBox)
Private Const SnapDistance As Integer = 5
Private Sub pb_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim pb As PictureBox = DirectCast(sender, PictureBox)
pb.BringToFront()
If e.Button = Windows.Forms.MouseButtons.Left Then
startX = e.X
startY = e.Y
End If
End Sub
Private Sub pb_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim pb As PictureBox = DirectCast(sender, PictureBox)
Dim newPt As New Point(pb.Location.X - (startX - e.X), pb.Location.Y - (startY - e.Y))
For Each shape As PictureBox In panel.Controls
If Not (shape Is pb) Then
'If there are several objects (picturebox)
If Math.Abs(newPt.Y - (shape.Location.Y + shape.Height)) <= SnapDistance Then
newPt = New Point(newPt.X, shape.Location.Y + shape.Height)
ElseIf Math.Abs(newPt.Y - shape.Location.Y) <= SnapDistance Then
newPt = New Point(newPt.X, shape.Location.Y)
End If
If Math.Abs(newPt.X) <= 20 Then
newPt = New Point(0, newPt.Y)
ElseIf (newPt.X) < 0 Then
newPt = New Point(0, newPt.Y)
ElseIf Math.Abs(newPt.X - (shape.Location.X + shape.Width)) <= SnapDistance Then
newPt = New Point(shape.Location.X + shape.Width, newPt.Y)
ElseIf Math.Abs(newPt.X - shape.Location.X) <= SnapDistance Then
newPt = New Point(shape.Location.X, newPt.Y)
End If
If (newPt.X + pb.Width) > 400 Then
newPt = New Point(400 - pb.Width, newPt.Y)
ElseIf Math.Abs((newPt.X + pb.Width) - shape.Location.X) <= SnapDistance Then
newPt = New Point(shape.Location.X - pb.Width, newPt.Y)
ElseIf Math.Abs((newPt.X + pb.Width) - (shape.Location.X + shape.Width)) <= SnapDistance Then
newPt = New Point((shape.Location.X + shape.Width) - (pb.Width), newPt.Y)
End If
If (panel.AutoScrollPosition.Y + newPt.Y + pb.Height) > 2500 Then
newPt = New Point(newPt.X, 2500 - pb.Height)
ElseIf Math.Abs((newPt.Y + pb.Height) - shape.Location.Y) <= SnapDistance Then
newPt = New Point(newPt.X, shape.Location.Y - pb.Height)
ElseIf Math.Abs((newPt.Y + pb.Height) - (shape.Location.Y + shape.Height)) <= SnapDistance Then
newPt = New Point(newPt.X, (shape.Location.Y + shape.Height) - (pb.Height))
End If
If (newPt.Y - panel.AutoScrollPosition.Y) < 0 Then
newPt = New Point(newPt.X, panel.AutoScrollPosition.Y)
ElseIf panel.AutoScrollPosition.Y = 0 And Math.Abs(newPt.Y) <= 20 Then
newPt = New Point(newPt.X, 0)
End If
If (newPt.Y + pb.Height + Math.Abs(panel.AutoScrollPosition.Y)) > 2500 Then
newPt = New Point(newPt.X, 2500 - pb.Height - Math.Abs(panel.AutoScrollPosition.Y))
End If
Else
'If there are only one object (picturebox)
If (newPt.Y - panel.AutoScrollPosition.Y) < 0 Then
newPt = New Point(newPt.X, panel.AutoScrollPosition.Y)
ElseIf panel.AutoScrollPosition.Y = 0 And Math.Abs(newPt.Y) <= 20 Then
newPt = New Point(newPt.X, 0)
End If
If Math.Abs(newPt.X) <= 20 Or (newPt.X) < 0 Then
newPt = New Point(0, newPt.Y)
End If
If (newPt.X + pb.Width) > 400 Then
newPt = New Point(400 - pb.Width, newPt.Y)
End If
If (newPt.Y + pb.Height + Math.Abs(panel.AutoScrollPosition.Y)) > 2500 Then
newPt = New Point(newPt.X, 2500 - pb.Height - Math.Abs(panel.AutoScrollPosition.Y))
End If
End If
Next
End If
Next
pb.Location = newPt
End If
End Sub
再次感谢所有给予您支持并花时间帮助我的人!