Picturebox 面板内平移边界
我的图像位于面板内部,我为只能移动的边界设置了一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,如果您的 PictureBox 位于面板内,则无需考虑面板的位置,因为 PictureBox 的位置将在面板的左上角归零。
此条件:
应更改为此条件:
此外,您遇到的问题是由于您的事件处理程序在将 PictureBox 从 0,0 移动到将 PictureBox 移动到增量位置之间切换。
例如:
当您向右拖动 PictureBox 使其左边界超过面板的左边界(即 PictureBox.Location.X > 0)时,您的 if 语句的条件将计算为 False,并且 PictureBox 的位置将设置为 0。但是,由于您现在更改了其位置,因此再次触发 MouseMove 事件,这次 if 语句的条件计算为 True,并且 PictureBox 的位置设置为增量位置。
再次触发 MouseMove 事件并重复该场景,来回翻转 PictureBox 的位置,从而导致抖动效果。
您可以通过更改条件以依赖 PictureBox 的新位置而不是当前位置来解决此问题:
此条件:
应更改为此条件:
这解决了抖动问题,但您的代码仅处理以下情况 : PictureBox 被拖向右侧和底部。
您可以通过将计算移动到单独处理每个轴的单独函数中来简化代码,而不是编写更多条件:
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:
should be changed to this condition:
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:
should be changed to this condition:
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: