保存bat或ps1- #&cls&cd /d "%~dp0" & @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.Runtime.Serialization")
- [void][System.Reflection.Assembly]::LoadWithPartialName("PresentationFramework")
- $fav = 'MyFavorites.txt' #收藏单词输出文件
- #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)
-
- $transURL = 'http://dict-co.iciba.com/api/dictionary.php?type=xml&key=C6AAC87C99A4223504F6B7A79C628120&w={0}'
-
- #------------------------界面区-------------------------------------
- #0.设置主窗口尺寸
- $mainFormWidth = 500 #主窗口宽度
- $mainFormHeight = 400 #主窗口高度
-
- #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 = "在线英汉词典 (Ctrl+S)" #主窗口标题
- #主窗口置顶
- [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 = 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 += '--------------------------------------------'
- }
- #Print-Host -obj $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()
- })
- #------------------------结 束-------------------------------------
- #4.显示主窗口
- $form_main.add_Load({$tb_input.Focus()})
- $form_main.ShowDialog()
复制代码
|