c#窗体入门实战程序——绿色草稿纸,一键解锁打开,保存,复制,粘贴,剪切,打印,删除,全选,字体,颜色等

超详细c#窗体入门实战程序——绿色草稿纸

 带你一键解锁打开,保存,复制,粘贴,剪切,打印,删除,全选,字体,颜色等

目录

超详细c#窗体入门实战程序——绿色草稿纸

 带你一键解锁打开,保存,复制,粘贴,剪切,打印,删除,全选,字体,颜色等

一、程序介绍

二、几个板块的实现解释

1、新建基本菜单栏以及文本框

2.打开——从电脑上传文件,使文件内容显示在文本框上面

3、保存——保存文件为指定的格式

4、复制,剪切,全选,清空

5、粘贴与撤回

6、颜色与字体

7、打印,预览,页面设置

7、Form窗口背景图片设置

三、心得

 

 

一、程序介绍

首先映入眼帘的是一个春意盎然的一个编辑界面,阳光和绿叶是最搭的啊!

 

菜单内容

 

二、几个板块的详细解释(附关键功能的代码)

1、新建基本菜单栏以及文本框

(1)新建c#窗体应用程序,找到新建的form1(一个原始简单的窗口,name默认是form1),可以打开【视图】—>【工具箱】,在公共空间中找到RichTextBox空间,拖到form1窗口中,这时新建的窗口支持多行文本输入等功能。

(2)同理,在工具栏找到【菜单和工具栏】,这个类型有,指针,contextmenustrip,menustrip,statusstrip,toolstrip以及toolstripcontainer几个空进啊可以选择,在这里我们选择menustrip这个空间生成一个菜单栏(显示文件,编辑,格式,帮助),自定义二级菜单,每一个菜单选项都可以右键设置分隔线(【右键】-【插入】-【separator】)和图标(【右键】-【设置图像】)。接下来,我们选择一个toolstrip控件拖到刚刚新建的menustrip下面,制作图片快捷菜单,相关设置和menustrip菜单相同,打开【视图】-【属性设置】,点击toolstrip菜单中的每一个子菜单,设置它的text属性为它所承载的功能,

(3)新建右键菜单,即在richtextbox中编辑文字时右键显示出来的菜单:在控件中拖一个contextmenustrip到richtextbox中,类似的方法设置一级,二级菜单一级图标。

设置完上述三步之后就可以得到我们文章开篇那个“绿色编辑器的主界面”了。作者的程序相关子菜单设置如下:

2.打开——从电脑上传文件,使文件内容显示在文本框上面

代码展示,在双击保存的图标或文字时会自动调转到该板块代码的编辑位置(form.cs文件)

private void toolStripButton2_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "*.rtf|Paint text files (*.txt)|*.txt|Rich text files(*.rtf)|All files|*.*"; //设置可以支持打开的文件类型
            open.Title = "打开";
           
            if (open.ShowDialog() == DialogResult.OK)
            {
                string filePath = open.FileName;
                System.IO.StreamReader sReader = new System.IO.StreamReader(filePath, Encoding.Default);
                this.richTextBox1.Text = sReader.ReadToEnd();
                sReader.Dispose();
            }
            open.Dispose();
        }

3、保存——保存文件为指定的格式

 "Rich text files(*.rtf)|*.rtf|Paint text files (*.txt)|*.txt|All files|*.*"这个语句就是设置的支持的文件格式,和打开文件支持的格式相同。这里我多设置了一个提示窗口,提醒用户是否保存文档,其实如果else{}里面的代码就可以实现这个功能了,但是作者还抱着到处点一点玩一玩的心态多做了一点东西,多多益善嘛。

 

 private void toolStripButton4_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否保存?", "温馨提示", MessageBoxButtons.YesNoCancel,
                 MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
            {
                SaveFileDialog save = new SaveFileDialog();
                save.Title = "保存文件";
                save.Filter = "Rich text files(*.rtf)|*.rtf|Paint text files (*.txt)|*.txt|All files|*.*";
                if (save.ShowDialog() == DialogResult.OK)
                {
                    string filePath = save.FileName;
                    richTextBox1.SaveFile(filePath, RichTextBoxStreamType.PlainText);
                }
            }
            else
            {
                Close();
            }
        }

效果图如下,点击是之后可以选择路径保存文件。

4、复制,剪切,全选,清空

开始做的时候再网上找到过一些例子,但是代码都是5,6行往上,后来发现可以直接,调用,所以这四个功能都只需一条代码

       private void toolStripButton7_Click(object sender, EventArgs e)
        {
            richTextBox1.Copy();//复制
        }

        private void toolStripButton10_Click(object sender, EventArgs e)
        {
            richTextBox1.Cut();//剪切
        }

        private void toolStripButton11_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectAll();//全选
        }

        private void toolStripButton8_Click(object sender, EventArgs e)
        {
            richTextBox1.Clear();//清空
        }

      }

5、粘贴与撤回

        private void toolStripButton9_Click(object sender, EventArgs e)//粘贴
        {
            this.Cursor = Cursors.WaitCursor;
            this.richTextBox1.Paste();
            this.Cursor = Cursors.Default;
        }
        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            if (richTextBox1.CanUndo)
            {
                richTextBox1.Undo();//撤回
            }
        }

