一个类似于windows写字板的程序

  今天写了一个类似windows写字板的程序,使用了RichTextBox控件。拿出来共享一下,也以便以后学习。通过这几天的学习,发现要使用自定义事件的地方还是相当多的。

 界面设计代码:

namespace RichTextBoxTest
{
    partial class Form1
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.btnBold = new System.Windows.Forms.Button();
            this.btnItalic = new System.Windows.Forms.Button();
            this.btnUnderline = new System.Windows.Forms.Button();
            this.btnCenter = new System.Windows.Forms.Button();
            this.lblSize = new System.Windows.Forms.Label();
            this.txtSize = new System.Windows.Forms.TextBox();
            this.rtfText = new System.Windows.Forms.RichTextBox();
            this.btnLoad = new System.Windows.Forms.Button();
            this.btnSave = new System.Windows.Forms.Button();
            this.SuspendLayout();
            //
            // btnBold
            //
            this.btnBold.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.btnBold.Location = new System.Drawing.Point(136, 27);
            this.btnBold.Name = "btnBold";
            this.btnBold.Size = new System.Drawing.Size(75, 23);
            this.btnBold.TabIndex = 0;
            this.btnBold.Text = "Bold";
            this.btnBold.UseVisualStyleBackColor = true;
            this.btnBold.Click += new System.EventHandler(this.btnBold_Click);
            //
            // btnItalic
            //
            this.btnItalic.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.btnItalic.Location = new System.Drawing.Point(252, 27);
            this.btnItalic.Name = "btnItalic";
            this.btnItalic.Size = new System.Drawing.Size(75, 23);
            this.btnItalic.TabIndex = 1;
            this.btnItalic.Text = "Italic";
            this.btnItalic.UseVisualStyleBackColor = true;
            this.btnItalic.Click += new System.EventHandler(this.btnItalic_Click);
            //
            // btnUnderline
            //
            this.btnUnderline.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.btnUnderline.Location = new System.Drawing.Point(365, 26);
            this.btnUnderline.Name = "btnUnderline";
            this.btnUnderline.Size = new System.Drawing.Size(75, 23);
            this.btnUnderline.TabIndex = 2;
            this.btnUnderline.Text = "Underline";
            this.btnUnderline.UseVisualStyleBackColor = true;
            this.btnUnderline.Click += new System.EventHandler(this.btnUnderline_Click);
            //
            // btnCenter
            //
            this.btnCenter.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.btnCenter.Location = new System.Drawing.Point(483, 26);
            this.btnCenter.Name = "btnCenter";
            this.btnCenter.Size = new System.Drawing.Size(75, 23);
            this.btnCenter.TabIndex = 3;
            this.btnCenter.Text = "Center";
            this.btnCenter.UseVisualStyleBackColor = true;
            this.btnCenter.Click += new System.EventHandler(this.btnCenter_Click);
            //
            // lblSize
            //
            this.lblSize.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.lblSize.AutoSize = true;
            this.lblSize.Location = new System.Drawing.Point(208, 74);
            this.lblSize.Name = "lblSize";
            this.lblSize.Size = new System.Drawing.Size(47, 12);
            this.lblSize.TabIndex = 4;
            this.lblSize.Text = "lblSize";
            //
            // txtSize
            //
            this.txtSize.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.txtSize.Location = new System.Drawing.Point(261, 65);
            this.txtSize.Name = "txtSize";
            this.txtSize.Size = new System.Drawing.Size(38, 21);
            this.txtSize.TabIndex = 5;
            this.txtSize.Text = "10";
            //
            // rtfText
            //
            this.rtfText.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.rtfText.Location = new System.Drawing.Point(24, 101);
            this.rtfText.Name = "rtfText";
            this.rtfText.Size = new System.Drawing.Size(677, 283);
            this.rtfText.TabIndex = 6;
            this.rtfText.Text = "";
            //
            // btnLoad
            //
            this.btnLoad.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
            this.btnLoad.Location = new System.Drawing.Point(224, 420);
            this.btnLoad.Name = "btnLoad";
            this.btnLoad.Size = new System.Drawing.Size(75, 23);
            this.btnLoad.TabIndex = 7;
            this.btnLoad.Text = "Load";
            this.btnLoad.UseVisualStyleBackColor = true;
            this.btnLoad.Click += new System.EventHandler(this.btnLoad_Click);
            //
            // btnSave
            //
            this.btnSave.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
            this.btnSave.Location = new System.Drawing.Point(407, 420);
            this.btnSave.Name = "btnSave";
            this.btnSave.Size = new System.Drawing.Size(75, 23);
            this.btnSave.TabIndex = 8;
            this.btnSave.Text = "Save";
            this.btnSave.UseVisualStyleBackColor = true;
            this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(729, 476);
            this.Controls.Add(this.btnSave);
            this.Controls.Add(this.btnLoad);
            this.Controls.Add(this.rtfText);
            this.Controls.Add(this.txtSize);
            this.Controls.Add(this.lblSize);
            this.Controls.Add(this.btnCenter);
            this.Controls.Add(this.btnUnderline);
            this.Controls.Add(this.btnItalic);
            this.Controls.Add(this.btnBold);
            this.MinimumSize = new System.Drawing.Size(737, 510);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button btnBold;
        private System.Windows.Forms.Button btnItalic;
        private System.Windows.Forms.Button btnUnderline;
        private System.Windows.Forms.Button btnCenter;
        private System.Windows.Forms.Label lblSize;
        private System.Windows.Forms.TextBox txtSize;
        private System.Windows.Forms.RichTextBox rtfText;
        private System.Windows.Forms.Button btnLoad;
        private System.Windows.Forms.Button btnSave;
    }
}

