WordPad (Windows programming)

Foreword

c # being compiled debug, exe files and so on, also can be exe decompiled to generate corresponding documents, once and for all, choice is yours ( `· ω · ') ( our teachers required in order for us to see the effect given us a semi-exe , Cv engineer is online, really do whatever I want )

Problem Description

Verify the implementation of the simple album program (no need to write a report)

Design a program similar to Windows WordPad so that it has the following functions:

  • Provide basic writing board functions, such as file opening and saving, support txt, rtf;
  • The editing and modification of multi-format text are controlled by corresponding menus. Commonly used menu items are equipped with corresponding toolbar buttons (refer to the Windows built-in writing board).
    Add other codes as needed, such as the layout of the window Wait;
  • Realize customized search dialog, capable students can further implement replacement / full text replacement and other functions
  • The status bar displays the student ID, name and other information, and displays the name of the file currently being edited in the window title
  • (Optional) Other functions you find useful: such as support for word, etc.
  • (Optional) Transform it into an MDI program: the parent window contains menus such as New, Window, and Help; at runtime, the menu of the child window will be merged with it, and the MergeIndex / MergeAction property of each menu item will be set appropriately to make it conform to our habits; programming allows the user to adjust the relationship between the layout of the respective window (tile, laminate, etc.) and the like
    look effect
    image.png

Solution

Using richtextbox is a control similar to WordPad, which can meet rtf, txt and other formats. If you want to meet doc documents, go to Baidu Microsoft.Office.Interop.Word.dll, download and quote, open the save code as follows (where saveName is a file name)


        /// <summary>
        /// 打开文档操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog1.InitialDirectory = "C:\\";
            openFileDialog1.Filter = "rtf 文件|*.rtf|txt 文件|*.txt|doc files|*.doc";
            if (openFileDialog1.ShowDialog() != DialogResult.OK)
                return;
            saveName = openFileDialog1.FileName;
            if (openFileDialog1.FilterIndex == 1)
                richTextBox1.LoadFile(saveName);
            else if (openFileDialog1.FilterIndex == 2)
                richTextBox1.LoadFile(saveName, RichTextBoxStreamType.PlainText);
            else
                OpenWord(saveName);
            Text = saveName;
        }
        public void 父窗体打开Open(WritingBoard writingBoard)
        {
            writingBoard.openFileDialog1.InitialDirectory = "C:\\";
            writingBoard.openFileDialog1.Filter = "rtf 文件|*.rtf|txt 文件|*.txt|doc files|*.doc";
            if (writingBoard.openFileDialog1.ShowDialog() != DialogResult.OK)
                return;
            saveName = writingBoard.openFileDialog1.FileName;
            if (writingBoard.openFileDialog1.FilterIndex == 1)
                writingBoard.richTextBox1.LoadFile(saveName);
            else if (writingBoard.openFileDialog1.FilterIndex == 2)
                writingBoard.richTextBox1.LoadFile(saveName, RichTextBoxStreamType.PlainText);
            else
                OpenWord(saveName);
            writingBoard.Text = saveName;
            return;
        }
        public void OpenWord(string fileName)
        {
            ApplicationClass applicationClass = new ApplicationClass();
            Document document = null;
            object obj = Missing.Value;
            object FileName = fileName;
            object ReadOnly = false;
            object Visible = true;
            try
            {
                document = applicationClass.Documents.Open(ref FileName, ref obj, ref ReadOnly, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref Visible, ref obj, ref obj, ref obj, ref obj);
                document.ActiveWindow.Selection.WholeStory();
                document.ActiveWindow.Selection.Copy();
                richTextBox1.Paste();
            }
            finally
            {
                document?.Close(ref obj, ref obj, ref obj);
                applicationClass?.Quit(ref obj, ref obj, ref obj);
            }
        }
        /// <summary>
        /// 保存操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (saveName != "")
            {
                richTextBox1.SaveFile(saveName, RichTextBoxStreamType.RichText);
            }
            else
            {
                saveFileDialog1.Filter = "rtf files|*.rtf";
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    saveName = saveFileDialog1.FileName;
                    richTextBox1.SaveFile(saveName, RichTextBoxStreamType.RichText);
                }
            }
        }
        /// <summary>
        /// 另存为操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 另存为AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileDialog1.FileName = saveName.Substring(saveName.LastIndexOf('\\') + 1);
            saveFileDialog1.Filter = "rtf files|*.rtf|txt files|*.txt|doc files|*.doc";
            saveFileDialog1.AddExtension = true;
            if (saveFileDialog1.ShowDialog() != DialogResult.OK)
                return;
            saveName = saveFileDialog1.FileName;
            if (saveFileDialog1.FilterIndex == 1)
                richTextBox1.SaveFile(saveName, RichTextBoxStreamType.RichText);
            else if (saveFileDialog1.FilterIndex == 2)
                richTextBox1.SaveFile(saveName, RichTextBoxStreamType.PlainText);
            else
                SaveAsWord(saveName);
        }
        public void SaveAsWord(string fileName)
        {
            ApplicationClass applicationClass = new ApplicationClass();
            Document document = null;
            object obj = Missing.Value;
            object FileName = fileName;
            try
            {
                document = applicationClass.Documents.Add(ref obj, ref obj, ref obj, ref obj);
                document.ActiveWindow.Selection.WholeStory();
                richTextBox1.SelectAll();
                Clipboard.SetData(DataFormats.Rtf, richTextBox1.SelectedRtf);
                document.ActiveWindow.Selection.Paste();
                document.SaveAs(ref FileName, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj, ref obj);
            }
            finally
            {
                document?.Close(ref obj, ref obj, ref obj);
                applicationClass?.Quit(ref obj, ref obj, ref obj);
            }
        }
        /// <summary>
        /// 新建操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Clear();
            saveName = "";
        }

