VB.NET Move Dynamic Borderless Form via PictureBox

左心房为你撑大大i 提交于 2019-12-11 12:18:35

问题


From the dynamic form I added. I realized that I can't get a gif animation to run in the form's background. So I thought I'd give it a try by embeding a picturebox on the dynamic form, but it's not working hence why I'm here.

So on my main form (Form1) I have 2 buttons, openfiledialog, and a picturebox. When you click button1 you browse for the an image to display in the picturebox, and when you press button2 as you can see from the code below. It'll open a new form, but what I want is to have the picturebox displayed over the whole form, but also play the gif animation I selected from my main form via Form1 onto the dynamically embeded one, but in the picturebox. Now I can't use it as BackgroundImage so I'm using it as just an Image, but my problem now is I'm unable to move each borderless form, and am unable to close each as well.

Anyway here's my code.

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

Any help would be greatly appreciated.


回答1:


This is because you handle the event from your PictureBox not the form itself, so you can change the event handler as below

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


来源:https://stackoverflow.com/questions/10478629/vb-net-move-dynamic-borderless-form-via-picturebox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!