| |
| cls |
| |
| [void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") |
| [void][System.Reflection.Assembly]::LoadWithPartialName("System.Web.Extensions") |
| $url = 'http://dict-co.iciba.com/api/dictionary.php?type=json&key=C6AAC87C99A4223504F6B7A79C628120&w={0}' |
| |
| $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) |
| |
| |
| |
| $mainFormWidth = 400 |
| $mainFormHeight = 300 |
| |
| |
| $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) |
| |
| |
| $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()}) |
| |
| |
| $form_main.ShowDialog()COPY |