Picturebox 面板内平移边界

发布于 2024-11-17 23:38:10 字数 814 浏览 3 评论 0原文

我的图像位于面板内部,我为只能移动的边界设置了一个 if 语句。当我尝试运行它时,当鼠标将其平移到边界之外时,它看起来很糟糕。这是我的平移代码:

If (mouse.Button = Windows.Forms.MouseButtons.Left) Then

  Dim mousePosNow As Point = mouse.Location

  Dim deltaX As Integer = mousePosNow.X - mouseDowns.X
  Dim deltaY As Integer = mousePosNow.Y - mouseDowns.Y

  Dim newX As Integer
  Dim newY As Integer

  If PictureBox1.Location.X <= Panel1.Location.X And PictureBox1.Location.Y <= Panel1.Location.Y And _
  (PictureBox1.Location.X + PictureBox1.Width) >= (Panel1.Location.X + Panel1.Width) And _
  (PictureBox1.Location.Y + PictureBox1.Height) >= (Panel1.Location.Y + Panel1.Height) Then

    newX = PictureBox1.Location.X + deltaX
    newY = PictureBox1.Location.Y + deltaY
  End If

  PictureBox1.Location = New Point(newX, newY)

End If

My image is inside the panel, I set up a if-statement for the boundary which it can only be moved. When I tried to run it, it looks crappy when the mouse has panned it outside the boundary. Here is my code for panning:

If (mouse.Button = Windows.Forms.MouseButtons.Left) Then

  Dim mousePosNow As Point = mouse.Location

  Dim deltaX As Integer = mousePosNow.X - mouseDowns.X
  Dim deltaY As Integer = mousePosNow.Y - mouseDowns.Y

  Dim newX As Integer
  Dim newY As Integer

  If PictureBox1.Location.X <= Panel1.Location.X And PictureBox1.Location.Y <= Panel1.Location.Y And _
  (PictureBox1.Location.X + PictureBox1.Width) >= (Panel1.Location.X + Panel1.Width) And _
  (PictureBox1.Location.Y + PictureBox1.Height) >= (Panel1.Location.Y + Panel1.Height) Then

    newX = PictureBox1.Location.X + deltaX
    newY = PictureBox1.Location.Y + deltaY
  End If

  PictureBox1.Location = New Point(newX, newY)

End If

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

征﹌骨岁月お 2024-11-24 23:38:10

首先,如果您的 PictureBox 位于面板内,则无需考虑面板的位置,因为 PictureBox 的位置将在面板的左上角归零。

此条件:

If PictureBox.Location.X <= Panel1.Location.X ...

应更改为此条件:

If PictureBox.Location.X <= 0

此外,您遇到的问题是由于您的事件处理程序在将 PictureBox 从 0,0 移动到将 PictureBox 移动到增量位置之间切换。

例如:
当您向右拖动 PictureBox 使其左边界超过面板的左边界(即 PictureBox.Location.X > 0)时,您的 if 语句的条件将计算为 False,并且 PictureBox 的位置将设置为 0。但是,由于您现在更改了其位置,因此再次触发 MouseMove 事件,这次 if 语句的条件计算为 True,并且 PictureBox 的位置设置为增量位置。
再次触发 MouseMove 事件并重复该场景,来回翻转 PictureBox 的位置,从而导致抖动效果。

您可以通过更改条件以依赖 PictureBox 的新位置而不是当前位置来解决此问题:

此条件:

If PictureBox.Location.X <= 0 ...

应更改为此条件:

If (PictureBox.Location.X + deltaX) <= 0 ...

这解决了抖动问题,但您的代码仅处理以下情况 : PictureBox 被拖向右侧和底部。

您可以通过将计算移动到单独处理每个轴的单独函数中来简化代码,而不是编写更多条件:

If (mouse.Button = Windows.Forms.MouseButtons.Left) Then

  Dim mousePosNow As Point = mouse.Location

  Dim deltaX As Integer = mousePosNow.X - mouseDowns.X
  Dim deltaY As Integer = mousePosNow.Y - mouseDowns.Y

  Dim newX As Integer = Clamp(PictureBox1.Location.X + deltaX, PictureBox1.Width, Panel1.Width)
  Dim newY As Integer = Clamp(PictureBox1.Location.Y + deltaY, PictureBox1.Height, Panel1.Height)

  PictureBox1.Location = New Point(newX, newY)
End If

...

Private Function Clamp(val As Integer, outerBound As Integer, innerBound As Integer) As Integer
  Dim newVal As Integer = val

  If newVal > 0 Then
    newVal = 0
  End If

  If newVal + outerBound < innerBound Then
    newVal = innerBound - outerBound
  End If

  Return newVal
End Function

First of all, if you've got your PictureBox inside your Panel, then you don't need to account for the Panel's location, since the PictureBox's location will be zeroed at the top-left of the Panel.

This condition:

If PictureBox.Location.X <= Panel1.Location.X ...

should be changed to this condition:

If PictureBox.Location.X <= 0

Also, the problem you're running into is due to that fact that your event-handler is flipping between moving the PictureBox from 0,0 to moving the PictureBox to the delta location.

E.g:
When you drag the PictureBox towards the right such that it's left boundary goes past the Panel's left boundary (i.e. PictureBox.Location.X > 0) then the condition of your if-statement evaluates to False and the PictureBox's location is set to 0. However, since you've now changed its location, the MouseMove event is triggered again and this time the condition of your if-statement evaluates to True and the PictureBox's location is set to the delta location.
Once again the MouseMove event is triggered and the scenario repeats, flipping the PictureBox's location back and forth, causing a jittering effect.

You can fix this by changing your condition to rely on the new location of the PictureBox, instead of the current location:

This condition:

If PictureBox.Location.X <= 0 ...

should be changed to this condition:

If (PictureBox.Location.X + deltaX) <= 0 ...

This fixes the jittering problem but your code only takes care of the case where the PictureBox is dragged towards the right and bottom.

Instead of writing more conditions, you could simplify your code by moving the calculations into a separate function that handles each axis separately:

If (mouse.Button = Windows.Forms.MouseButtons.Left) Then

  Dim mousePosNow As Point = mouse.Location

  Dim deltaX As Integer = mousePosNow.X - mouseDowns.X
  Dim deltaY As Integer = mousePosNow.Y - mouseDowns.Y

  Dim newX As Integer = Clamp(PictureBox1.Location.X + deltaX, PictureBox1.Width, Panel1.Width)
  Dim newY As Integer = Clamp(PictureBox1.Location.Y + deltaY, PictureBox1.Height, Panel1.Height)

  PictureBox1.Location = New Point(newX, newY)
End If

...

Private Function Clamp(val As Integer, outerBound As Integer, innerBound As Integer) As Integer
  Dim newVal As Integer = val

  If newVal > 0 Then
    newVal = 0
  End If

  If newVal + outerBound < innerBound Then
    newVal = innerBound - outerBound
  End If

  Return newVal
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文