Powershell中Winform的透明背景

问题描述 投票:0回答:1

enter image description here

这里我有一个带有PictureBox的窗体。我删除了边框,现在我想使表单的背景透明,因此,当我启动脚本时,我们只看到该图像。

我正在制作一个具有非常规png形状的启动画面项目。我尝试了来自.Net的“ TransperancyKey = Color”问题,但是它不起作用。我希望它在PowerShell中运行。

# Importing Assembly for Windows Forms 
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

# Main form/SplashScreen Object
$SScreen                      = New-Object system.Windows.Forms.Form
$SScreen.BackColor            = [System.Drawing.Color]::FromArgb(255,0,0,0)
#$SScreen.BackColor = Color.Lime
$SScreen.StartPosition        = 1
$SScreen.FormBorderStyle      = 0

$img = [System.Drawing.Image]::Fromfile('./1.png')
$pictureBox = new-object Windows.Forms.PictureBox
$pictureBox.Width = $img.Size.Width
$pictureBox.Height = $img.Size.Height
$pictureBox.Image = $img
$SScreen.controls.add($pictureBox)

$SScreen.Width = $pictureBox.Width
$SScreen.Height = $pictureBox.Height

# Open the main form
Start-Process -FilePath "C:\Windows\system32\WindowsPowerShell\v1.0\powershell_ise.exe"
$SScreen.TopMost = $true
$SScreen.Show()

Start-Sleep -seconds 5

$SScreen.Close()```
windows forms powershell
1个回答
0
投票

您可以在不向表单添加图片框的情况下执行此操作,而只需为此使用表单自己的BackgroundImage属性。

当然,请确保您的图像具有透明度。

为此,我拍摄了您的图像并使所有黑色透明。因为我是从网页上复制的,所以它不如您的清晰,但这才是最重要的。

尝试:

# Importing Assembly for Windows Forms 
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

$img = [System.Drawing.Image]::Fromfile('./1.png')

# Main form/SplashScreen Object
$SScreen                   = New-Object system.Windows.Forms.Form
$SScreen.Width             = $img.Width
$SScreen.Height            = $img.Height
$SScreen.TopMost           = $true
$SScreen.BackgroundImage   = $img
$SScreen.AllowTransparency = $true
$SScreen.TransparencyKey   = $SScreen.BackColor
$SScreen.StartPosition     = 1
$SScreen.FormBorderStyle   = 0

# Open the main form
Start-Process -FilePath "C:\Windows\system32\WindowsPowerShell\v1.0\powershell_ise.exe"

$SScreen.Show()

Start-Sleep -Seconds 5

$SScreen.Close()
# tell Windows it can be removed from memory
$SScreen.Dispose()

结果:

demo

© www.soinside.com 2019 - 2024. All rights reserved.