如何在面板中移动图片框

时间:2017-05-13 11:19:57

标签: vb.net

我可以将一个图片框从一个Form移动到一个面板,如下所示(PictureBox1和Panel1都在同一个Form控件上)。但是一旦它在panel1中,PictureBox1就不会随MouseMove事件移动。我的意图是,一旦图片框进入面板区域,我想只在Panel区域内自由移动它(MouseMove)。任何人都可以帮我实现这个目标吗?

代码是这样的:

Dim locate As Point
    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseDown
        locate = New Point(-e.X, -e.Y)
    End Sub
    
    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseMove
        If e.Button = Windows.Forms.MouseButtons.Left Then
            Dim MoveTo As Point = Me.PointToClient(MousePosition)
            MoveTo.locate(locate.X, locate.Y)
            If PictureBox1.Location.X < Panel1.Location.X Then
                 PictureBox1.Location = MoveTo
            End If
        End If
    End Sub

0 个答案:

没有答案