系统相关
首页 > 系统相关> > 写字板(Windows程序设计)

写字板(Windows程序设计)

作者:互联网

前言

c# 正编译生成debug,exe等文件,同样也可以通过exe反编译生成相对应的文件,一劳永逸,任君选择(`・ω・´) (我们老师为了给我们看要求效果给了我们一个半成品exe,cv工程师上线了,真为所欲为

问题描述

验证简易相册程序的实现(不需要写入报告)

设计一个类似Windows写字板的程序,使其具有以下功能:

解决方法


        /// <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 = "";
        }

  这是一个MDI调整子窗体使用DropDownItemClicked的例子,高效优雅 = 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;
            }
        }

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

源码附上

  传送门,我设置了分支,这样我就可以把不同内容相同属性的代码放在一起了,同时也可以拥有不同的md可以阅读,对于git 的整理也是不错的(`・ω・´)
image.png

标签:saveName,obj,写字板,void,object,Windows,writingBoard,程序设计,ref
来源: https://www.cnblogs.com/xxhao/p/12756315.html