1 Star 0 Fork 0

jaythin/Google Translate

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
MainForm.cs 16.63 KB
一键复制 编辑 原始数据 按行查看 历史
cn.su 提交于 2013-09-05 15:20 . Merge requests, auto check the language.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;
using Newtonsoft.Json;
using DotNetSpeech;
namespace Translate
{
public partial class MainForm : Form
{
#region Fields
/// <summary>
/// HotKey
/// </summary>
private HotKeyClass _regisKey = new HotKeyClass();
/// <summary>
/// spFlags
/// </summary>
private SpeechVoiceSpeakFlags spFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
/// <summary>
/// sp
/// </summary>
private SpVoice sp = new SpVoice();
/// <summary>
/// notifyicon
/// </summary>
private NotifyIcon notifyicon = new NotifyIcon();
/// <summary>
/// Icon
/// </summary>
private Icon ico = Properties.Resources.translate;
/// <summary>
/// notifyContextMenu
/// </summary>
private ContextMenu notifyContextMenu = new ContextMenu();
/// <summary>
/// isExit
/// </summary>
private bool isExit = false;
/// <summary>
/// request
/// </summary>
private TranslateRequest request;
/// <summary>
/// isRequest
/// </summary>
private bool isRequest = false;
#endregion
#region Constructor
public MainForm(string[] args)
{
InitializeComponent();
cmbSource.DisplayMember = "LanguageName";
cmbSource.ValueMember = "LanguageKey";
cmbTarge.DisplayMember = "LanguageName";
cmbTarge.ValueMember = "LanguageKey";
cmbSource.DataSource = GetLanguageList();
cmbTarge.DataSource = GetLanguageList();
cmbSource.SelectedValue = "en";
cmbTarge.SelectedValue = "zh-CN";
chkAuto_CheckedChanged(null, null);
translatorNotifyIcon.Icon = ico;
this.ShowInTaskbar = false;
notifyicon.Visible = true;
this.Hide();
if (args.Length > 0 && args[0]=="ONSTART")
{
WindowState = FormWindowState.Minimized;
}
}
#endregion
#region Methods
/// <summary>
/// btnSubmit_Click
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSubmit_Click(object sender, EventArgs e)
{
StartTranslate();
}
/// <summary>
/// StartTranslate
/// </summary>
private void StartTranslate()
{
string source = txtSource.Text;
string languageSource = cmbSource.SelectedValue.ToString();
string languageTarge = cmbTarge.SelectedValue.ToString();
txtResult.Clear();
if (!string.IsNullOrEmpty(source))
{
txtResult.Text = "Translation...";
if (IsChinese(source))
{
string oldSource = string.Empty;
if (languageSource != "zh-CN")
{
oldSource = cmbSource.SelectedValue.ToString();
cmbSource.SelectedValue = "zh-CN";
languageSource = "zh-CN";
}
if (languageTarge == "zh-CN")
{
if (string.IsNullOrEmpty(oldSource))
{
cmbTarge.SelectedValue = "en";
}
else
{
cmbTarge.SelectedValue = oldSource;
}
languageTarge = cmbTarge.SelectedValue.ToString();
}
}
request = new TranslateRequest(source, languageSource, languageTarge);
if (!isRequest)
{
TranslateExecute(request.Text, request.Source, request.Targe);
}
}
}
/// <summary>
/// TranslateExecute
/// </summary>
private void TranslateExecute(string source, string languageSource, string languageTarge)
{
Action action = new Action(() =>
{
string result = GoogleTranslate.Execute(source, languageSource, languageTarge);
SetResultText(result);
isRequest = false;
if (request != null)
{
TranslateExecute(request.Text, request.Source, request.Targe);
}
});
request = null;
isRequest = true;
action.BeginInvoke(null, null);
}
/// <summary>
/// IsChinese
/// </summary>
/// <param name="CString"></param>
/// <returns></returns>
public bool IsChinese(string CString)
{
bool BoolValue = false;
for (int i = 0; i < CString.Length; i++)
{
if (Convert.ToInt32(Convert.ToChar(CString.Substring(i, 1))) < Convert.ToInt32(Convert.ToChar(128)))
{
BoolValue = false;
}
else
{
return BoolValue = true;
}
}
return BoolValue;
}
/// <summary>
/// SetResultText
/// </summary>
/// <param name="value"></param>
private void SetResultText(string value)
{
this.BeginInvoke(new Action(() =>
{
txtResult.Text = value;
}), null);
}
/// <summary>
/// rdAuto_CheckedChanged
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void chkAuto_CheckedChanged(object sender, EventArgs e)
{
if (chkAuto.Checked)
{
txtSource.TextChanged += txtSource_TextChanged;
}
else
{
txtSource.TextChanged -= txtSource_TextChanged;
}
}
/// <summary>
/// txtSource_TextChanged
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txtSource_TextChanged(object sender, EventArgs e)
{
StartTranslate();
}
/// <summary>
/// translatorNotifyIcon_DoubleClick
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void translatorNotifyIcon_DoubleClick(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
this.Show();
WindowState = FormWindowState.Normal;
this.Activate();
}
else
{
WindowState = FormWindowState.Minimized;
this.Hide();
}
}
/// <summary>
/// MainForm_SizeChanged
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MainForm_SizeChanged(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
this.Hide();
}
}
/// <summary>
/// GetLanguageList
/// </summary>
/// <returns></returns>
private List<Language> GetLanguageList()
{
Hashtable array = new Hashtable();
array.Add ("en", "English");
            array.Add ("zh-CN", "Chinese");
#region Hashtable
array.Add ("sq", "Albanian");
            array.Add ("ar", "Arabic");
            array.Add ("ga", "Irish");
            array.Add ("et", "Estonian");
            array.Add ("be", "Belarusian");
            array.Add ("bg", "Bulgarian");
            array.Add ("is", "Icelandic");
            array.Add ("pl", "Polish");
            array.Add ("fa", "Persian");
            array.Add ("af", "Boolean text (Afrikaans)");
            array.Add ("da", "Danish");
            array.Add ("de", "German");
            array.Add ("ru", "Russian");
            array.Add ("fr", "French");
            array.Add ("tl", "Filipino");
            array.Add ("fi", "Finnish");
            array.Add ("ko", "Korean");
            array.Add ("nl", "Dutch");
            array.Add ("gl", "Galician");
            array.Add ("ca", "Catalan");
            array.Add ("cs", "Czech");
            array.Add ("hr", "Croatian");
            array.Add ("lv", "Latvian");
            array.Add ("lt", "Lithuanian");
            array.Add ("ro", "Romanian");
            array.Add ("mt", "Maltese");
            array.Add ("ms", "Malay");
            array.Add ("mk", "Macedonian");
            array.Add ("no", "Norwegian");
            array.Add ("pt", "Portuguese");
            array.Add ("ja", "Japanese");
            array.Add ("sv", "Swedish");
            array.Add ("sr", "Serbian");
            array.Add ("sk", "Slovak");
            array.Add ("sl", "Slovenian");
            array.Add ("sw", "Swahili");
            array.Add ("th", "Thai");
            array.Add ("tr", "Turkish");
            array.Add ("cy", "Welsh");
            array.Add ("uk", "Ukrainian");
            array.Add ("es", "Spanish");
            array.Add ("iw", "Hebrew");
            array.Add ("el", "Greek");
            array.Add ("hu", "Hungarian");
            array.Add ("it", "Italian");
            array.Add ("yi", "Yiddish");
            array.Add ("hi", "Hindi");
            array.Add ("id", "Indonesian");
            array.Add ("vi", "Vietnamese");
#endregion
List<Language> languages = new List<Language>();
foreach (DictionaryEntry element in array)
{
languages.Add(new Language(element.Key.ToString(), element.Value.ToString()));
}
return languages;
}
/// <summary>
/// btnSwitch_Click
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSwitch_Click(object sender, EventArgs e)
{
string languageSource = cmbSource.SelectedValue.ToString();
string languageTarge = cmbTarge.SelectedValue.ToString();
cmbSource.SelectedValue = languageTarge;
cmbTarge.SelectedValue = languageSource;
StartTranslate();
}
/// <summary>
/// UrlEncode
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
private string UrlEncode(string str)
{
return System.Web.HttpUtility.UrlEncode(str, System.Text.Encoding.UTF8);
}
/// <summary>
/// SetAutoRun
/// </summary>
/// <param name="fileName"></param>
/// <param name="isAutoRun"></param>
public static void SetAutoRun(string fileName, bool isAutoRun)
{
RegistryKey reg = null;
try
{
if (!System.IO.File.Exists(fileName))
throw new Exception("The file does not exist!");
String name = "Translate";
reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
if (reg == null)
reg = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
if (isAutoRun)
reg.SetValue(name, fileName + " ONSTART");
else
reg.SetValue(name, false);
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
finally
{
if (reg != null)
reg.Close();
}
}
/// <summary>
/// btnRead_Click
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnRead_Click(object sender, EventArgs e)
{
string result = txtResult.Text;
if (!string.IsNullOrEmpty(result))
{
sp.Rate = 0;
sp.Speak(result, spFlags);
}
}
/// <summary>
/// OnFormClosing
/// </summary>
/// <param name="e"></param>
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (!isExit)
{
WindowState = FormWindowState.Minimized;
e.Cancel = true;
}
}
/// <summary>
/// toolStripMenuItemAutoStart_CheckedChanged
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toolStripMenuItemAutoStart_CheckedChanged(object sender, EventArgs e)
{
SetAutoRun(Application.ExecutablePath, toolStripMenuItemAutoStart.Checked);
}
/// <summary>
/// toolStripMenuItemExit_Click
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toolStripMenuItemExit_Click(object sender, EventArgs e)
{
isExit = true;
Application.Exit();
}
/// <summary>
/// MainForm_Load
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MainForm_Load(object sender, EventArgs e)
{
_regisKey.Keys = Keys.T;
_regisKey.ModKey = HotKeyClass.MODKEY.MOD_CONTROL;
_regisKey.WindowHandle = this.Handle;
_regisKey.HotKey += new HotKeyClass.HotKeyPass(Regis_HotKey);
_regisKey.StarHotKey();
}
/// <summary>
/// Regis_HotKey
/// </summary>
/// <param name="e"></param>
private void Regis_HotKey()
{
if (WindowState == FormWindowState.Minimized)
{
this.Show();
WindowState = FormWindowState.Normal;
this.Activate();
}
else
{
WindowState = FormWindowState.Minimized;
this.Hide();
}
}
#endregion
}
#region LanguageClass
/// <summary>
/// Language
/// </summary>
public class Language
{
/// <summary>
/// Language
/// </summary>
/// <param name="id"></param>
/// <param name="name"></param>
public Language(string id, string name)
{
this.LanguageKey = id;
this.LanguageName = name;
}
/// <summary>
/// LanguageKey
/// </summary>
public string LanguageKey
{
get;
set;
}
/// <summary>
/// Name
/// </summary>
public string LanguageName
{
get;
set;
}
}
#endregion
#region TranslateRequest
public class TranslateRequest
{
/// <summary>
/// TranslateRequest
/// </summary>
public TranslateRequest(string source, string languageSource, string languageTarge)
{
this.Text = source;
this.Source = languageSource;
this.Targe = languageTarge;
}
/// <summary>
/// Text
/// </summary>
public string Text
{
set;
get;
}
/// <summary>
/// Source
/// </summary>
public string Source
{
set;
get;
}
/// <summary>
/// Targe
/// </summary>
public string Targe
{
set;
get;
}
}
#endregion
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/jaythin/Google-Translate.git
git@gitee.com:jaythin/Google-Translate.git
jaythin
Google-Translate
Google Translate
master

搜索帮助