123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- //Target:Winform
- //Ref:System
- //Ref:System.Drawing
- //Ref:System.Windows.Forms
- using System;
- using System.Diagnostics;
- using System.Drawing;
- using System.Reflection;
- using System.Text;
- using System.Windows.Forms;
- [assembly: AssemblyTitle("Mojibake correcting for filename and file content")]
- [assembly: AssemblyProduct("Mojibake correcting")]
- [assembly: AssemblyCopyright("Copyright © V 2016")]
- [assembly: AssemblyVersion("1.0.0.0")]
- [assembly: AssemblyFileVersion("1.0.0.0")]
- namespace MojibakeCorrecting
- {
- internal static class ShutUpDesigener
- {
- public static T AddControl<T>(this Control me, T toAdd) where T : Control
- {
- me.Controls.Add(toAdd);
- return toAdd;
- }
- public static T AddItem<T>(this ToolStrip me, T toAdd) where T : ToolStripItem
- {
- me.Items.Add(toAdd);
- return toAdd;
- }
- }
- public class MainForm : Form
- {
- private MainForm()
- {
- InitUi();
- }
- private void InitUi()
- {
- ClientSize = new Size(640, 480);
- Text = Application.ProductName;
- var tcMain = new TabControl { Dock = DockStyle.Fill };
- Controls.Add(tcMain);
- //var tpFileName = new TabPage("File name");
- //tcMain.TabPages.Add(tpFileName);
- //var tpFileContent = new TabPage("File content");
- //tcMain.TabPages.Add(tpFileContent);
- var tpEncodingDetect = new TabPage("Encoding Detect");
- tcMain.TabPages.Add(tpEncodingDetect);
- {
- //instance && conf
- var toolbar = new ToolStrip { Dock = DockStyle.Top, RenderMode = ToolStripRenderMode.System, GripStyle = ToolStripGripStyle.Hidden };
- toolbar.AddItem(new ToolStripLabel("From:"));
- var tbcbF = toolbar.AddItem(new ToolStripComboBox { ComboBox = { DisplayMember = "DisplayName", ValueMember = "CodePage", DropDownStyle = ComboBoxStyle.DropDownList, DataSource = Encoding.GetEncodings(), Width = 200 } }).ComboBox;
- toolbar.AddItem(new ToolStripLabel("To:"));
- var tbcbT = toolbar.AddItem(new ToolStripComboBox { ComboBox = { DisplayMember = "DisplayName", ValueMember = "CodePage", DropDownStyle = ComboBoxStyle.DropDownList, DataSource = Encoding.GetEncodings(), Width = 200 } }).ComboBox;
- var tbtnC = toolbar.AddItem(new ToolStripButton("MConv") { ToolTipText = "Manual convert" });
- var tbckA = toolbar.AddItem(new ToolStripButton("AOChg") { ToolTipText = "Auto convert on changed" });
- var tbtnR = toolbar.AddItem(new ToolStripButton("Reset") { ToolTipText = "Reset controls" });
- var split = new SplitContainer { Dock = DockStyle.Fill, Orientation = Orientation.Horizontal };
- var txtOrig = new TextBox { Multiline = true, Dock = DockStyle.Fill };
- var txtResu = new TextBox { Multiline = true, Dock = DockStyle.Fill, ReadOnly = true };
- //fucking resharper
- Debug.Assert(tbcbF != null, "cb is null");
- Debug.Assert(tbcbT != null, "cb is null");
- //event
- tbckA.Click += delegate
- {
- tbckA.Checked = !tbckA.Checked;
- tbtnC.Enabled = !tbckA.Checked;
- };
- var actConv = new Action(() =>
- {
- var enc = Encoding.GetEncoding((int)tbcbF.SelectedValue);
- var dec = Encoding.GetEncoding((int)tbcbT.SelectedValue);
- txtResu.Text = dec.GetString(enc.GetBytes(txtOrig.Text));
- });
- var actChkConv = new Action(() => { if (tbckA.Checked) actConv(); });
- var actReset = new Action(() =>
- {
- var def = Encoding.Default.CodePage;
- tbcbF.SelectedValue = def;
- tbcbT.SelectedValue = def;
- txtOrig.Clear();
- txtResu.Clear();
- });
- tbcbF.SelectedIndexChanged += delegate { actChkConv(); };
- tbcbT.SelectedIndexChanged += delegate { actChkConv(); };
- txtOrig.TextChanged += delegate { actChkConv(); };
- tbtnC.Click += delegate { actConv(); };
- tbtnR.Click += delegate { actReset(); };
- //put
- tpEncodingDetect.Controls.Add(split);
- split.Panel1.Controls.Add(txtOrig);
- split.Panel2.Controls.Add(txtResu);
- tpEncodingDetect.Controls.Add(toolbar);
- //init
- Shown += delegate
- {
- actReset();
- };
- }
- }
- /// <summary>
- /// 应用程序的主入口点。
- /// </summary>
- [STAThread]
- private static void Main()
- {
- Application.EnableVisualStyles();
- Application.SetCompatibleTextRenderingDefault(false);
- Application.Run(new MainForm());
- }
- }
- }
|