批处理之家's Archiver

went 发表于 2020-9-30 00:34

PowerShell教程 界面编程,GUI,WinForm

[i=s] 本帖最后由 went 于 2022-9-23 23:38 编辑 [/i]

powershell写GUI界面程序很方便,有WinForm和WPF两种方式
powershell强大的数据处理能力有了C#的WinForm加持,很容易开发出一些简单易用的GUI程序
写个小教程,方便喜欢写GUI程序的朋友使用,也当做个笔记

1.HelloWorld[code]#&cls&@powershell -c "Get-Content '%~0' | Out-String | Invoke-Expression" & exit
#------------------------初始化区------------------------------------
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") #加载WinForm库

#------------------------界面区-------------------------------------
#0.设置主窗口尺寸
$mainFormWidth = 600                 #主窗口宽度
$mainFormHeight = 400                #主窗口高度

#1.创建主窗口
$form_main = New-Object "System.Windows.Forms.Form"
$form_main.Width = $mainFormWidth                                                   #设置主窗口宽度
$form_main.Height = $mainFormHeight                                                 #设置主窗口高度
$form_main.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen   #主窗口居中屏幕
$form_main.Text = "Hello WinForm"                                                   #主窗口标题

#2.创建并添加子控件:测试按钮
$btn_test = New-Object "System.Windows.Forms.Button"
$btn_test.Width = $mainFormWidth / 3                                         #设置测试按钮宽度
$btn_test.Height = $mainFormHeight / 3                                       #设置测试按钮高度
$testBtnX = ($form_main.Width - $btn_test.Width) / 2                         #测试按钮居中坐标X
$testBtnY = ($form_main.Height - $btn_test.Height) / 2                       #测试按钮居中坐标Y
$btn_test.Location = New-Object "System.Drawing.Point" $testBtnX,$testBtnY   #居中
$btn_test.Text = "Hello World!"                                              #测试按钮文字
$form_main.Controls.Add($btn_test)                                           #测试按钮控件设置为主窗口的子控件


#------------------------事件区-------------------------------------
#为测试按钮添加单击事件
$btn_test.add_Click({
    #弹出对话框
    [System.Windows.Forms.MessageBox]::Show("Hello World!","MessageBox",[System.Windows.Forms.MessageBoxButtons]::OK,[System.Windows.Forms.MessageBoxIcon]::Asterisk)
    #事件里有两个内置对象
    Write-Host $this        #当前控件对象,此处为测试按钮$btn_test  ->  [System.Windows.Forms.Button]
    Write-Host $_           #当前事件对象,此处为鼠标事件对象       ->  [System.Windows.Forms.MouseEventArgs]
})

#------------------------结  束-------------------------------------
#4.显示主窗口
$form_main.ShowDialog()

#运行方法1: 代码存为 test.ps1,然后cmd执行 powershell -ExecutionPolicy Bypass "X:\...\test.ps1" ,其中 "X:\...\test.ps1" 是ps1文件的全路径
#运行方法2: 代码存为 test.bat,然后双击bat文件运行[/code]#运行方法1: 代码存为 test.ps1,然后cmd执行 powershell -ExecutionPolicy Bypass "X:\...\test.ps1" ,其中 "X:\...\test.ps1" 是ps1文件的全路径
#运行方法2: 代码存为 test.bat,然后双击bat文件运行
高级玩法,初始化区添加以下脚本代码,隐藏cmd窗口[code]#------------------------初始化区------------------------------------
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") #加载WinForm库
#Windows API
$code=@"
    using System;
    using System.Runtime.InteropServices;
    public static class GetApi{
        [DllImport("user32.dll")]
        private static extern bool ShowWindow(IntPtr hWnd,uint showType); //声明 Windows API 函数
        [DllImport("kernel32.dll")]
        private static extern IntPtr GetConsoleWindow(); //声明 Windows API 函数
                public static bool ShowConsoleWindow(uint showType){
                        return ShowWindow(GetConsoleWindow(),showType);
                }
    }
"@
Add-Type -TypeDefinition $code
[GetApi]::ShowConsoleWindow(0)[/code]附一个powershell写的mp3下载器,已修改支持win7,ps2.0

qixiaobin0715 发表于 2020-9-30 11:03

直观,易上手。谢谢分享!!!

went 发表于 2021-6-15 14:01

壁纸程序图片源失效,已删除

whiter 发表于 2021-9-4 21:48

新的官方创建界面教程地址: https://docs.microsoft.com/zh-cn/powershell/scripting/samples/creating-a-custom-input-box?view=powershell-7.1

页: [1]

Powered by Discuz! Archiver 7.2  © 2001-2009 Comsenz Inc.