Skip to content

Instantly share code, notes, and snippets.

@agowa
Last active August 1, 2021 19:23
Show Gist options
  • Save agowa/580655e84b8f8cd9cdf86413c2bb6459 to your computer and use it in GitHub Desktop.
Save agowa/580655e84b8f8cd9cdf86413c2bb6459 to your computer and use it in GitHub Desktop.
Show image on windows server core desktop (like bginfo background image)
Add-Type -AssemblyName System.Windows.Forms
$file = "C:\bginfo\bginfo.png"
[System.Windows.Forms.Application]::EnableVisualStyles()
[System.Windows.Forms.Application]::SetCompatibleTextRenderingDefault($false)
$form = [System.Windows.Forms.Form]::new()
$form | Add-Member -Name pictureBox1 -MemberType NoteProperty -Value ([System.Windows.Forms.PictureBox]::new())
([System.ComponentModel.ISupportInitialize]$form.pictureBox1).BeginInit()
$form.SuspendLayout()
$form.pictureBox1.Location = [System.Drawing.Point]::new(0,0)
$form.pictureBox1.Margin = [System.Windows.Forms.Padding]::new(0,0,0,0)
$form.pictureBox1.Name = "pictureBox1"
$form.pictureBox1.TabIndex = 0
$form.pictureBox1.TabStop = $false
$form.pictureBox1.Image = [System.Drawing.Bitmap]::new($file)
$form.pictureBox1.Size = [System.Drawing.Size]::new($form.pictureBox1.Image.Width, $form.pictureBox1.Image.Height)
$form.AutoScaleDimensions = [System.Drawing.SizeF]::new([float]0,[float]0)
$form.AutoScaleMode = [System.Windows.Forms.AutoScaleMode]::Font
$form.AutoSize = $true
$form.ClientSize = [System.Drawing.Size]::new($form.pictureBox1.Image.Width,$form.pictureBox1.Image.Height)
$form.ControlBox = $false
$form.Controls.Add($form.pictureBox1)
$form.Cursor = [System.Windows.forms.cursors]::Default
$form.Enabled = $false
$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::None
([System.ComponentModel.ISupportInitialize]$form.pictureBox1).EndInit()
$form.ResumeLayout($false)
$form.ShowInTaskbar = $false
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::Manual
$form.Location = [System.Drawing.Point]::new([System.Windows.Forms.Screen]::PrimaryScreen.WorkingArea.Width - $form.Width,[System.Windows.Forms.Screen]::PrimaryScreen.WorkingArea.Height - $form.Height)
$sendToBackground = {
$signature = @'
[DllImport("user32.dll")]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
[DllImport("user32.dll", SetLastError = true)]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
'@
$type = Add-type -MemberDefinition $signature -Name aaa -Using System.Text -PassThru
$type::SetWindowPos($form.WindowTarget.Handle, [System.IntPtr]::new(1), 0,0,0,0,0x0013)
$WS_EX_NOACTIVATE = 0x08000000L
$type::SetWindowLong($form.WindowTarget.Handle, -20, $WS_EX_NOACTIVATE)
$form.SendToBack()
}
$form.Add_Activated($sendToBackground)
$form.Add_Mouseclick($sendToBackground)
$resizeWindow = {
$form.WindowState = [System.Windows.Forms.FormWindowState]::Normal
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::Manual
$form.Location = [System.Drawing.Point]::new([System.Windows.Forms.Screen]::PrimaryScreen.WorkingArea.Width - $form.Width,[System.Windows.Forms.Screen]::PrimaryScreen.WorkingArea.Height - $form.Height)
}
$form.Add_Resize($resizeWindow)
# Update our position when display resolution changes
[Microsoft.Win32.SystemEvents]::add_DisplaySettingsChanged($resizeWindow)
[System.Windows.Forms.Application]::Run($form)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment