2020-09-23:
修正了编译脚本时的一个小 Bug。
Base.PressKeys(string keys),更改了转义符的方向(例如:/t:Tab 键)。
Base库新增了两个函数:
Base.IsKeyDown(Keys key):某个键是否按下。
Base.IsKeyToggled(Keys key):某键是否开启(例如:CapsLock)。
可自由引用程序集和命名空间,默认配置文件提供的命名空间有:“System”, “System.Collections”, “System.Collections.Concurrent”, “System.Collections.Generic”, “System.Collections.ObjectModel”, “System.Collections.Specialized”, “System.Diagnostics”, “System.Drawing”, “System.Drawing.Imaging”, “System.Drawing.Text”, “System.IO”, “System.IO.Compression”, “System.IO.Pipes”, “System.IO.Ports”, “System.Linq”, “System.Linq.Expressions”, “System.Media”, “System.Net”, “System.Net.Http”, “System.Net.Http.Headers”, “System.Net.Security”, “System.Net.Sockets”, “System.Reflection”, “System.Security”, “System.Security.Authentication”, “System.Security.Cryptography”, “System.Text”, “System.Text.RegularExpressions”, “System.Threading”, “System.Threading.Tasks”, “System.Timers”, “System.Web”, “System.Windows.Forms”, “System.Xml”, “System.Xml.Linq”, “System.Xml.Schema”, “System.Xml.Serialization”。
这里附赠一个百度翻译的脚本:
Base.Activate();
Base.PressKeys("(c)");
Thread.Sleep(4);
SynchronizationContext context = SynchronizationContext.Current;
string original = Clipboard.GetText().Replace("-\r\n", "").Replace("\r\n", " ");
(new Thread(new ThreadStart(() =>
{
string q = original;
string from = "en";
string to = "zh";
string appId = "";
string salt = (new Random()).Next(100000).ToString();
string secretKey = "";
StringBuilder sb = new StringBuilder();
foreach (byte b in MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(appId + q + salt + secretKey)))
{
sb.Append(b.ToString("x2"));
}
string sign = sb.ToString();
string url = "http://api.fanyi.baidu.com/api/trans/vip/translate?";
url += "q=" + HttpUtility.UrlEncode(q);
url += "&from=" + from;
url += "&to=" + to;
url += "&appid=" + appId;
url += "&salt=" + salt;
url += "&sign=" + sign;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";
request.UserAgent = null;
request.Timeout = 6000;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
string result = streamReader.ReadToEnd();
streamReader.Close();
responseStream.Close();
string translation = "";
foreach (Match item in Regex.Matches(Regex.Match(result, "trans_result\":\\[.*\\]").Value, "{\"src\":\".*?\",\"dst\":\".*?\"}"))
{
translation += Regex.Unescape(Regex.Match(item.Value, "\"dst\":\".*\"").Value.Replace("\"dst\":", "")).Trim('\"');
}
if (translation != "")
{
context.Post((text) =>
{
Form form = new Form();
form.FormBorderStyle = FormBorderStyle.None;
form.StartPosition = FormStartPosition.Manual;
form.Location = Cursor.Position;
form.ControlBox = false;
form.ShowIcon = false;
form.ShowInTaskbar = false;
form.BackColor = Color.White;
form.Opacity = 0.96D;
Label label = new Label();
label.Text = (string)text;
label.Font = new Font("宋体", 10.1f);
label.MaximumSize = new Size(400, 1080);
label.AutoSize = true;
label.Location = new Point(4, 4);
label.TextAlign = ContentAlignment.MiddleLeft;
form.Controls.Add(label);
form.MinimumSize = new Size(label.Width + 8, label.Height + 8);
form.MaximumSize = form.MinimumSize;
form.Size = form.MinimumSize;
bool MoveWindow = false;
int MoveX = Control.MousePosition.X;
int MoveY = Control.MousePosition.Y;
label.MouseDown += ((object sender, MouseEventArgs e) =>
{
if (e.Button == MouseButtons.Left)
{
MoveX = e.X;
MoveY = e.Y;
MoveWindow = true;
}
if (e.Button == MouseButtons.Right)
{
form.Close();
}
});
label.MouseMove += ((object sender, MouseEventArgs e) =>
{
if (MoveWindow == true)
{
form.Location = new Point(Control.MousePosition.X - MoveX, Control.MousePosition.Y - MoveY);
return;
}
});
label.MouseUp += ((object sender, MouseEventArgs e) =>
{
if (e.Button == MouseButtons.Left && MoveWindow == true)
{
MoveWindow = false;
}
});
label.MouseDoubleClick += ((object sender, MouseEventArgs e) =>
{
Clipboard.SetText(label.Text);
});
form.Show();
form.TopMost = true;
}, translation);
}
}))).Start();
请前往 https://api.fanyi.baidu.com/api/trans/product/desktop?req=developer ,申请并获取以下两个值。
appId(APP ID)、secretKey(密钥) 。