【问题标题】:Can I have rounded corners on my form in PowerShell?我可以在 PowerShell 中的表单上设置圆角吗?
【发布时间】:2021-05-25 08:58:22
【问题描述】:

我正在使用 Powershell Studio 创建一个 GUI。

注意:语言是 Powershell 脚本,而不是 C#。

有没有办法使用 Powershell 脚本在 Windows 窗体上设置圆角?或者也许是一种在 Powershell 脚本中实现 WPF 控件的方法。

这个视频正是我想要的,虽然我不能在 Powershell 脚本中包含 C#。 https://www.youtube.com/watch?v=LE3y5a0G4JA

【问题讨论】:

  • 我不能在 Powershell 脚本中包含 C# You can 实际上,但你不一定需要。
  • 如果您使用 XAML,请参阅 stackoverflow.com/a/120895/4066646。以编程方式,您可以将控件作为子控件放在边框对象中,$borderObj.AddChild( $obj )

标签: wpf winforms powershell user-interface


【解决方案1】:

PowerShell + WinForms + Windows API

如果你想在 PowerShell 中使用CreateRoundRectRgn 来创建圆形区域形式,你可以这样做:

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

PowerShell + WPF

如果你想在 WPF 中这样做,你可以这样做:

[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()

【讨论】:

  • 坦克@Jimi,这是因为表单和白色背景的控制颜色以及表单周围没有填充。我觉得现在看起来好多了。
  • 这是一个非常广泛的答案,我非常感谢!
  • @MichaelKayal 酷!没问题,感谢您的反馈:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多