【问题标题】:powershell NET Form set transparent backgroundpowershell NET Form设置透明背景
【发布时间】: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")

但是,当然,这会引发关于“控件不允许透明背景颜色”的错误。这是因为(我认为?)我应该将setStyleSupportsTransparentBackColorUserPaint 一起使用,但我不知道如何设置它的样式......我想是这样的:

$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 之类的东西...

【问题讨论】:

标签: c# .net powershell


【解决方案1】:

OP 更新 --- 根据您的回复删除了之前的答案。

我认为有一个误解...我不想设置它的不透明度 (这也会影响孩子),我想将不透明度设置为 父级或如果可能将背景设置为 ARGB 数据 $Form.BackColor = [System.Drawing.Color]::FromArgb(80,15,78,2) 和 文本应该仍然是不透明的黑色

经过更多的T&E,表单不支持将其设置为透明,对象就从做。 Sapien PowerShell Studio 等工具中的对象浏览器根本不公开它,也没有办法这样做。

在挖掘了一些 C/C++ 论坛的资料后证实了这句话,他们所能推测的,它从未被添加到基本/默认 WinForm 中。其他人报告解决此问题的方法是使用以下方法或类似方法。同样不是 PowerShell 原生的东西,而是一个基本的 WinForm 限制。

private void Form1_Load(object sender, EventArgs e) 
{ 
    //Hide our form from user  
    this.SetStyle (ControlStyles.SupportsTransparentBackColor, true); 
    this.TransparencyKey = Color.FromKnownColor(KnownColor.Control);
    this.Update();
}

观看此视频 - 再次不是 PowerShell 特定的东西,只是 WinForms 必须做些什么才能获得这种效果。

Creating a transparent form in window in windows forms application

总而言之,恕我直言,如果您需要这种控制,我会转向 WPF。除了行业公认的立场,WinForms 被认为是折旧的。 WPF 是操作/活动所在的位置。

【讨论】:

  • 我认为有一个误解......我不想设置它的不透明度(这也会影响孩子),我想只为父母设置不透明度,或者如果可能的话将背景设置为ARGB 数据 $Form.BackColor = [System.Drawing.Color]::FromArgb(80,15,78,2) 并且文本应该仍然是不透明的黑色
  • 好的,现在这是另一回事,并不是真正的 PowerShell 问题。因此,一个通用的 Winform/WPF 属性用例。此外,除非我镜像它们,否则我从来没有让我的子表单继承父表单的设置。默认情况下,它们是唯一设置。我刚刚做了一个快速的 MDI 示例,以显示孩子没有显示为透明,尽管父母在回复之前。但是,您在表单上的对象不透明。我没有见过或尝试过这样的事情,从视觉上看,这似乎是一种奇怪的用户体验。所以,有时间看看我能不能做到这一点。
【解决方案2】:

诀窍是将TransparencyKey 设置为与BackColor 相同。

如果您将具有白色背景的图像添加到表单并将键设置为白色,则只会显示其他颜色。

但我希望这应该能如你所愿:

Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

$Form               = New-Object system.Windows.Forms.Form
$Form.StartPosition = 'CenterScreen'
$Form.AutoSize      = $true

#Important here to set the transparency key the same as the back colour.
$transKey               = "#c0c0c0"
$Form.BackColor         = $transKey # [Control]
$Form.TransparencyKey   = $transKey

$Form.Font              = [System.Drawing.Font]::New("Segoe UI", 50, [System.Drawing.FontStyle]::Regular) # Bold, Italic, Regular, Strikeout or Underline
$Form.ForeColor         = "Black" # [ControlText]
$Form.FormBorderStyle   = [System.Windows.Forms.FormBorderStyle]::None
$Form.Text              = "Sample Form"

$Label                     = New-Object System.Windows.Forms.Label
$Label.AutoSize            = $True
$Label.Text                = "THIS FORM IS VERY SIMPLE."
$Form.Controls.Add($Label)

$Form.ShowDialog()

【讨论】:

    猜你喜欢
    • 2018-12-25
    • 2016-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-27
    • 2021-04-27
    • 2011-04-14
    • 1970-01-01
    相关资源
    最近更新 更多