Перетаскивание Picturebox внутри winform во время выполнения

Мне нужно иметь возможность перетаскивать свою картинку с изображением вокруг моей winform в vb.net.

3 ответа

Решение

Это в 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;
}

Вот немного 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

Код, аналогичный приведенным ответам, существует в этой теме DreamInCode.com. Еще одна вещь, к которой обращается поток, - это сохранение окна рисунка в пределах формы и изменение размера окна рисунка.

Другие вопросы по тегам