标题: [原创代码] powershell便携英汉词典 [打印本页]
作者: went 时间: 2021-5-16 13:56 标题: powershell便携英汉词典
保存为bat或ps1文件- #&cls&@powershell -c "Get-Content '%~0' | Select-Object -Skip 1 | Out-String | Invoke-Expression" & exit
- cls
- #------------------------初始化区------------------------------------
- [void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") #加载WinForm库
- [void][System.Reflection.Assembly]::LoadWithPartialName("System.Web.Extensions")
- $url = 'http://dict-co.iciba.com/api/dictionary.php?type=json&key=C6AAC87C99A4223504F6B7A79C628120&w={0}'
- #Windows API
- $code=@"
- using System;
- using System.Runtime.InteropServices;
- public static class GetApi{
- [DllImport("user32.dll")]
- public static extern bool SetWindowPos(IntPtr hWnd,IntPtr hWnd0,uint x,uint y,uint cx,uint cy,uint flag); //声明 Windows API 函数
- [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
- #隐藏控制台窗口
- [void][GetApi]::ShowConsoleWindow(0)
-
- #------------------------界面区-------------------------------------
- #0.设置主窗口尺寸
- $mainFormWidth = 400 #主窗口宽度
- $mainFormHeight = 300 #主窗口高度
-
- #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 = "在线英汉词典" #主窗口标题
- #主窗口置顶
- [GetApi]::SetWindowPos($form_main.Handle,-1,$form_main.Location.X,$form_main.Location.Y,$form_main.Width,$form_main.Height,64)
-
- #2.创建输入框
- $tb_input = New-Object 'System.Windows.Forms.TextBox'
- $tb_input.Height = $form_main.Height / 5
- $tb_input.Width = $form_main.Width
- $tb_input.BackColor = '#012456'
- $tb_input.ForeColor = 'yellow'
- $form_main.Controls.Add($tb_input)
-
- #创建显示框
- $rtb_show = New-Object 'System.Windows.Forms.RichTextBox'
- $rtb_show.Height = $form_main.Height - $tb_input.Height
- $rtb_show.Width = $form_main.Width
- $rtb_show.Location = New-Object 'System.Drawing.Point' 0,$tb_input.Height
- $rtb_show.BackColor = '#012456'
- $rtb_show.ForeColor = '#F5F5F5'
- $rtb_show.ReadOnly = $true
- $form_main.Controls.Add($rtb_show)
-
- #自定义方法
- function Print-Host($obj){ Write-Host ($obj | Out-String) }
- $js = New-Object 'System.Web.Script.Serialization.JavaScriptSerializer'
- $whr = New-Object -ComObject 'WinHttp.WinHttpRequest.5.1'
- #------------------------事件区-------------------------------------
- $tb_input.add_TextChanged({
- cls
- $whr.Open('GET',($url -f $tb_input.Text),$false)
- $errMsg = '';
- try{$whr.Send()}catch{$errMsg = $_.Exception.Message}
- if($whr.Status -ne 200){
- [System.Windows.Forms.MessageBox]::Show(('错误代码:{0} {1} {2}' -f $whr.Status,$whr.StatusText,$errMsg),'网络连接失败')
- return
- }
- $str = [System.Text.RegularExpressions.Regex]::Unescape($whr.ResponseText);
- $means = [System.Text.RegularExpressions.Regex]::Matches($str,'"means":\[".*?"\]}')
- if($means.Count -eq 0){ $means = [regex]::Matches($str,'"word_mean":".*?"') }
- $str = ''
- for($i = 0;$i -lt $means.Count;$i++){
- $str += '' + ($i + 1) + ":"
- $parts = ($means[$i].Value -replace '^.*:\["|"\]}$|word_mean|:','' -replace ',',',') -split '","'
- for($j = 0;$j -lt $parts.Count;$j++){ $str += "`t" + ($parts[$j] -replace '"','') + "`r`n" }
- $str += "`r`n"
- }
- Print-Host $str
- $rtb_show.Text = $str
- })
- $form_main.add_SizeChanged({
- $tb_input.Width = $form_main.Width
- $rtb_show.Height = $form_main.Height - $tb_input.Height
- $rtb_show.Width = $form_main.Width
- })
- $form_main.add_Load({$tb_input.Focus()})
- #------------------------结 束-------------------------------------
- #4.显示主窗口
- $form_main.ShowDialog()
复制代码
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |