【问题标题】:keep pictureBox's corner outside the bounds of the panel将图片框的角保持在面板边界之外
【发布时间】:2014-02-03 14:03:05
【问题描述】:

这是我的代码:

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

这是图片,尚未拖动。当然它不适合屏幕,虽然顶部和左侧是图片的边缘。 (灰色为面板)

现在,如果我把它拖到左边,这就是它的样子..

这种情况还可以,因为图片真的很大,所以我需要能够拖动它。正如您在右下角看到的那样,现在可以看到面板,因为我拖动了左上角图片太多(不正确)

现在,我真正想要的是它看起来像这样..

当我拖动时,例如 picture #2,我希望图片的边缘保持原位。用户现在不能将它拖到左上角,因为那是图片的最后一部分(从技术上讲,用户不能看到面板的背景)同样的事情也适用于另一边。

希望这更清楚:)

【问题讨论】:

    标签: .net vb.net winforms panel picturebox


    【解决方案1】:

    试试这个代码:

    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
    

    【讨论】:

    • 不,先生,您正在反其道而行之,抱歉我没有解释。我需要图片框的一角,不能从面板内部过去,但可以从面板外面出去。
    • 我很高兴你能帮助我,尽管你没有得到我的问题。大声笑首先,我可以将图片拖到我想要的任何地方。但是,因此图片超出面板的边界,它完全可以,只要图片的边缘进入面板的边界。
    • 不,我显然不明白你的问题。阅读您的标题:"keep pictureBox's corner to the edge of the panel"。哪个边?左、上、右、下?全部?你想让它粘住吗?还是只是不超越?请编辑您的问题,因为我仍然不知道您的意思。另外,请考虑更改您的图片。
    • 更像它,虽然它只在右边缘和下边缘有效。顶部和左侧怎么样?我尝试为顶部和左侧添加相同的代码,但没有任何反应?
    • @AdorableVB 很简单,只需 Math.Min(x/y, 0)。我已经更新了答案。
    猜你喜欢
    • 2012-07-12
    • 1970-01-01
    • 2021-04-25
    • 1970-01-01
    • 2018-01-26
    • 1970-01-01
    • 2014-10-11
    • 2018-08-20
    • 1970-01-01
    相关资源
    最近更新 更多