6、颜色与字体

  private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (fontDialog1.ShowDialog() == DialogResult.OK)
                richTextBox1.SelectionFont = fontDialog1.Font;
        }
  
   private void 颜色ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
                richTextBox1.BackColor = colorDialog1.Color;
        }

7、打印,预览,页面设置

步骤如下:

  1. 添加控件:添加一个printdialog组件printdialog1(默认的),一个printdocument组件printdocument1,一个pagesetupdialog组件pagesetupdialog1,一个printpreviewdialog组件printpreviewdialog1。
  2. 添加控制事件:为printdocument组件添加printpage时间,用来打印richtextbox1控件内容,代码如下:
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            Graphics g = e.Graphics;
            string[] str = richTextBox1.Text.Split('\n');
            int i = 0;
            foreach (string s in str)
            {
                g.DrawString(str[i], fontDialog1.Font, new SolidBrush(richTextBox1.ForeColor),
                    new PointF(100, 80 + richTextBox1.Font.Height * i));
                i++;
            }
        }

双击设置“打印”功能的图标或者文字,输入代码如下:

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            printDialog1.Document = printDocument1;
            if (printDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    printDocument1.Print();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "打印出错");
                }
            }
        }

双击设置“页面设置”功能的位置,在其中设置代码为:

        private void toolStripButton1_Click_2(object sender, EventArgs e)
        {
            pageSetupDialog1.Document = printDocument1;
            try
            {
                pageSetupDialog1.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "打印出错");
            }
        }

双击“预览”功能的位置,在其中设置代码为:

  private void toolStripButton12_Click(object sender, EventArgs e)
        {
            printPreviewDialog1.Document = printDocument1;
            try
            {
                printPreviewDialog1.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "打印出错");
            }
        }

 

  1. 3.设置上述代码完成后即可运行(Ctrl+F5)测试功能。

7、Form窗口背景图片设置

【form】—【属性】—【backgroundimage】可以自定义导入图片

 

三、心得

  1. 敲代码的时候可以仔细一点,我在敲打印这个功能的时候,fontDialog.Front的时候一直报错,最后发现是输入法的错,于是,写代码也是很需要耐心的
  2. 安利一个很好用的网站,有海量的图片,支持多种格式(包括icon,)是一个国外的网站,界面很简洁,免费的。https://www.easyicon.net/
  3. 最后给大家留下这张照片吧(我觉得还是很有feel的照片),感谢阅读,

 

 

 

 

 

 

 

 

  • 16
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是 MFC 多文档实现复制粘贴、撤销、重做、剪切全选删除功能的代码示例: 复制: ```cpp // 获取当前视图 CMyView* pView = (CMyView*)GetActiveView(); if (!pView) return; // 获取文档 CMyDoc* pDoc = pView->GetDocument(); if (!pDoc) return; // 获取选中区域 CMFCRibbonRichEditCtrl& editCtrl = pView->GetEditCtrl(); editCtrl.Copy(); ``` 粘贴: ```cpp // 获取当前视图 CMyView* pView = (CMyView*)GetActiveView(); if (!pView) return; // 获取文档 CMyDoc* pDoc = pView->GetDocument(); if (!pDoc) return; // 获取选中区域 CMFCRibbonRichEditCtrl& editCtrl = pView->GetEditCtrl(); editCtrl.Paste(); ``` 撤销: ```cpp // 获取当前视图 CMyView* pView = (CMyView*)GetActiveView(); if (!pView) return; // 获取文档 CMyDoc* pDoc = pView->GetDocument(); if (!pDoc) return; // 撤销 CMFCRibbonRichEditCtrl& editCtrl = pView->GetEditCtrl(); editCtrl.Undo(); ``` 重做: ```cpp // 获取当前视图 CMyView* pView = (CMyView*)GetActiveView(); if (!pView) return; // 获取文档 CMyDoc* pDoc = pView->GetDocument(); if (!pDoc) return; // 重做 CMFCRibbonRichEditCtrl& editCtrl = pView->GetEditCtrl(); editCtrl.Redo(); ``` 剪切: ```cpp // 获取当前视图 CMyView* pView = (CMyView*)GetActiveView(); if (!pView) return; // 获取文档 CMyDoc* pDoc = pView->GetDocument(); if (!pDoc) return; // 剪切 CMFCRibbonRichEditCtrl& editCtrl = pView->GetEditCtrl(); editCtrl.Cut(); ``` 全选: ```cpp // 获取当前视图 CMyView* pView = (CMyView*)GetActiveView(); if (!pView) return; // 获取文档 CMyDoc* pDoc = pView->GetDocument(); if (!pDoc) return; // 全选 CMFCRibbonRichEditCtrl& editCtrl = pView->GetEditCtrl(); editCtrl.SetSel(0, -1); ``` 删除: ```cpp // 获取当前视图 CMyView* pView = (CMyView*)GetActiveView(); if (!pView) return; // 获取文档 CMyDoc* pDoc = pView->GetDocument(); if (!pDoc) return; // 删除 CMFCRibbonRichEditCtrl& editCtrl = pView->GetEditCtrl(); editCtrl.ReplaceSel(_T("")); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值