⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainform.cs

📁 PPC下的文本编辑器源代码
💻 CS
字号:
/*
 * PPC_edit Lite - lite version of PPC_edit text editor.
 * 
 * 
 * changelog: 
 * 1.0a - added context menu and Edit menu and fixed some bugs
 *      bugfixes: textbox won't resize if SIP was activated 
 * 
 * 1.0 - first version
 * 
 * 
 * */

using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace PPC_editLite
{
    public partial class MainForm : Form
    {

        const uint EM_REPLACESEL = 0xc2;
        //need this to avoid flickering in textfield
        [DllImport("coredll")]
        extern static int SendMessage(IntPtr hWnd, uint Msg, bool WParam, string LParam);

        public MainForm()
        {
            InitializeComponent();
        }

        private void FileExitClick(object sender, EventArgs e)
        {
            if (Editor.Text == "")
                Close();
            else
            {
                if (MessageBox.Show("Do you really want to exit PPC_edit Lite?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                    Close();
            }
        }

        private void HelpClick(object sender, EventArgs e)
        {
            MessageBox.Show("PPC_edit Lite is smaller version of the PPC_edit text editor.\r\nIt supports loading and saving of TXT format only.\r\nSee http://ppc-editlite.sourceforge.net for more information.", "Help");
        }

        private void FileNewClick(object sender, EventArgs e)
        {
            if (Editor.Text != "")
                if (MessageBox.Show("Do you really want create a new file? Your current text will be lost if unsaved.", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                    Editor.Text = "";
        }

        private void FileOpenClick(object sender, EventArgs e)
        {
            DialogResult dr;
            if (Editor.Text != "")
                dr = MessageBox.Show("Do you really want open a file? Your current text will be lost if unsaved.", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
            else
                dr = DialogResult.Yes;
            
            if(dr == DialogResult.Yes)
                {
                    OpenFileDialog ofd = new OpenFileDialog();
                    ofd.Filter = "Text file (*.txt)|*.txt";
                    if (ofd.ShowDialog() == DialogResult.OK)
                    {
                        StreamReader sr = new StreamReader(ofd.FileName);
                        Editor.Text = sr.ReadToEnd();
                        sr.Close();
                    }
                }
        }

        private void FileSaveClick(object sender, EventArgs e)
        {
            SaveFileDialog SaveDialog = new SaveFileDialog();
            SaveDialog.Filter = "Text file (*.txt)|*.txt";
            if (SaveDialog.ShowDialog() == DialogResult.OK)
            {
                StreamWriter sw = new StreamWriter(SaveDialog.FileName);
                sw.Write(Editor.Text);
                sw.Close();
            }
        }

        //SendMessage(Editor.Handle, EM_REPLACESEL, false, Text);
        /// <summary>
        /// Deletes selected text
        /// </summary>
        private void Clear()
        {
            if (Editor.SelectedText != "")
                SendMessage(Editor.Handle, EM_REPLACESEL, false, "");
        }

        /// <summary>
        /// Cuts selected text to clipboard
        /// </summary>
        private void Cut()
        {
            Copy();
            Clear();
        }

        /// <summary>
        /// Copies selected text to clipboard
        /// </summary>
        private void Copy()
        {
            if (Editor.SelectedText != "")
                Clipboard.SetDataObject(Editor.SelectedText);
        }

        /// <summary>
        /// Pastes text from clipboard to the current position
        /// </summary>
        private void Paste()
        {
            //paste text from clipboard
            IDataObject iData = Clipboard.GetDataObject(); //get the clipboard data object
            if (iData.GetDataPresent(DataFormats.Text)) //if we have text in clipboard,
            {
                SendMessage(Editor.Handle, EM_REPLACESEL, false, Convert.ToString(iData.GetData(DataFormats.Text)));
            }
        }
        /// <summary>
        /// Selects all text in the editor
        /// </summary>
        private void SelectAll()
        {
            Editor.SelectAll();
        }

        private void menuItem19_Click(object sender, EventArgs e)
        {
            SelectAll();
        }

        private void menuItem17_Click(object sender, EventArgs e)
        {
            Clear();
        }

        private void menuItem16_Click(object sender, EventArgs e)
        {
            Paste();
        }

        private void menuItem15_Click(object sender, EventArgs e)
        {
            Copy();
        }

        private void menuItem14_Click(object sender, EventArgs e)
        {
            Cut();
        }

        private void menuItem7_Click(object sender, EventArgs e)
        {
            SelectAll();
        }

        private void menuItem10_Click(object sender, EventArgs e)
        {
            Clear();
        }

        private void menuItem9_Click(object sender, EventArgs e)
        {
            Paste();
        }

        private void menuItem8_Click(object sender, EventArgs e)
        {
            Copy();
        }

        private void menuItem11_Click(object sender, EventArgs e)
        {
            Cut();
        }

        private void SIP_EnabledChanged(object sender, EventArgs e)
        {
            if (SIP.Enabled)
                Editor.Size = new Size(240, 188);
            else
                Editor.Size = new Size(240, 268);
        }

        private void menuItem20_Click(object sender, EventArgs e)
        {
            if (menuItem20.Checked)
            {
                menuItem20.Checked = false;
                Editor.WordWrap = false;
                Editor.ScrollBars = ScrollBars.Both;
            }
            else
            {
                menuItem20.Checked = true;
                Editor.WordWrap = true;
                Editor.ScrollBars = ScrollBars.Vertical;    
            }
        }

    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -