Powershell NET窗体设置透明背景

时间:2019-03-16 20:51:55

标签: c# .net powershell

我想以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一起使用,但是我不知道如何设置其样式...我想这样的话:< / p>

$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)

但是它说$ Style不存在SetStyle。我应该如何正确启用透明背景?顺便说一下,我已经看过这些帖子\来源:1st; 2nd; 3rd; 4th; 5th;
我不想使用“ TransparencyKey”,因为它会使标签带有一些彩色边框...
编辑17/03/2019
@postanote链接了我发现可以使用的两个帖子:

$Form.AllowTransparency = $true
#and (but it doesn' work)
$Form.WindowStyle = $true

我还发现,使用$Form | Format-List -Property *可以检索表单的所有属性,而看不到WindowStyle或SetStyle之类的东西。

2 个答案:

答案 0 :(得分:0)

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

  

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

经过更多的T&E之后,该表单不支持将其设置为透明,即from上的对象。诸如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是操作/活动所在的地方。

答案 1 :(得分:0)

诀窍是将 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()

enter image description here