在运行时在winform中拖动图片框

时间:2009-07-06 13:33:08

标签: vb.net winforms

我需要能够在vb.net中的winform周围拖放带有图像的图片框。

3 个答案:

答案 0 :(得分:5)

这是在C#中,但应该很容易在VB.Net中复制。

private int   currentX, currentY;
private bool  isDragging = false;

private void myPictureBox_MouseDown(object sender, MouseEventArgs e) 
{
  isDragging = true;

  currentX = e.X;
  currentY = e.Y;
}

private void myPictureBox_MouseMove(object sender, MouseEventArgs e) 
{
  if (isDragging) 
  {
    myPictureBox.Top = myPictureBox.Top + (e.Y - currentY);
    myPictureBox.Left = myPictureBox.Left + (e.X - currentX);
  }
}

private void myPictureBox_MouseUp(object sender, MouseEventArgs e) 
{
  isDragging = false;
}

答案 1 :(得分:1)

这是一些VB.NET


    Private IsDragging As Boolean = False
    Private StartPoint As Point

    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        StartPoint = PictureBox1.PointToScreen(New Point(e.X, e.Y))
        IsDragging = True
    End Sub

    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        If IsDragging Then
            Dim EndPoint As Point = PictureBox1.PointToScreen(New Point(e.X, e.Y))            
            PictureBox1.Left += (EndPoint.X - StartPoint.X)
            PictureBox1.Top += (EndPoint.Y - StartPoint.Y)
            StartPoint = EndPoint
        End If
    End Sub

    Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
        IsDragging = False
    End Sub

答案 2 :(得分:0)

与此DreamInCode.com thread中提供的答案类似的代码。 线程解决的另一件事是将图片框保持在表单的边界内并调整图片框的大小。