1. 程式人生 > WINDOWS開發 >寫字板(Windows程式設計)

寫字板(Windows程式設計)

前言

c# 正編譯生成debug,exe等檔案,同樣也可以通過exe反編譯生成相對應的檔案,一勞永逸,任君選擇(`?ω?′) (我們老師為了給我們看要求效果給了我們一個半成品exe,cv工程師上線了,真為所欲為

問題描述

驗證簡易相簿程式的實現(不需要寫入報告)

設計一個類似Windows寫字板的程式,使其具有以下功能:

  • 提供基本的寫字板功能,如檔案的開啟,儲存,支援txt、rtf;
  • 實現多格式的文字的編輯、修飾等,均有相應的選單來控制,常用的選單項配備相應的工具欄按鈕(可參照Windows自帶的寫字板)
    按需新增其他的程式碼,如視窗的佈局等;
  • 實現自定義的查詢對話方塊,有能力的同學可進一步實現替換/全文替換等功能
  • 狀態列中顯示學生學號、姓名等資訊,在視窗的標題中顯示當前正在編輯的檔名
  • (可選)其他你覺得實用的功能:如支援word等
  • (可選)將其改造成MDI程式:父視窗中包含新建、視窗、幫助等選單;在執行時,子視窗的選單會與其合併,合理設定各選單項的MergeIndex/MergeAction屬性,使其符合我們的使用習慣;程式設計允許使用者調整各自視窗間的佈局關係(平鋪、層疊等)等
    先看效果
    技術分享圖片

解決方法

用richtextbox是一個類似寫字板的一個控制元件,能滿足rtf,txt等格式,如果要滿足doc文件,去百度Microsoft.Office.Interop.Word.dll,下載並引用,開啟儲存程式碼如下(其中saveName為檔名)


        /// <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 Visible,ref obj);
                document.ActiveWindow.Selection.WholeStory();
                document.ActiveWindow.Selection.Copy();
                richTextBox1.Paste();
            }
            finally
            {
                document?.Close(ref obj,ref obj);
                applicationClass?.Quit(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);
                document.ActiveWindow.Selection.WholeStory();
                richTextBox1.SelectAll();
                Clipboard.SetData(DataFormats.Rtf,richTextBox1.SelectedRtf);
                document.ActiveWindow.Selection.Paste();
                document.SaveAs(ref FileName,ref obj);
            }
            finally
            {
                document?.Close(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 = "";
        }

其他一系列doc加粗,傾斜,下劃線,左中右對齊,字型型號顏色,查詢(大小寫,全字,正逆),其實本質上來說都是對richtextbox的一些屬性的修改,注意多用DropDownOpening,DropDownItemClicked等操作代替Click,使程式碼更加精簡高效

?? 技術分享圖片

??這是一個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;
            }
        }

MDI的使用,是為了讓一個父窗體擁有許多個子窗體,進行更加簡單有效的操作

技術分享圖片
在父窗體new子窗體就行,記得一系列基本操作,你也可以通過生成函式和屬性進行快速高效設定

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 的整理也是不錯的(`?ω?′)
技術分享圖片