1

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.

Michael Schwartz
  • 8,153
  • 14
  • 81
  • 144
  • You already know that BackgroundImage doesn't animate. So assign WidgetBG.Image instead. – Hans Passant May 07 '12 at 19:16
  • Code updated! Now my problem is I'm unable to move each borderless form. You know how to fix this problem? – Michael Schwartz May 07 '12 at 19:59
  • Well, of course, it doesn't have a border or title bar. So don't make it borderless. http://stackoverflow.com/a/3102238/17034 – Hans Passant May 07 '12 at 20:03
  • For this particular application. I'm purposely making it so it has a borderless form. My only problem is moving it, as I get this error "Unable to cast object of type 'System.Windows.Forms.PictureBox' to type 'System.Windows.Forms.Form'." – Michael Schwartz May 07 '12 at 20:18

1 Answers1

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
Nick
  • 1,128
  • 7
  • 12
  • It worked decent, until the gif goes back to frame 1, and until I move it. As I get the following error - http://img32.imageshack.us/img32/3042/errorzm.png You can download the project, and source code via DeviantART. Of course with the gif problem I made sure when releasing to have it disabled until this error is resolved. http://mikethedj4.deviantart.com/art/WidgetArea-Use-Images-On-Your-HD-As-A-Widget-300769639 – Michael Schwartz May 08 '12 at 19:27