MichaelKayal
MichaelKayal

Reputation: 15

Can I have rounded corners on my form in PowerShell?

I'm using Powershell Studio to create a GUI.

Take note: the langauge is Powershell script, not C#.

Is there a way to have rounded edges on a windows form using Powershell script? Or perhaps a way to implement WPF controls in the Powershell script.

This video is exactly what I want, though I can't include C# in Powershell script. https://www.youtube.com/watch?v=LE3y5a0G4JA

Upvotes: 1

Views: 3134

Answers (1)

Reza Aghaei
Reza Aghaei

Reputation: 125227

PowerShell + WinForms + Windows API

If you would like to use CreateRoundRectRgn in PowerShell to create a round region form, you can do it like this:

using assembly System.Windows.Forms
using namespace System.Windows.Forms
using namespace System.Drawing
$code = @"
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern IntPtr CreateRoundRectRgn(int nLeftRect, int nTopRect,
    int nRightRect, int nBottomRect, int nWidthEllipse, int nHeightEllipse);
"@
$Win32Helpers = Add-Type -MemberDefinition $code -Name "Win32Helpers" -PassThru
$form = [Form] @{
    ClientSize = [Point]::new(400,100);
    FormBorderStyle = [FormBorderStyle]::None;
    BackColor = [SystemColors]::ControlDark
}
$form.Controls.AddRange(@(
    ($button1 = [Button] @{Location = [Point]::new(10,10); Text = "Close";
    BackColor = [SystemColors]::Control;
    DialogResult = [DialogResult]::OK })
))
$form.add_Load({
    $hrgn = $Win32Helpers::CreateRoundRectRgn(0,0,$form.Width, $form.Height, 20,20)
    $form.Region = [Region]::FromHrgn($hrgn)
})
$null = $form.ShowDialog()
$form.Dispose()

enter image description here

PowerShell + WPF

If you would like to do it in WPF, you can do it like this:

[xml]$xaml = @"
<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Name="Window" WindowStyle="None" 
    Width= "400" Height="100"
    Background="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}"
    AllowsTransparency="True">
    <Window.Clip>
        <RectangleGeometry Rect="0,0,400,100" RadiusX="20" RadiusY="20"/>
    </Window.Clip>
    <Grid x:Name="Grid">
        <Button x:Name="button1" Content="Close" HorizontalAlignment="Left"
         VerticalAlignment="Top" Width="75" Margin="10,10,0,0"/>
    </Grid>
</Window>
"@
$reader = (New-Object System.Xml.XmlNodeReader $xaml)
$window = [Windows.Markup.XamlReader]::Load($reader)
$button1 = $window.FindName("button1")
$button1.Add_Click({
   $window.Close();
})
$window.ShowDialog()

enter image description here

Upvotes: 1

Related Questions