C#編寫的Unicode文本空行去除器

來源:互聯網
上載者:User

C#編寫的Unicode文本空行去除器

  網頁儲存文本時,往往會出現很多空行,對於編程及其它應用都很不方便,使用Word把原有格式都繼承了,用其他編輯工具命令又太多,因此編寫了這樣一個文本空行去除器。可以縮小到托盤表徵圖,使用起來很方便。
  注意:文字檔首先必須是Unicode格式的。

*------------------------------------------------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reflection;
using System.Data;
using System.IO;
using System.Text;

namespace RMBlankLine
{
    /// Form1 的摘要說明。
    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Button button1;
        private Icon mNetTrayIcon = new Icon("Tray.ico");
        private NotifyIcon TrayIcon;
        private ContextMenu notifyiconMnu;
       
        /// 必需的設計器變數。
        private System.ComponentModel.Container components = null;

        public Form1()
        {
            // Windows 表單設計器支援所必需的
            InitializeComponent();
            //初始化托盤程式的各個要素
            Initializenotifyicon();
            // TODO: 在 InitializeComponent 調用後添加任何建構函式代碼
        }

        private void Initializenotifyicon()
        {
            //設定托盤程式的各個屬性
            TrayIcon = new NotifyIcon();
            TrayIcon.Icon = mNetTrayIcon;
            TrayIcon.Text = "文本空行去除器" + "\n" + "Mossan 2004-2006";
            TrayIcon.Visible = true;
            TrayIcon.DoubleClick += new System.EventHandler(this.TrayIcon_DoubleClick);

            //定義一個MenuItem數組,並把此數組同時賦值給ContextMenu對象
            MenuItem[] mnuItms = new MenuItem[4];
            mnuItms[0] = new MenuItem();
            mnuItms[0].Text = "開啟(&O)";
            mnuItms[0].Click += new System.EventHandler(this.openwindow);
            mnuItms[0].DefaultItem = true;
            mnuItms[1] = new MenuItem();
            mnuItms[1].Text = "關於(&A)";
            mnuItms[1].Click += new System.EventHandler(this.showmessage);
            mnuItms[2] = new MenuItem("-");
            mnuItms[3] = new MenuItem();
            mnuItms[3].Text = "退出(&C)";
            mnuItms[3].Click += new System.EventHandler(this.ExitSelect);
            notifyiconMnu = new ContextMenu(mnuItms);
            TrayIcon.ContextMenu = notifyiconMnu;
            //為托盤程式加入設定好的ContextMenu對象
        }

        private void TrayIcon_DoubleClick(object Sender, EventArgs e)
        {

            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Visible = true;
                this.WindowState = FormWindowState.Normal;
                this.Activate();
            }
            else
            {
                this.WindowState = FormWindowState.Minimized;
                this.Hide();
            }

        }

        public void openwindow(object sender, System.EventArgs e)
        {
            this.Visible = true;

            if (this.WindowState == FormWindowState.Minimized)
                this.WindowState = FormWindowState.Normal;

            this.Activate();
        }

        public void showmessage(object sender, System.EventArgs e)
        {
            MessageBox.Show("雙 擊 主 窗 口 最 小 化" + "\n\n" + "http://mossan.cnblogs.com");
        }

        public void ExitSelect(object sender, System.EventArgs e)
        {
            //隱藏托盤程式中的表徵圖
            TrayIcon.Visible = false;
            //關閉系統
            this.Close();
        }

        private void Form1_DoubleClick(object sender, System.EventArgs e)
        {
            this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
            this.Hide();
        }

        /// <summary>
        /// 清理所有正在使用的資源。
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        #region Windows 表單設計器產生的程式碼
        /// 設計器支援所需的方法 - 不要使用代碼編輯器修改
        /// 此方法的內容。
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // button1
            this.button1.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.button1.Location = new System.Drawing.Point(116, 38);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(64, 24);
            this.button1.TabIndex = 0;
            this.button1.Text = "瀏覽(&B)";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // Form1
            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
            this.ClientSize = new System.Drawing.Size(292, 103);
            this.ControlBox = false;
            this.Controls.Add(this.button1);
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "Form1";
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "UniCode文本空行去除器";
            this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
            this.DoubleClick += new System.EventHandler(this.Form1_DoubleClick);
            this.ResumeLayout(false);

        }
        #endregion

        /// 應用程式的主進入點。
        [STAThread]
        static void Main()
        {
            //得到正在啟動並執行常式
            Process instance = RunningInstance();
            if (instance == null)
            {
                //如果沒有其它常式,就建立一個表單
                Application.Run(new Form1());
            }
            else
            {
                //處理髮現的常式
                HandleRunningInstance(instance);
            }
        }

        public static Process RunningInstance()
        {
            Process current = Process.GetCurrentProcess();
            Process[] processes = Process.GetProcessesByName(current.ProcessName);
            //遍曆正在有相同名字啟動並執行常式
            foreach (Process process in processes)
            {
                //忽略現有的常式
                if (process.Id != current.Id)
                {
                    //確保常式從EXE檔案運行
                    if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") ==current.MainModule.FileName)
                    {
                        //返回另一個常式執行個體
                        return process;
                    }
                }
            }
            //沒有其它的常式,返回Null
            return null;
        }
   
    public static void HandleRunningInstance(Process instance)
    {
        //確保視窗沒有被最小化或最大化
        ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);
        //設定真執行個體程為foreground window
        SetForegroundWindow(instance.MainWindowHandle);
    }

    [DllImport("User32.dll")]
    private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);

    [DllImport("User32.dll")]
    private static extern bool SetForegroundWindow(IntPtr hWnd);
    private const int WS_SHOWNORMAL = 1;

        private void button1_Click(object sender, System.EventArgs e)
        {
            // 開啟目錄對話方塊,選中文字檔
            OpenFileDialog fdlg = new OpenFileDialog();
            fdlg.Title = "選擇文字檔";
            // 檔案過濾類型
            fdlg.Filter = "文字檔(*.txt)|*.txt|所有檔案(*.*)|*.*";
            // 預設過濾檔案類型
            fdlg.FilterIndex = 1;
            if (fdlg.ShowDialog() == DialogResult.OK)
            {
                string Path = fdlg.FileName;
                int n = Path.LastIndexOf('\\');
                string Dir = Path.Substring(0, n);
                string Name = Path.Substring(n + 1, Path.Length - n - 1);

                StreamReader reader = null;
                StreamWriter writer = null;
                Encoding unicode = Encoding.Unicode;
                try
                {
                    reader = new StreamReader(Path);
                    writer = new StreamWriter("new_" + Name, false, unicode);
                    string line = reader.ReadLine();
                    while (line != null)
                    {
                        if (line.Trim().Length > 0)
                        {
                            writer.WriteLine(line);
                        }
                        line = reader.ReadLine();
                    }
                    MessageBox.Show("轉換成功!");
                }
                catch
                {
                    MessageBox.Show("意外中斷!");
                    return;
                }
                finally
                {
                    if (reader != null)
                    {
                        reader.Close();
                    }
                    if (writer != null)
                    {
                        writer.Close();
                    }
                }
            }
        }
    }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.