zoukankan      html  css  js  c++  java
  • C#编写的Unicode文本空行去除器

    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();
                        }
                    }
                }
            }
        }
    }

  • 相关阅读:
    springboot+mybatis集成多数据源MySQL/Oracle/SqlServer
    向Spring容器中注册组件的方法汇总小结
    使用spring initialization创建SpringBoot项目
    mybatis-generator 的坑我都走了一遍
    初识Jsp,JavaBean,Servlet以及一个简单mvc模式的登录界面
    【Linux】用户只显示$问题
    【Linux】Ubuntu创建用户、删除用户、设置用户密码,root和普通用户切换
    【linux】ubuntu安装ssh
    【整理】【JS】map的基本操作
    【整理】【JS】数组定义、添加、删除、替换、遍历基本操作
  • 原文地址:https://www.cnblogs.com/mossan/p/335798.html
Copyright © 2011-2022 走看看