找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 30348|回复: 3

[原创教程] PowerShell教程 界面编程,GUI,WinForm

[复制链接]
发表于 2020-9-30 00:34:50 | 显示全部楼层 |阅读模式
本帖最后由 went 于 2022-9-23 23:38 编辑

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

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

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

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

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


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

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

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

评分

参与人数 3技术 +3 收起 理由
qixiaobin0715 + 1 感谢
yhcfsr + 1 感谢分享
老刘1号 + 1 感谢分享

查看全部评分

发表于 2020-9-30 11:03:03 | 显示全部楼层
直观,易上手。谢谢分享!!!
 楼主| 发表于 2021-6-15 14:01:07 | 显示全部楼层
壁纸程序图片源失效,已删除
发表于 2021-9-4 21:48:04 | 显示全部楼层
新的官方创建界面教程地址: https://docs.microsoft.com/zh-cn/powershell/scripting/samples/creating-a-custom-input-box?view=powershell-7.1
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-16 23:15 , Processed in 0.019777 second(s), 9 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表