Board logo

标题: [原创代码] powershell便携英汉词典 [打印本页]

作者: went    时间: 2021-5-16 13:56     标题: powershell便携英汉词典

保存为bat或ps1文件
  1. #&cls&@powershell -c "Get-Content '%~0' | Select-Object -Skip 1 | Out-String | Invoke-Expression" & exit
  2. cls
  3. #------------------------初始化区------------------------------------
  4. [void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") #加载WinForm库
  5. [void][System.Reflection.Assembly]::LoadWithPartialName("System.Web.Extensions")
  6. $url = 'http://dict-co.iciba.com/api/dictionary.php?type=json&key=C6AAC87C99A4223504F6B7A79C628120&w={0}'
  7. #Windows API
  8. $code=@"
  9.     using System;
  10.     using System.Runtime.InteropServices;
  11.     public static class GetApi{
  12.         [DllImport("user32.dll")]
  13.         public static extern bool SetWindowPos(IntPtr hWnd,IntPtr hWnd0,uint x,uint y,uint cx,uint cy,uint flag); //声明 Windows API 函数
  14.         [DllImport("user32.dll")]
  15.         private static extern bool ShowWindow(IntPtr hWnd,uint showType); //声明 Windows API 函数
  16.         [DllImport("kernel32.dll")]
  17.         private static extern IntPtr GetConsoleWindow(); //声明 Windows API 函数
  18. public static bool ShowConsoleWindow(uint showType){
  19. return ShowWindow(GetConsoleWindow(),showType);
  20. }
  21.     }
  22. "@
  23. Add-Type -TypeDefinition $code
  24. #隐藏控制台窗口
  25. [void][GetApi]::ShowConsoleWindow(0)
  26. #------------------------界面区-------------------------------------
  27. #0.设置主窗口尺寸
  28. $mainFormWidth = 400                 #主窗口宽度
  29. $mainFormHeight = 300                #主窗口高度
  30. #1.创建主窗口
  31. $form_main = New-Object "System.Windows.Forms.Form"
  32. $form_main.Width = $mainFormWidth                                                   #设置主窗口宽度
  33. $form_main.Height = $mainFormHeight                                                 #设置主窗口高度
  34. $form_main.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen   #主窗口居中屏幕
  35. $form_main.Text = "在线英汉词典"                                                    #主窗口标题
  36. #主窗口置顶
  37. [GetApi]::SetWindowPos($form_main.Handle,-1,$form_main.Location.X,$form_main.Location.Y,$form_main.Width,$form_main.Height,64)
  38. #2.创建输入框
  39. $tb_input = New-Object 'System.Windows.Forms.TextBox'
  40. $tb_input.Height = $form_main.Height / 5
  41. $tb_input.Width = $form_main.Width
  42. $tb_input.BackColor = '#012456'
  43. $tb_input.ForeColor = 'yellow'
  44. $form_main.Controls.Add($tb_input)
  45. #创建显示框
  46. $rtb_show = New-Object 'System.Windows.Forms.RichTextBox'
  47. $rtb_show.Height = $form_main.Height - $tb_input.Height
  48. $rtb_show.Width = $form_main.Width
  49. $rtb_show.Location = New-Object 'System.Drawing.Point' 0,$tb_input.Height
  50. $rtb_show.BackColor = '#012456'
  51. $rtb_show.ForeColor = '#F5F5F5'
  52. $rtb_show.ReadOnly = $true
  53. $form_main.Controls.Add($rtb_show)
  54. #自定义方法
  55. function Print-Host($obj){ Write-Host ($obj | Out-String) }
  56. $js = New-Object 'System.Web.Script.Serialization.JavaScriptSerializer'
  57. $whr = New-Object -ComObject 'WinHttp.WinHttpRequest.5.1'
  58. #------------------------事件区-------------------------------------
  59. $tb_input.add_TextChanged({
  60.     cls
  61.     $whr.Open('GET',($url -f $tb_input.Text),$false)
  62.     $errMsg = '';
  63.     try{$whr.Send()}catch{$errMsg = $_.Exception.Message}
  64.     if($whr.Status -ne 200){
  65.         [System.Windows.Forms.MessageBox]::Show(('错误代码:{0} {1} {2}' -f $whr.Status,$whr.StatusText,$errMsg),'网络连接失败')
  66.         return
  67.     }
  68.     $str = [System.Text.RegularExpressions.Regex]::Unescape($whr.ResponseText);
  69.     $means = [System.Text.RegularExpressions.Regex]::Matches($str,'"means":\[".*?"\]}')
  70.     if($means.Count -eq 0){ $means = [regex]::Matches($str,'"word_mean":".*?"') }
  71.     $str = ''
  72.     for($i = 0;$i -lt $means.Count;$i++){
  73.         $str += '' + ($i + 1) + ":"
  74.         $parts = ($means[$i].Value -replace '^.*:\["|"\]}$|word_mean|:','' -replace ',',',') -split '","'
  75.         for($j = 0;$j -lt $parts.Count;$j++){ $str += "`t" + ($parts[$j] -replace '"','') + "`r`n" }
  76.         $str += "`r`n"
  77.     }
  78.     Print-Host $str
  79.     $rtb_show.Text = $str
  80. })
  81. $form_main.add_SizeChanged({
  82.     $tb_input.Width = $form_main.Width
  83.     $rtb_show.Height = $form_main.Height - $tb_input.Height
  84.     $rtb_show.Width = $form_main.Width
  85. })
  86. $form_main.add_Load({$tb_input.Focus()})
  87. #------------------------结  束-------------------------------------
  88. #4.显示主窗口
  89. $form_main.ShowDialog()
复制代码





欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2