VB.NET通过PictureBox移动动态无边框表单

时间:2012-05-07 07:59:57

标签: vb.net dynamic picturebox gif playback

从我添加的动态表单中。我意识到我无法在表单的背景中运行gif动画。所以我想我会尝试通过在动态表单上嵌入一个图片框来试一试,但它不起作用因此我就在这里。

所以在我的主窗体(Form1)上我有2个按钮,openfiledialog和一个图片框。当您单击button1时,您将浏览要在图片框中显示的图像,并按下button2,如下面的代码所示。它会打开一个新的表单,但我想要的是将图片框显示在整个表单上,还可以通过Form1将我从主表单中选择的gif动画播放到动态嵌入的表单上,但是在图片框中。现在我不能将它用作BackgroundImage,所以我将它用作图像,但我现在的问题是我无法移动每个无边框形式,也无法关闭每个。

无论如何,这是我的代码。

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    WidgetForm = New Form()
    WidgetForm.ShowInTaskbar = False
    WidgetForm.TopMost = True
    WidgetForm.FormBorderStyle = Windows.Forms.FormBorderStyle.None
    WidgetForm.ContextMenuStrip = ContextMenuStrip2
    WidgetForm.Show()

    Dim WidgetBG As PictureBox = New PictureBox()
    sizew = Me.TextBox1.Text
    sizey = Me.TextBox2.Text
    WidgetBG.Size = New System.Drawing.Size(sizew, sizey)
    WidgetBG.Image = Image.FromFile(Me.OpenFileDialog1.FileName)
    WidgetBG.Location = New System.Drawing.Point(0, 0)
    WidgetBG.Visible = True
    WidgetForm.Controls.Add(WidgetBG)

    opac = Me.TextBox3.Text
    WidgetForm.Opacity = opac
    WidgetForm.Size = New System.Drawing.Size(sizew, sizey)

    'Add the event here
    AddHandler WidgetBG.MouseDown, AddressOf WidgetBG_MouseDown
    AddHandler WidgetBG.MouseMove, AddressOf WidgetBG_MouseMove
    AddHandler WidgetBG.MouseUp, AddressOf WidgetBG_MouseUp
    AddHandler WidgetBG.DoubleClick, AddressOf WidgetBG_DoubleClick
End Sub

Private Sub WidgetBG_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    If e.Button = Windows.Forms.MouseButtons.Left Then
        drag = True
        mousex = Windows.Forms.Cursor.Position.X - CType(sender, Form).Left
        mousey = Windows.Forms.Cursor.Position.Y - CType(sender, Form).Top
    End If

    Timer1.Enabled = True
    Timer1.Interval = 2500
End Sub

Private Sub WidgetBG_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    If drag Then
        CType(sender, Form).Top = Windows.Forms.Cursor.Position.Y - mousey
        CType(sender, Form).Left = Windows.Forms.Cursor.Position.X - mousex
    End If
End Sub

Private Sub WidgetBG_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    Timer1.Enabled = False
    drag = False
End Sub

Private Sub WidgetBG_DoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    CType(sender, Form).Close()
End Sub

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

这是因为您从PictureBox处理事件而不是表单本身,因此您可以更改事件处理程序,如下所示

Private Sub WidgetBG_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    If e.Button = Windows.Forms.MouseButtons.Left Then
        drag = True

        'Use FindForm() here to get your parent form
        'You can also use CType(sender, PictureBox).Parent.Left which makes more sense
        mousex = Windows.Forms.Cursor.Position.X - CType(sender, PictureBox).FindForm().Left
        mousey = Windows.Forms.Cursor.Position.Y - CType(sender, PictureBox).FindForm().Top
    End If

    Timer1.Enabled = True
    Timer1.Interval = 2500
End Sub

Private Sub WidgetBG_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    If drag Then
        CType(sender, PictureBox).FindForm().Top = Windows.Forms.Cursor.Position.Y - mousey
        CType(sender, PictureBox).FindForm().Left = Windows.Forms.Cursor.Position.X - mousex
    End If
End Sub

Private Sub WidgetBG_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    Timer1.Enabled = False
    drag = False
End Sub

Private Sub WidgetBG_DoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    CType(sender, PictureBox).FindForm().Close()
End Sub