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



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

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

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

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

任何帮助将不胜感激。

这是因为您从 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

最新更新