| |
| cls |
| |
| [void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") |
| [void][System.Reflection.Assembly]::LoadWithPartialName("System.Runtime.Serialization") |
| [void][System.Reflection.Assembly]::LoadWithPartialName("PresentationFramework") |
| $fav = 'MyFavorites.txt' |
| |
| $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) |
| |
| $transURL = 'http://dict-co.iciba.com/api/dictionary.php?type=xml&key=C6AAC87C99A4223504F6B7A79C628120&w={0}' |
| |
| |
| |
| $mainFormWidth = 500 |
| $mainFormHeight = 400 |
| |
| |
| $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 = "在线英汉词典 (Ctrl+S)" |
| |
| [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 = 20 |
| $tb_input.Width = $form_main.Width |
| $tb_input.BackColor = '#012456' |
| $tb_input.ForeColor = 'yellow' |
| $form_main.Controls.Add($tb_input) |
| |
| |
| $lable_sbol_1 = New-Object 'System.Windows.Forms.Label' |
| $lable_sbol_1.Location = New-Object 'System.Drawing.Point' 0,$tb_input.Height |
| $lable_sbol_1.ForeColor = '#00C853' |
| $lable_sbol_1.BackColor = '#012456' |
| $lable_sbol_1.Height = 20 |
| $lable_sbol_1.Width = $form_main.Width |
| $lable_sbol_1.Cursor = [System.Windows.Forms.Cursors]::Hand |
| $form_main.Controls.Add($lable_sbol_1) |
| |
| |
| $lable_sbol_2 = New-Object 'System.Windows.Forms.Label' |
| $lable_sbol_2.Location = New-Object 'System.Drawing.Point' 0,($tb_input.Height + $lable_sbol_1.Height) |
| $lable_sbol_2.ForeColor = '#00C853' |
| $lable_sbol_2.BackColor = '#012456' |
| $lable_sbol_2.Height = 20 |
| $lable_sbol_2.Width = $form_main.Width |
| $lable_sbol_2.Cursor = [System.Windows.Forms.Cursors]::Hand |
| $form_main.Controls.Add($lable_sbol_2) |
| |
| |
| $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.ForeColor = '#F5F5F5' |
| $rtb_show.BackColor = '#012456' |
| $rtb_show.ReadOnly = $true |
| $rtb_show.BorderStyle = [System.Windows.Forms.BorderStyle]::None |
| $form_main.Controls.Add($rtb_show) |
| |
| $whr = New-Object -ComObject 'WinHttp.WinHttpRequest.5.1' |
| $xmlDoc = New-Object 'System.Xml.XmlDocument' |
| $player = New-Object 'System.Windows.Media.MediaPlayer' |
| $Script:query = $false |
| $endl = "`r`n" |
| |
| function Print-Host($obj){ Write-Host ($obj | Out-String) } |
| |
| $tb_input.add_KeyDown({ |
| cls |
| [System.Windows.Forms.KeyEventArgs]$e = $_ |
| if($e.KeyCode -eq [System.Windows.Forms.Keys]::S -and $e.Control){ |
| if(!$Script:query){[System.Windows.Forms.MessageBox]::Show('请先查询数据后再收藏!','提示: ' + $fav);return} |
| $str = $tb_input.Text.Trim() + $endl + '----' + $endl |
| if([System.IO.File]::Exists($fav) -and (Get-Content $fav | Out-String) -match $str){ |
| [System.Windows.Forms.MessageBox]::Show($str,'提示: 该词条已被收藏过' + $fav) |
| return |
| } |
| $s1 = $lable_sbol_1.Text |
| $s2 = $lable_sbol_2.Text |
| if($s1 -match '\S'){$str += $s1 + $endl} |
| if($s2 -match '\S'){$str += $s2 + $endl} |
| $arr = $rtb_show.Text -split '--------------------------------------------' |
| $str += $arr[1] + $endl + $arr[2] |
| $str = $str -replace '(?m)^\s+$','' |
| $str += '***************************************************************' |
| Write-Host ('保存:' + $endl + $str) |
| $str | Out-File $fav -Encoding utf8 -Append |
| [System.Windows.Forms.MessageBox]::Show($str,'提示: 收藏成功! ' + $fav) |
| } |
| if($e.KeyCode -ne [System.Windows.Forms.Keys]::Enter){return} |
| $whr.Open('GET',($transURL -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; |
| } |
| $xmlDoc.LoadXml([System.Text.UTF8Encoding]::UTF8.GetString($whr.ResponseBody)) |
| $lable_sbol_1.Text = $lable_sbol_2.Text = '' |
| $lable_sbol_1.Visible = $false |
| $lable_sbol_2.Visible = $false |
| $rtb_show.Location = New-Object 'System.Drawing.Point' 0,$tb_input.Height |
| if ($xmlDoc.dict.ps -is [string]){ |
| $lable_sbol_1.Visible = $true |
| $lable_sbol_1.Text = ' 英[{0}]' -f $xmlDoc.dict.ps |
| $lable_sbol_1.Tag = $xmlDoc.dict.pron |
| $rtb_show.Location = New-Object 'System.Drawing.Point' 0,($tb_input.Height + $lable_sbol_1.Height) |
| } elseif ($xmlDoc.dict.ps -is [array]){ |
| $lable_sbol_1.Visible = $true |
| $lable_sbol_2.Visible = $true |
| $lable_sbol_1.Text = ' 英[{0}]' -f $xmlDoc.dict.ps[0] |
| $lable_sbol_1.Tag = $xmlDoc.dict.pron[0] |
| $lable_sbol_2.Text = ' 美[{0}]' -f $xmlDoc.dict.ps[1] |
| $lable_sbol_2.Tag = $xmlDoc.dict.pron[1] |
| $rtb_show.Location = New-Object 'System.Drawing.Point' 0,($tb_input.Height + $lable_sbol_1.Height + $lable_sbol_2.Height) |
| } |
| $str = '--------------------------------------------' + $endl |
| if($xmlDoc.dict.pos -is [string]){ |
| $str += $xmlDoc.dict.pos + $xmlDoc.dict.acceptation |
| } elseif ($xmlDoc.dict.pos -is [array]){ |
| for($i=0;$i -lt $xmlDoc.dict.pos.Count;$i++){ |
| $str += '{0}{1}' -f $xmlDoc.dict.pos[$i],$xmlDoc.dict.acceptation[$i] |
| } |
| } |
| $str += $endl + '--------------------------------------------' |
| if(($str -replace '\s|;|;|-','') -eq ''){$str = ''} |
| for($i=0;$i -lt $xmlDoc.dict.sent.Count;$i++){ |
| $str += $xmlDoc.dict.sent[$i].orig |
| $str += $xmlDoc.dict.sent[$i].trans |
| $str += '--------------------------------------------' |
| } |
| |
| $rtb_show.Text = $str |
| $Script:query = $true |
| }) |
| $form_main.add_SizeChanged({ |
| $tb_input.Width = $rtb_show.Width = $lable_sbol_1.Width = $lable_sbol_2.Width = $form_main.Width |
| $rtb_show.Height = $form_main.Height - $tb_input.Height |
| }) |
| |
| $lable_sbol_1.add_Click({ |
| Print-Host ('英式发音: ' + $this.Tag) |
| $player.Stop() |
| $player.Open($this.Tag) |
| $player.Play() |
| }) |
| $lable_sbol_2.add_Click({ |
| Print-Host ('美式发音: ' + $this.Tag) |
| $player.Stop() |
| $player.Open($this.Tag) |
| $player.Play() |
| }) |
| |
| |
| $form_main.add_Load({$tb_input.Focus()}) |
| $form_main.ShowDialog()COPY |