private void textBox_PreviewExecuted(object sender, ExecutedRoutedEventArgs e) { if (e.Command == ApplicationCommands.Paste) { e.Handled = true; string text = ClipBoardHelper.GetClipBoard_UnicodeText(); Input = text.Substring(0, Math.Min(500, text.Length)); } }
void HotkeyAction4(Moudle.HotKey hotKey) { if (App.Settings.HotKey.AutoCopy) { ClipBoardHelper.Copy(hotKey.Key, hotKey.KeyModifiers); } MenuItem_Click(new MenuItem { Uid = App.Settings.HotKey.Feature4.Action, Visibility = Visibility.Hidden }, null); if (App.Settings.HotKey.AutoPaste) { ClipBoardHelper.Paste(); } }
private void MenuItem_Click(object sender, RoutedEventArgs e) { if (sender == null) { return; } if (((MenuItem)sender).Uid == null) { return; } string clip = ClipBoardHelper.GetClipBoard_UnicodeText(); if (!string.IsNullOrWhiteSpace((string)(((MenuItem)sender).ToolTip))) { clip = (string)(((MenuItem)sender).ToolTip); } StringBuilder sb = new StringBuilder(); System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Reset(); sw.Start(); switch (((MenuItem)sender).Uid) { case "1": this.Visibility = this.Visibility == Visibility.Visible ? Visibility.Hidden : Visibility.Visible; break; case "a1": if (App.Settings.RecognitionEncoding) { int encodingtype = EncodingAnalyzer.Analyze(clip); if (encodingtype == 0 || encodingtype == 1) { if (MessageBox.Show(this, "編碼似乎已是Big5,繼續轉換?", "警告", MessageBoxButton.YesNo) == MessageBoxResult.No) { return; } } } clip = ConvertHelper.Convert(clip, new Encoding[] { Encoding.GetEncoding("GBK"), Encoding.GetEncoding("BIG5") }, 1); break; case "a2": if (App.Settings.RecognitionEncoding) { int encodingtype = EncodingAnalyzer.Analyze(clip); if (encodingtype == 2 || encodingtype == 3) { if (MessageBox.Show(this, "編碼似乎已是GBK,繼續轉換?", "警告", MessageBoxButton.YesNo) == MessageBoxResult.No) { return; } } } clip = ConvertHelper.Convert(clip, new Encoding[] { Encoding.GetEncoding("BIG5"), Encoding.GetEncoding("GBK") }, 2); break; case "a3": clip = ConvertHelper.Convert(clip, 1); break; case "a4": clip = ConvertHelper.Convert(clip, 2); break; case "b1": Window_DialogHost window_File_FileNameConverter = new Window_DialogHost(Enums.Enum_Mode.Mode.File_FileName); window_File_FileNameConverter.Show(); break; case "b2": Window_DialogHost window_ClipBoard_Converter = new Window_DialogHost(Enums.Enum_Mode.Mode.ClipBoard); window_ClipBoard_Converter.Show(); break; case "c1": Window_DialogHost Window_DialogHost = new Window_DialogHost(Enums.Enum_Mode.Mode.AutioTag, Format.ID3); Window_DialogHost.Show(); break; case "c2": Window_DialogHost Window_DialogHost2 = new Window_DialogHost(Enums.Enum_Mode.Mode.AutioTag, Format.APE); Window_DialogHost2.Show(); break; case "c3": Window_DialogHost Window_DialogHost3 = new Window_DialogHost(Enums.Enum_Mode.Mode.AutioTag, Format.OGG); Window_DialogHost3.Show(); break; case "za1": foreach (char c in clip) { if ((' ' <= c && c <= '~') || (c == '\r') || (c == '\n')) { if (c == '&') { sb.Append("&"); } else if (c == '<') { sb.Append("<"); } else if (c == '>') { sb.Append(">"); } else { sb.Append(c.ToString()); } } else { sb.Append("&#"); sb.Append(Convert.ToInt32(c)); sb.Append(";"); } } clip = sb.ToString(); break; case "za2": foreach (char c in clip) { if ((' ' <= c && c <= '~') || (c == '\r') || (c == '\n')) { if (c == '&') { sb.Append("&"); } else if (c == '<') { sb.Append("<"); } else if (c == '>') { sb.Append(">"); } else { sb.Append(c.ToString()); } } else { sb.Append("&#x"); sb.Append(Convert.ToInt32(c).ToString("X")); sb.Append(";"); } } clip = sb.ToString(); break; case "za3": clip.Replace("&", "&"); clip.Replace("<", "<"); clip.Replace(">", ">"); //以;將文字拆成陣列 string[] tmp = clip.Split(';'); //檢查最後一個字元是否為【;】,因為有【英文】、【阿拉伯數字】、【&#XXXX;】 //若最後一個要處理的字並非HTML UNICODE則不進行處理 bool Process_last = clip.Substring(clip.Length - 1, 1).Equals(";"); //Debug.WriteLine(tmp.Length + ""); for (int i = 0; i < tmp.Length; i++) { //以&#將文字拆成陣列 string[] tmp2 = tmp[i].Split(new string[] { "&#" }, StringSplitOptions.RemoveEmptyEntries); if (tmp2.Length == 1) { //如果長度為1則試圖轉換UNICODE回字符,若失敗則使用原本的字元 if (i != tmp.Length - 1) { try { if (tmp2[0].StartsWith("x")) { sb.Append(Convert.ToChar(Convert.ToInt32(tmp2[0].Substring(1, tmp2[0].Length - 1), 16)).ToString()); } else { sb.Append(Convert.ToChar(Convert.ToInt32(int.Parse(tmp2[0]))).ToString()); } } catch { sb.Append(tmp2[0]); } } else { sb.Append(tmp2[0]); } } if (tmp2.Length == 2) { //若長度為2,則第一項不處理,只處理第二項即可 sb.Append(tmp2[0]); var g = Convert.ToInt32(tmp2[1].Substring(1, tmp2[1].Length - 1), 16); if (tmp2[1].StartsWith("x")) { sb.Append(Convert.ToChar(Convert.ToInt32(tmp2[1].Substring(1, tmp2[1].Length - 1), 16)).ToString()); } else { sb.Append(Convert.ToChar(Convert.ToInt32(tmp2[1])).ToString()); } } } clip = sb.ToString(); break; case "zb1": //Unicode>GBK clip = Encoding.Default.GetString(Encoding.GetEncoding("GBK").GetBytes(clip)); break; case "zb2": clip = Encoding.Default.GetString(Encoding.GetEncoding("BIG5").GetBytes(clip)); break; case "zb3": clip = Encoding.Default.GetString(Encoding.GetEncoding("Shift-JIS").GetBytes(clip)); break; case "zc1": //Shift-JIS>GBK clip = Encoding.GetEncoding("shift_jis").GetString(Encoding.GetEncoding("GBK").GetBytes(clip)); break; case "zc2": clip = Encoding.GetEncoding("shift_jis").GetString(Encoding.GetEncoding("BIG5").GetBytes(clip)); break; case "zc3": clip = Encoding.GetEncoding("GBK").GetString(Encoding.GetEncoding("shift_jis").GetBytes(clip)); break; case "zc4": clip = Encoding.GetEncoding("BIG5").GetString(Encoding.GetEncoding("shift_jis").GetBytes(clip)); break; case "zd1": //hz-gb-2312>GBK clip = Encoding.GetEncoding("hz-gb-2312").GetString(Encoding.GetEncoding("GBK").GetBytes(clip)); break; case "zd2": clip = Encoding.GetEncoding("hz-gb-2312").GetString(Encoding.GetEncoding("BIG5").GetBytes(clip)); break; case "zd3": clip = Encoding.GetEncoding("GBK").GetString(Encoding.GetEncoding("hz-gb-2312").GetBytes(clip)); break; case "zd4": clip = Encoding.GetEncoding("BIG5").GetString(Encoding.GetEncoding("hz-gb-2312").GetBytes(clip)); break; } ClipBoardHelper.SetClipBoard_UnicodeText(clip); sw.Stop(); //顯示提示 switch (((MenuItem)sender).Uid) { case "1": case "b1": case "b2": case "c1": case "c2": case "c3": break; default: if (App.Settings.Prompt && !(((MenuItem)sender).Visibility == Visibility.Hidden && (App.Settings.HotKey.AutoCopy || App.Settings.HotKey.AutoPaste))) { ContextMenu NotifyIconMenu = (ContextMenu)this.FindResource("NotifyIconMenu"); string ItemInfo = ((MenuItem)GetByUid(NotifyIconMenu, ((MenuItem)sender).Uid)).Header.ToString(); MessageBox.Show(this, String.Format("{0}轉換完成\r\n耗時:{1} ms", ItemInfo, sw.ElapsedMilliseconds)); } break; } }