Zombie
Zombie

Reputation: 23

PictureBox in the Background?

I want to display a GIF as background in my Form that I created with PowerShell.

I already figured out how to display the GIF and it works great, but of course all my Buttons and Textboxes etc. disappear behind it.

Upvotes: 1

Views: 2718

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200453

Background images are assigned via the BackgroundImage property of the form:

Add-Type -Assembly System.Windows.Forms

$form = New-Object Windows.Forms.Form

$img = [Drawing.Image]::FromFile('C:\path\to\your.gif')
$form.BackgroundImage = $img
$form.BackgroundImageLayout = 'Tile'

$form.ShowDialog()

See here for more information about using forms in PowerShell.

For displaying an animated GIF (you should've mentioned that in your question) it seems a PictureBox element is required. You can get it behind other elements by adding it after other elements (new elements are placed behind/below existing elements):

$img = [Drawing.Image]::FromFile('C:\path\to\your.gif')

$picbox = New-Object Windows.Forms.PictureBox
$picbox.Width  = $img.Size.Width
$picbox.Height = $img.Size.Height
$picbox.Image  = $img

$form.Controls.Add($otherElement)
$form.Controls.Add($yetAnotherElement)
$form.Controls.Add($picbox)

or by sending it to the back via the SendToBack() method:

$img = [Drawing.Image]::FromFile('C:\path\to\your.gif')

$picbox = New-Object Windows.Forms.PictureBox
$picbox.Width  = $img.Size.Width
$picbox.Height = $img.Size.Height
$picbox.Image  = $img

$form.Controls.Add($picbox)
$form.Controls.Add($otherElement)
$form.Controls.Add($yetAnotherElement)

$picbox.SendToBack()

Upvotes: 1

Related Questions