keep pictureBox's corner outside the bounds of the panel

偶尔善良 提交于 2019-12-11 08:19:16

问题


Here is my 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

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)

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.

Hope this is much clearer :)


回答1:


Try this 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


来源:https://stackoverflow.com/questions/21131721/keep-pictureboxs-corner-outside-the-bounds-of-the-panel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!