【发布时间】:2019-08-07 15:23:45
【问题描述】:
我想在由 powershell 脚本创建的 NET 表单中设置透明背景:
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName System.Windows.Forms
$Form = New-Object system.Windows.Forms.Form
$Form.Location = New-Object System.Drawing.Point(0, 0);
$Form.StartPosition = "manual"
$Form.AutoSize = $true;
$Form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::None
$Form.Text = "Sample Form"
$Font = New-Object System.Drawing.Font("Times New Roman",18,[System.Drawing.FontStyle]::Italic)
$Form.Font = $Font
$Label = New-Object System.Windows.Forms.Label
$Label.Text = "This form is very simple."
$Label.AutoSize = $True
$Form.Controls.Add($Label)
$Form.ShowDialog()
pause
我想将背景颜色设置为透明,所以我应该添加:
$Form.BackColor = [System.Drawing.Color]::FromName("Transparent")
但是,当然,这会引发关于“控件不允许透明背景颜色”的错误。这是因为(我认为?)我应该将setStyle 与SupportsTransparentBackColor 和UserPaint 一起使用,但我不知道如何设置它的样式......我想是这样的:
$Form.SetStyle([System.Windows.Forms]::SupportsTransparentBackColor, true)
#or
$Form.SetStyle([System.Windows.Forms]::SupportsTransparentBackColor, $true)
#or
$Form.SetStyle= ([System.Windows.Forms]::SupportsTransparentBackColor, true)
#or
$Form.SetStyle= ([System.Windows.Forms]::SupportsTransparentBackColor, $true)
但它说 $Form 不存在 SetStyle。我应该如何正确启用透明背景?顺便说一句,我已经看到了这些帖子\来源:1st; 2nd; 3rd; 4th; 5th;
我不想使用“TransparencyKey”,因为它会在标签上留下一些彩色边框...
EDIT 17/03/2019
@postanote 链接了我在其中的两个帖子发现我可以使用:
$Form.AllowTransparency = $true
#and (but it doesn' work)
$Form.WindowStyle = $true
我还发现使用 $Form | Format-List -Property * 可以检索表单的所有属性,但我看不到 WindowStyle 或 SetStyle 之类的东西...
【问题讨论】:
-
你的意思是Form.Opacity 可能吗?
-
查看这些主题 --- 具有透明背景的 WPF 窗口包含不透明控件 [重复] ... stackoverflow.com/questions/21461017/… ... 在不更改内容不透明度的情况下更改背景不透明度 ... stackoverflow.com/questions/12646906/…
-
@postanote 我更新了我的帖子,对你提供给我的帖子进行了一些测试
标签: c# .net powershell