2

I want to set a transparent background in a NET form created by a powershell script:

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

I would like to set the background color as transparent so I should add:

$Form.BackColor = [System.Drawing.Color]::FromName("Transparent")

But, of course, this throws an error about that "The control doesn't allow transparent background colors". This is because (I think?) I should use setStyle with the SupportsTransparentBackColor and UserPaint but I don't know how to set its style... I thought something like this:

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

but It says that SetStyle doesn't exist for $Form. How am I supposed to correctly enable transparent background? By the way I already saw these posts\sources: 1st; 2nd; 3rd; 4th; 5th;
I don't want to use "TransparencyKey" becuse it leaves the label with some colored borders...
EDIT 17/03/2019
@postanote has linked two posts where I found that i could use:

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

I also found that with $Form | Format-List -Property * I could retrive all properties of the form, and I couldn't see anything like WindowStyle or SetStyle...

DadiBit
  • 769
  • 10
  • 22
  • Do you mean [Form.Opacity](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.form.opacity?view=netframework-4.7.2#System_Windows_Forms_Form_Opacity) perhaps? – Theo Mar 16 '19 at 21:37
  • 1
    See these topics --- WPF Window with transparent background containing opaque controls [duplicate] … https://stackoverflow.com/questions/21461017/wpf-window-with-transparent-background-containing-opaque-controls … Change Background opacity without changing content opacity … https://stackoverflow.com/questions/12646906/change-background-opacity-without-changing-content-opacity – postanote Mar 16 '19 at 21:51
  • @postanote I updated my post with some tests that I did with the posts you provided me – DadiBit Mar 17 '19 at 17:50

2 Answers2

0

Update for OP --- Deleted previous answer based on your response.

I think there was a misunderstood... I don't want to set its opacity (which effects childs too), I would like to set opacity just to the parent OR if possible set the background to an ARGB data $Form.BackColor = [System.Drawing.Color]::FromArgb(80,15,78,2) and the text should still be an opaque black

After more T&E, the form does not support setting it to transparent, object on the from do. The object browser in tools like Sapien PowerShell Studio, does not expose it at all, with no means to do so either.

After digging at some of the C/C++ forum stuff validates this saying, it was never added to the base/default WinForm from all they can surmise. The way others have reported getting around this is using the below or the like. Again not a PowerShell native thing but a base WinForm limitation.

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();
}

See this video - again not a PowerShell specific thing, just what has to be done for WinForms to get this effect.

Creating a transparent form in window in windows forms application

All-in-all, IMHO, I would move to WPF if you have need for this sort of control. Besides the industry accepted stance that WinForms is considered depreciated. WPF is where the action / activity is.

postanote
  • 15,138
  • 2
  • 14
  • 25
  • I think there was a misunderstood... I don't want to set its opacity (which effects childs too), I would like to set opacity just to the parent OR if possible set the background to an ARGB data `$Form.BackColor = [System.Drawing.Color]::FromArgb(80,15,78,2)` and the text should still be an opaque black – DadiBit Mar 17 '19 at 20:41
  • OK, now that is a different thing and not really a PowerShell issue. So, a general Winform/WPF properties use case. Also, I've never had my child forms inherit the settings from the parent unless I mirror them. By default they are unique settings. I just did a quick MDI sample to show the child does not show as transparent, though the parent is before replying here. Yet, you're after objects on the form not to be transparent. I've not seen or tried anything like that and visually, that seems an odd user experience. So, play time to see if I can do this. – postanote Mar 17 '19 at 22:00
0

The trick is to set the TransparencyKey the same as the BackColor.

If you add an image with a white background to the form and set the key to white then only the other colours will show.

But this should work how you would like, I hope:

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

Ste
  • 1,729
  • 1
  • 17
  • 27