代码拉取完成,页面将自动刷新
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
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。