实现代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace RichTextBoxTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
           

           //订阅事件处理程序
            this.txtSize.KeyPress +=
                new KeyPressEventHandler(txtSize_KeyPress);
            this.txtSize.Validating +=
                new CancelEventHandler(txtSize_Validating);
            this.rtfText.LinkClicked +=
                new LinkClickedEventHandler(rtfText_LinkClicked);

        }


        /// <summary>
        /// Bold按钮的响应事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBold_Click(object sender, EventArgs e)
        {
            Font oldFont;
            Font newFont;

            oldFont = this.rtfText.SelectionFont;

            if (oldFont.Bold)
            {
                newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Bold);//取消黑体格式
            }
            else
            {
                newFont = new Font(oldFont, oldFont.Style | FontStyle.Bold); //添加黑体格式
            }

            this.rtfText.SelectionFont = newFont;
            this.rtfText.Focus();
        }


        /// <summary>
        /// Italic按钮的响应事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnItalic_Click(object sender, EventArgs e)
        {
            Font oldFont;
            Font newFont;

            oldFont = this.rtfText.SelectionFont;

            if (oldFont.Italic)
            {
                newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Italic);
            }
            else
            {
                newFont = new Font(oldFont, oldFont.Style | FontStyle.Italic);
            }

            this.rtfText.SelectionFont = newFont;
            this.rtfText.Focus();
        }


        /// <summary>
        /// Underline的响应事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUnderline_Click(object sender, EventArgs e)
        {
            Font oldFont;
            Font newFont;

            oldFont = this.rtfText.SelectionFont;

            if (oldFont.Underline)
            {
                newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Underline);
            }
            else
            {
                newFont = new Font(oldFont, oldFont.Style | FontStyle.Underline);
            }

            this.rtfText.SelectionFont = newFont;
            this.rtfText.Focus();
        }

        /// <summary>
        /// Center的响应事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCenter_Click(object sender, EventArgs e)
        {
            if (this.rtfText.SelectionAlignment == HorizontalAlignment.Center)
            {
                this.rtfText.SelectionAlignment = HorizontalAlignment.Left;
            }
            else
            {
                this.rtfText.SelectionAlignment = HorizontalAlignment.Center;
            }

            this.rtfText.Focus();
        }


        /// <summary>
        /// 键盘的键按下的响应事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtSize_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar < 48 || e.KeyChar > 57)
                && e.KeyChar != 8 && e.KeyChar != 13)
            {
                e.Handled = true;
            }
            else if(e.KeyChar == 13)
            {
                TextBox tb = (TextBox)sender;

                if (tb.Text.Length > 0)
                {
                    ApplyTextSize(tb.Text);
                }
                e.Handled = true;
                rtfText.Focus();
            }
        }

        /// <summary>
        /// 字体设置响应事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtSize_Validating(object sender, CancelEventArgs e)
        {
            TextBox tb = (TextBox)sender;

            ApplyTextSize(tb.Text);
            this.rtfText.Focus();
        }


        /// <summary>
        /// 实现网页的链接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void rtfText_LinkClicked(object sender, LinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start(e.LinkText);
        }


        private void ApplyTextSize(string textSize)
        {
            float newSize = Convert.ToSingle(textSize);
            FontFamily currentFontFamily;
            Font newFont;

            currentFontFamily = this.rtfText.SelectionFont.FontFamily;
            newFont = new Font(currentFontFamily, newSize);

            this.rtfText.SelectionFont = newFont;

        }


        //加载文件
        private void btnLoad_Click(object sender, EventArgs e)
        {
            try
            {
                rtfText.LoadFile("../../Test.rtf");
            }
            catch(System.IO.FileNotFoundException)
            {
                MessageBox.Show("NO file to load yet");
            }
        }


        //保存文件 
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                rtfText.SaveFile("../../Test.rtf");
            }
            catch(System.Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }


    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值