保持图片框的拐角面板的边界之外拐角、边界、面板、图片

2023-09-06 15:07:11 作者:青春續寫誰的永恒ゝ

下面是我的code:

Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseMove
    If (e.Button = Windows.Forms.MouseButtons.Left) Then
        Me.PictureBox1.Location = New Point((Me.pbpos.X + (Control.MousePosition.X - Me.offset.X)), _
                                            (Me.pbpos.Y + (Control.MousePosition.Y - Me.offset.Y)))
    End If
End Sub

这是图片,没有拖动。 Ofcourse它不适合在屏幕上,虽然顶部和左侧是图像的边缘。 (灰颜色的面板)

This is the picture, not yet dragged. Ofcourse it does not fit the screen, although the top and the left side is the edges of the picture. (the gray color is the panel)

现在,如果我将其拖动到左侧,这是它的样子.. 这种情况是只是确定由于图像是非常大的,这就是为什么我需要能够拖动它..你可以在右下角看到,现在,该面板将被看见,因为我拖图象在左上过多(这是不正确的)

Now if I drag it up to the left this is how it looks like.. This scenario is just OK since the picture is really big that's why I need to be able to drag it.. As you can see at the bottom right, now, the panel is to be seen because I dragged the picture too much in the upper left(which is not right)

现在,我真正想要的是,它看起来像这样.. 当我做拖,像照片#2 的我想要的图片的边缘,呆在那里。用户现在,必须不能够进一步将其拖动到左上,因为这是该图像的最后部分(的技术上,用户必须不能够看到这是面板的背景的)施加到另一侧的同样的事情。

Now, what I really want is for it to look like this.. When I do drag, like picture #2 I want the edges of the picture to stay right there. The user now, must not be able to drag it further to the upper left since that is the last part of the picture (technically, the user must not be able to see the background which is the panel) same thing applied to the other side.

希望这是更清楚:)

推荐答案

试试这个code:

Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseMove
    If (e.Button = Windows.Forms.MouseButtons.Left) Then

        Dim x As Integer = (Me.pbpos.X + (Control.MousePosition.X - Me.offset.X))
        Dim y As Integer = (Me.pbpos.Y + (Control.MousePosition.Y - Me.offset.Y))

        x = Math.Min(Math.Max(x, -(Me.PictureBox1.Width - Me.Panel1.Right)), 0)
        y = Math.Min(Math.Max(y, -(Me.PictureBox1.Height - Me.Panel1.Bottom)), 0)

        Me.PictureBox1.Location = New Point(x, y)

    End If
End Sub
 
精彩推荐
图片推荐