2012-05-07 64 views
1

從我添加的動態形式。我意識到我無法獲得gif動畫在窗體背景中運行。所以我想我會嘗試在動態表單上嵌入一個picturebox,但它不起作用,因此我在這裏。VB.NET通過PictureBox移動動態無邊界形式

所以在我的主窗體(Form1)上我有2個按鈕,openfiledialog和一個picturebox。當你點擊button1時,你可以瀏覽一個圖片以顯示在圖片框中,當你按下按鈕2時,你可以從下面的代碼中看到。它會打開一個新的表單,但我想要的是將圖片框顯示在整個表單上,但也可以將通過Form1從我的主表單中選擇的gif動畫播放到動態嵌入的表單上,但在picturebox中。現在我不能將它用作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 

任何幫助將不勝感激。

+0

你已經知道的BackgroundImage不動畫。因此改爲分配WidgetBG.Image。 –

+0

代碼已更新!現在我的問題是我無法移動每個無邊界形式。你知道如何解決這個問題嗎? –

+0

當然,它沒有邊框或標題欄。所以不要使其無邊界。 http://stackoverflow.com/a/3102238/17034 –

回答

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 
+0

它運行得很好,直到gif回到第1幀,直到我移動它。當我收到以下錯誤 - http://img32.imageshack.us/img32/3042/errorzm.png您可以通過DeviantART下載項目和源代碼。當然,在發佈gif問題時,我確信它將被禁用,直到解決此錯誤。 http://mikethedj4.deviantart.com/art/WidgetArea-Use-Images-On-Your-HD-As-A-Widget-300769639 –