PowerShell GUI 之基础知识

您所在的位置:网站首页 powershell书 PowerShell GUI 之基础知识

PowerShell GUI 之基础知识

2023-03-15 06:28| 来源: 网络整理| 查看: 265

一直想学习PowerShell怎么弄成带GUI的小tools,会觉得很好玩很神奇。今天又是周末了,终于可以玩点自己喜欢的东西,那么就继续来更新我的blog吧。

要想生成一个GUI,那么得调用一个叫做System.Windows.Forms的命名空间。System.Windows.Forms 命名空间包含用于创建基于 Windows 的应用程序的类,以充分利用 Microsoft Windows 操作系统中提供的丰富的用户界面功能。

关于这个命名空间的详细说明请参考:System.Windows.Forms 命名空间

  关于.NET Framework 类库下面的命名空间汇总则可以参考:.NET Framework 类库

关于Forms的方法属性等详细说明则请参考:Form 类

好了,汇总资料上面都有了,那么我们开始来学习吧。用PowerShell生成一个GUI只需要简单的三步:

1.  加载System.Windows.Forms ;

2.  创建system.windows.forms.forms;

3.  调用 ShowDialog() 方法;

例子:

是不是很简单?一个form就这么出来了,好神奇对不?咱们继续往下走哈。

如果我们想添加点文字,改变点大小,修改下颜色怎么办呢?

1. 加个标题 给这个form加个标题,应该使用哪个属性?还是看上面贴出来的三个链接吧,先从命名空间中找到类,再从类中找到属性、方法。通过查找资料得知,Control.Text 属性是用来获取或设置与此控件关联的文本,然而Text已经替代了Control.Text,那么$owerShellForms.Text应该是会显示点什么吧?测试一下?到底是标题还是内容?

嗯,果然和预想的一样。

2.添加正文内容 有了标题,内容还是空空的,接下来要怎么添加内容呢?此时需要调用到的是label这个类,不再是form这个类了。 3.更改内容显示字体

是不是觉得“this is my first from”比较丑呢?我也觉得。我想改变一下它的大小,字体什么的,怎么办呢?

请参考system.drawing.font

其中,Bold表示加粗文本,Italic 表示倾斜文本,Regular 表示普通文本,Strikeout表示中间有直线通过的文本,Underline表示带下划线的文本。

4.调整form大小 5.添加滚动条 6.自动调整大小 我相信更多的小伙伴是想实现自动调整大小吧,虽然我没可以定义form的长宽,但是总感觉预估不好啊,对吧。 7.更多属性设置 8.更改背景 颜色请参考system.drawing.color 或者博客颜色对照表 9.添加背景图片 10.更改form图标 最后,附上完整的脚本 Add-Type -AssemblyName System.Windows.Forms $PowerShellForms = New-Object system.Windows.Forms.Form $PowerShellForms.Text="Melody's Form" #$PowerShellForms.width=500 #$PowerShellForms.height=500 $Images = [system.drawing.image]::FromFile("c:\Users\melody\Desktop\55.png") $PowerShellForms.BackgroundImage = $Images $PowerShellForms.BackgroundImageLayout = "None" $PowerShellForms.Width = $Images.Width $PowerShellForms.Height = $Images.Height #$PowerShellForms.AutoScroll = $True #$PowerShellForms.AutoSize = $True #$PowerShellForms.AutoSizeMode = "GrowAndShrink" #$PowerShellForms.MinimizeBox = $False #$PowerShellForms.MaximizeBox = $False #$PowerShellForms.WindowState = "Normal" $PowerShellForms.SizeGripStyle = "Hide" $Icons = [system.drawing.icon]::ExtractAssociatedIcon($PSHOME + "\powershell.exe") $PowerShellForms.Icon = $Icons #$PowerShellForms.BackColor = "Lime" #$PowerShellForms.ShowInTaskbar = $False #$PowerShellForms.Opacity = 0.8 #$PowerShellForms.StartPosition = "CenterScreen" $Fonts = New-Object System.Drawing.Font("Times New Roman",18,[System.Drawing.FontStyle]::Regular) $PowerShellForms.Font = $Fonts $Labels = New-Object System.Windows.Forms.Label $Labels.Text = "This is my first Form" $Labels.AutoSize = $True $Labels.BackColor = "Transparent" $Labels.ForeColor = "Lime" $PowerShellForms.Controls.Add($Labels) $PowerShellForms.ShowDialog()


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3