MainForm.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //Target:Winform
  2. //Ref:System
  3. //Ref:System.Drawing
  4. //Ref:System.Windows.Forms
  5. using System;
  6. using System.Diagnostics;
  7. using System.Drawing;
  8. using System.Reflection;
  9. using System.Text;
  10. using System.Windows.Forms;
  11. [assembly: AssemblyTitle("Mojibake correcting for filename and file content")]
  12. [assembly: AssemblyProduct("Mojibake correcting")]
  13. [assembly: AssemblyCopyright("Copyright © V 2016")]
  14. [assembly: AssemblyVersion("1.0.0.0")]
  15. [assembly: AssemblyFileVersion("1.0.0.0")]
  16. namespace MojibakeCorrecting
  17. {
  18. internal static class ShutUpDesigener
  19. {
  20. public static T AddControl<T>(this Control me, T toAdd) where T : Control
  21. {
  22. me.Controls.Add(toAdd);
  23. return toAdd;
  24. }
  25. public static T AddItem<T>(this ToolStrip me, T toAdd) where T : ToolStripItem
  26. {
  27. me.Items.Add(toAdd);
  28. return toAdd;
  29. }
  30. }
  31. public class MainForm : Form
  32. {
  33. private MainForm()
  34. {
  35. InitUi();
  36. }
  37. private void InitUi()
  38. {
  39. ClientSize = new Size(640, 480);
  40. Text = Application.ProductName;
  41. var tcMain = new TabControl { Dock = DockStyle.Fill };
  42. Controls.Add(tcMain);
  43. //var tpFileName = new TabPage("File name");
  44. //tcMain.TabPages.Add(tpFileName);
  45. //var tpFileContent = new TabPage("File content");
  46. //tcMain.TabPages.Add(tpFileContent);
  47. var tpEncodingDetect = new TabPage("Encoding Detect");
  48. tcMain.TabPages.Add(tpEncodingDetect);
  49. {
  50. //instance && conf
  51. var toolbar = new ToolStrip { Dock = DockStyle.Top, RenderMode = ToolStripRenderMode.System, GripStyle = ToolStripGripStyle.Hidden };
  52. toolbar.AddItem(new ToolStripLabel("From:"));
  53. var tbcbF = toolbar.AddItem(new ToolStripComboBox { ComboBox = { DisplayMember = "DisplayName", ValueMember = "CodePage", DropDownStyle = ComboBoxStyle.DropDownList, DataSource = Encoding.GetEncodings(), Width = 200 } }).ComboBox;
  54. toolbar.AddItem(new ToolStripLabel("To:"));
  55. var tbcbT = toolbar.AddItem(new ToolStripComboBox { ComboBox = { DisplayMember = "DisplayName", ValueMember = "CodePage", DropDownStyle = ComboBoxStyle.DropDownList, DataSource = Encoding.GetEncodings(), Width = 200 } }).ComboBox;
  56. var tbtnC = toolbar.AddItem(new ToolStripButton("MConv") { ToolTipText = "Manual convert" });
  57. var tbckA = toolbar.AddItem(new ToolStripButton("AOChg") { ToolTipText = "Auto convert on changed" });
  58. var tbtnR = toolbar.AddItem(new ToolStripButton("Reset") { ToolTipText = "Reset controls" });
  59. var split = new SplitContainer { Dock = DockStyle.Fill, Orientation = Orientation.Horizontal };
  60. var txtOrig = new TextBox { Multiline = true, Dock = DockStyle.Fill };
  61. var txtResu = new TextBox { Multiline = true, Dock = DockStyle.Fill, ReadOnly = true };
  62. //fucking resharper
  63. Debug.Assert(tbcbF != null, "cb is null");
  64. Debug.Assert(tbcbT != null, "cb is null");
  65. //event
  66. tbckA.Click += delegate
  67. {
  68. tbckA.Checked = !tbckA.Checked;
  69. tbtnC.Enabled = !tbckA.Checked;
  70. };
  71. var actConv = new Action(() =>
  72. {
  73. var enc = Encoding.GetEncoding((int)tbcbF.SelectedValue);
  74. var dec = Encoding.GetEncoding((int)tbcbT.SelectedValue);
  75. txtResu.Text = dec.GetString(enc.GetBytes(txtOrig.Text));
  76. });
  77. var actChkConv = new Action(() => { if (tbckA.Checked) actConv(); });
  78. var actReset = new Action(() =>
  79. {
  80. var def = Encoding.Default.CodePage;
  81. tbcbF.SelectedValue = def;
  82. tbcbT.SelectedValue = def;
  83. txtOrig.Clear();
  84. txtResu.Clear();
  85. });
  86. tbcbF.SelectedIndexChanged += delegate { actChkConv(); };
  87. tbcbT.SelectedIndexChanged += delegate { actChkConv(); };
  88. txtOrig.TextChanged += delegate { actChkConv(); };
  89. tbtnC.Click += delegate { actConv(); };
  90. tbtnR.Click += delegate { actReset(); };
  91. //put
  92. tpEncodingDetect.Controls.Add(split);
  93. split.Panel1.Controls.Add(txtOrig);
  94. split.Panel2.Controls.Add(txtResu);
  95. tpEncodingDetect.Controls.Add(toolbar);
  96. //init
  97. Shown += delegate
  98. {
  99. actReset();
  100. };
  101. }
  102. }
  103. /// <summary>
  104. /// 应用程序的主入口点。
  105. /// </summary>
  106. [STAThread]
  107. private static void Main()
  108. {
  109. Application.EnableVisualStyles();
  110. Application.SetCompatibleTextRenderingDefault(false);
  111. Application.Run(new MainForm());
  112. }
  113. }
  114. }