使用Powershell创建一个带GUI程序:英语阅读翻译器
#Codes
powershell -sta
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
#构建主窗口
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "English tool"
$objForm.Size = New-Object System.Drawing.Size(800,600)
$objForm.StartPosition = "CenterScreen"
#设置边界类型为FixedSingle,不允许用户拖动边框来改变窗口大小.
$objForm.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle
#不允许窗口最大化.
$objForm.MaximizeBox = $false
#tabControl
$tabControl = New-Object System.Windows.Forms.TabControl
$tabControl.Location = New-Object System.Drawing.Point(0, 10);
$tabControl.SelectedIndex = 0;
$tabControl.Size = New-Object System.Drawing.Size(800, 590);
$tabControl.TabIndex = 0;
$tabPage1 = New-Object System.Windows.Forms.TabPage
$tabPage2 = New-Object System.Windows.Forms.TabPage
$tabPage3 = New-Object System.Windows.Forms.TabPage
#tabPage1
$tabPage1.Location = New-Object System.Drawing.Point(4, 22);
$tabPage1.Padding = New-Object System.Windows.Forms.Padding(3);
$tabPage1.Size = New-Object System.Drawing.Size(688, 401);
$tabPage1.TabIndex = 0;
$tabPage1.Text = "Translator";
$tabPage1.UseVisualStyleBackColor = "true";
#tabPage2
$tabPage2.Location = New-Object System.Drawing.Point(4, 22);
$tabPage2.Padding = New-Object System.Windows.Forms.Padding(3);
$tabPage2.Size = New-Object System.Drawing.Size(688, 401);
$tabPage2.TabIndex = 1;
$tabPage2.Text = "Reader";
$tabPage2.UseVisualStyleBackColor = "true";
#tabPage3
#
$tabPage3.Location = New-Object System.Drawing.Point(4, 22);
$tabPage3.Name = "tabPage3";
$tabPage3.Padding = New-Object System.Windows.Forms.Padding(3);
$tabPage3.Size = New-Object System.Drawing.Size(688, 401);
$tabPage3.TabIndex = 2;
$tabPage3.Text = "WebBrowser";
$tabPage3.UseVisualStyleBackColor = "true";
#
###############################################################################################
############################################################tabPage1###########################
#设置一些前提参数
function wget([string]$url)
{
$client = new-object System.Net.WebClient
$client.Encoding = [System.Text.Encoding]::GetEncoding("utf-8")
return $client.DownloadString($url)
}
$url = "http://dict.youdao.com/search?tab=chn&keyfrom=dict.top&q="
$SourcePath = "E:\EnglishWords.txt"
$SavingPath = $SourcePath.Replace(".txt","") + "-Translate.txt"
function ReplaceYinbiao([string]$StringYinbiao)
{
$StringYinbiao = $StringYinbiao.Replace("ə","ə")
$StringYinbiao = $StringYinbiao.Replace("ɔ","ɔ")
$StringYinbiao = $StringYinbiao.Replace("ʌ","ʌ")
$StringYinbiao = $StringYinbiao.Replace("æ","æ")
$StringYinbiao = $StringYinbiao.Replace("ɑ","ɑ")
$StringYinbiao = $StringYinbiao.Replace("ɛ","ɛ")
$StringYinbiao = $StringYinbiao.Replace("θ","θ")
$StringYinbiao = $StringYinbiao.Replace("ŋ","ŋ")
$StringYinbiao = $StringYinbiao.Replace("ʃ","ʃ")
$StringYinbiao = $StringYinbiao.Replace("ð","ð")
$StringYinbiao = $StringYinbiao.Replace("ʒ","ʒ")
$StringYinbiao = $StringYinbiao.Replace("ʊ","ʊ")
return $StringYinbiao
}
#设置提示文本.
$objLabel1 = New-Object System.Windows.Forms.Label
$objLabel1.Location = New-Object System.Drawing.Size(10,20)
$objLabel1.Size = New-Object System.Drawing.Size(280,20)
$objLabel1.Text = "Here is the translation results:"
$tabPage1.Controls.Add($objLabel1)
#创建文本框,作为文字粘贴的窗口.
$objTextBox1 = New-Object System.Windows.Forms.RichTextBox
$objTextBox1.Location = New-Object System.Drawing.Size(20,40)
$objTextBox1.Size = New-Object System.Drawing.Size(750,450)
$objTextBox1.text = ""
$tabPage1.Controls.Add($objTextBox1)
#提示源文件路径
$SourceTextlabel = New-Object System.Windows.Forms.Label
$SourceTextlabel.Location = New-Object System.Drawing.Size(55,505)
$SourceTextlabel.Size = New-Object System.Drawing.Size(55,28)
$SourceTextlabel.Text = "File Path:"
$tabPage1.Controls.Add($SourceTextlabel)
#选择源文件
$SourceText = New-Object System.Windows.Forms.TextBox
$SourceText.Location = New-Object System.Drawing.Size(120,505)
$SourceText.Size = New-Object System.Drawing.Size(230,28)
$SourceText.Text = $SourcePath
$tabPage1.Controls.Add($SourceText)
#提示源文件是否存在
$SourceTextExistlabel = New-Object System.Windows.Forms.Label
$SourceTextExistlabel.Location = New-Object System.Drawing.Size(120,525)
$SourceTextExistlabel.Size = New-Object System.Drawing.Size(135,28)
$SourceTextExistlabel.Text = ""
$SourceTextExistlabel.ForeColor = "Red"
$tabPage1.Controls.Add($SourceTextExistlabel)
#打开文件对话框
$OpenFileDlg = New-Object -TypeName System.Windows.Forms.OpenFileDialog;
$OpenFileDlg.CheckFileExists = $true;
$OpenFileDlg.RestoreDirectory = $true;
$OpenFileDlg.SupportMultiDottedExtensions = $true;
$OpenFileDlg.ShowHelp = $true;
$OpenFileDlg.Filter = "Text Files(*.txt)|*.txt";
$OpenFileDlg.FilterIndex = 2;
$OpenFileDlg.title = "Open text file";
#BrowserEvent
$BrowserEvent = {
$IsOK = $OpenFileDlg.ShowDialog();
if($IsOK -eq "OK")
{
$SourceText.Text = $OpenFileDlg.FileName
}
}
#创建浏览按钮
$BrowserButton = New-Object System.Windows.Forms.Button
$BrowserButton.Location = New-Object System.Drawing.Size(360,505)
$BrowserButton.Size = New-Object System.Drawing.Size(60,25)
$BrowserButton.Text = "Browse"
$BrowserButton.Add_Click($BrowserEvent)
$tabPage1.Controls.Add($BrowserButton)
#点击开始的事件
$TranslateEvent = {
$objTextBox1.text = ""
$isExist = Test-Path $SourceText.Text
if($isExist -eq "True")
{
$SourceTextExistlabel.Text = ""
$Contents = Get-Content -Path $SourceText.Text
$StartButton.Enabled = "false"
foreach($ContentItem in $Contents)
{
$result = wget($url + $ContentItem)
$Yinbiao = ""
$bYinbiao = $result -match ">.*]"
if($bYinbiao -eq "True")
{
$Yinbiao = "" + $matches.values
$Yinbiao = $Yinbiao.Substring(1,($Yinbiao.Length - 1))
$Yinbiao = ReplaceYinbiao($Yinbiao)
}
$index = 0
$regexPattern = "attributem1web.*</td"
$Fanyi = ""
$regex = New-Object System.Text.RegularExpressions.Regex $regexPattern
while($index -lt $result.Length)
{
$match = $regex.Match($result, $index)
if($match.Success -and $match.Length -gt 0)
{
$Fanyi += $match.Value.ToString() + "`t"
$index = $match.Index + $match.Length
}
else
{
$index = $result.Length
}
}
$Fanyi = $Fanyi.Replace("attributem1web`">","").Replace("</td","").Replace("<font class=graynolinktext>","").Replace(" ","").Replace("</font>","").Replace("`;`;"," ")
$objTextBox1.text += $ContentItem + "`t" + $Yinbiao + "`t" + $Fanyi + "`n"
}
$StartButton.Enabled = "true"
}
else {$SourceTextExistlabel.Text = "Please select a valid file"}
}
#创建开始按钮
$StartButton = New-Object System.Windows.Forms.Button
$StartButton.Location = New-Object System.Drawing.Size(600,505)
$StartButton.Size = New-Object System.Drawing.Size(120,30)
$StartButton.Text = "Translate"
$StartButton.Add_Click($TranslateEvent)
$tabPage1.Controls.Add($StartButton)
###############################################################################################
#################################################tabPage2######################################
#设置提示文本.
$objLabel2 = New-Object System.Windows.Forms.Label
$objLabel2.Location = New-Object System.Drawing.Size(10,20)
$objLabel2.Size = New-Object System.Drawing.Size(280,20)
$objLabel2.Text = "Please copy your English article here:"
$tabPage2.Controls.Add($objLabel)
#创建文本框,作为文字粘贴的窗口.
$objTextBox = New-Object System.Windows.Forms.RichTextBox
$objTextBox.Location = New-Object System.Drawing.Size(20,40)
$objTextBox.Size = New-Object System.Drawing.Size(750,450)
$objTextBox.text = "Copy your article here and click on start button."
$tabPage2.Controls.Add($objTextBox)
#创建 SAPI.SpVoice对象.
$Voice = new-object -com SAPI.SpVoice;
#设置默认阅读速度
$Voice.Rate = -1
#选择速度
$SpeedLabel = New-Object System.Windows.Forms.Label
$SpeedLabel.Location = New-Object System.Drawing.Size(150,510)
$SpeedLabel.Size = New-Object System.Drawing.Size(135,28)
$SpeedLabel.Text = "Select a suitable speed:"
$tabPage2.Controls.Add($SpeedLabel)
#创建单选按钮
$RadioButton = New-Object Windows.Forms.radiobutton
$RadioButton.Location = New-Object System.Drawing.Size(300,498)
$RadioButton.Size = New-Object System.Drawing.Size(100,15)
$RadioButton.Text = "Fast"
$tabPage2.Controls.Add($RadioButton)
#创建单选按钮1
$RadioButton1 = New-Object Windows.Forms.radiobutton
$RadioButton1.Location = New-Object System.Drawing.Size(300,512)
$RadioButton1.Size = New-Object System.Drawing.Size(100,15)
$RadioButton1.Text = "medium"
$tabPage2.Controls.Add($RadioButton1)
#创建单选按钮2
$RadioButton2 = New-Object Windows.Forms.radiobutton
$RadioButton2.Location = New-Object System.Drawing.Size(300,528)
$RadioButton2.Size = New-Object System.Drawing.Size(100,15)
$RadioButton2.Text = "Slow"
$tabPage2.Controls.Add($RadioButton2)
#设置默认的单选按钮:中速
$Radiobutton1.checked = "true"
#为单选按钮创建事件
$event = {
if($Radiobutton.checked){$Voice.Rate = 1}
if($Radiobutton1.checked){$Voice.Rate = 0}
if($Radiobutton2.checked){$Voice.Rate = -1}
$Voice.Speak($objTextBox.text, 1)
}
#创建暂停按钮
$PauseButton = New-Object System.Windows.Forms.Button
$PauseButton.Location = New-Object System.Drawing.Size(400,505)
$PauseButton.Size = New-Object System.Drawing.Size(100,30)
$PauseButton.Text = "Pause"
$PauseButton.Add_Click({$Voice.Pause()} )
$tabPage2.Controls.Add($PauseButton)
#创建继续按钮
$ResumeButton = New-Object System.Windows.Forms.Button
$ResumeButton.Location = New-Object System.Drawing.Size(500,505)
$ResumeButton.Size = New-Object System.Drawing.Size(100,30)
$ResumeButton.Text = "Continue"
$ResumeButton.Add_Click({$Voice.Resume()})
$tabPage2.Controls.Add($ResumeButton)
#创建开始按钮
$SpeechButton = New-Object System.Windows.Forms.Button
$SpeechButton.Location = New-Object System.Drawing.Size(600,505)
$SpeechButton.Size = New-Object System.Drawing.Size(120,30)
$SpeechButton.Text = "Start from scratch"
$SpeechButton.Add_Click($event)
$tabPage2.Controls.Add($SpeechButton)
###############################################################################################
##############################################tabPage3#########################################
$webBrowser1 = New-Object System.Windows.Forms.webBrowser
$webBrowser1.Location = New-Object System.Drawing.Point(3, 3);
$webBrowser1.MinimumSize = New-Object System.Drawing.Size(20, 20);
$webBrowser1.Size = New-Object System.Drawing.Size(780, 530);
$webBrowser1.TabIndex = 2;
$webBrowser1.Navigate("http://www.youdao.com/");
$tabPage3.Controls.Add($webBrowser1);
###############################################################################################
###############################################################################################
#Add some controls to Form
$objForm.Controls.Add($tabControl)
$tabControl.Controls.Add($tabPage1);
$tabControl.Controls.Add($tabPage2);
$tabControl.Controls.Add($tabPage3);
#显示GUI对话框
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()