Other series of doc are bold, slanted, underlined, left-center-right aligned, font model color, search (case, whole word, forward and reverse), in fact, they are essentially modifications of some properties of richtextbox, pay attention to use DropDownOpening Operations such as DropDownItemClicked replace Click, making the code more streamlined and efficient

   image.png

  This is an example of MDI adjustment sub-form using DropDownItemClicked, efficient and elegant = v =

private void 窗口WToolStripMenuItem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            string layout=e.ClickedItem.Text;
            switch(layout)
            {
                case "水平平铺": LayoutMdi(MdiLayout.TileHorizontal);break;
                case "垂直平铺": LayoutMdi(MdiLayout.TileVertical); break;
                case "层叠": LayoutMdi(MdiLayout.Cascade); break;
            }
        }

The use of MDI is to allow a parent form to have many child forms for simpler and more effective operations

image.png
On the parent form new child form, just remember a series of basic operations, you can also quickly and efficiently set up by generating functions and properties

private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WritingBoard writingBoard = new WritingBoard();
            writingBoard.TopLevel = false;
            writingBoard.MdiParent = this;
            writingBoard.Show();
        }

        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WritingBoard writingBoard = new WritingBoard();
            writingBoard.TopLevel = false;
            writingBoard.MdiParent = this;
            writingBoard.父窗体打开Open(writingBoard);
            writingBoard.Show();
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (WritingBoard writingBoard in MdiChildren) writingBoard.Close();
            Application.Exit();
        }

        private void 窗口WToolStripMenuItem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            string layout=e.ClickedItem.Text;
            switch(layout)
            {
                case "水平平铺": LayoutMdi(MdiLayout.TileHorizontal);break;
                case "垂直平铺": LayoutMdi(MdiLayout.TileVertical); break;
                case "层叠": LayoutMdi(MdiLayout.Cascade); break;
            }
        }

Source code attached

  Portal , I set up a branch, so that I can put the code with the same attribute of different content together, and can also have different md to read, which is also good for git collation (` ・ ω ・ ´)
image.png

Guess you like

Origin www.cnblogs.com/xxhao/p/12756315.html