C# WinForm GUI之控件
Button控件
派生于ButtonBase,单击或Enter键触发Click事件。
常用属性:
- Text标题、
- Name名称、
- Image图片、
- Location位置。
Label控件与LinkLabel控件
- Label控件即标签控件,显示描述性文字和信息。
常用属性与事件:
Text显示文字;
- TextAlign文本在控件中位置(需设置AutoSize为true);
- AutoSize是否根据文本自动调整控件大小,默认true;
- BorderStyle边框样式,0-Name无边框,1-FixeSingle单线边框,2-Fixed3D表示3D样式边框;
- Click、DoubleClick事件;
- MouseHover鼠标悬停事件。
- LinkLabel为带链接的标签,可创建Web样式的链接,可将部分文本指向某个对象或Web页链接。
常用属性与事件:
- LinkArea获取或设置超链接文本区域;
- LinkColor设置未访问超链接前默认颜色;
- LinkVisited链接是否显示为如同被访问过;
- LinkVisitedColor,LinkVisited为true时设置超链接颜色;
- ActiveLinkColor单击时超链接颜色;
- LinkClicked单击时触发事件。
TextBox控件
用于可编辑文本(可设置只读),可显示多行,可对文本换行使其符合控件大小。其中所有文本使用一种格式。
常用属性与事件:
- Text文本内容,textBox1.Text = “文本内容”;
- MaxLength最大长度,默认0受系统内存限制,textBox1.MaxLength= 1024;
- MultiLine是否多行,默认false;
- ScollBars是否有垂直或水平滚动条,0-None无滚动条,1-Horizontal水平滚动条,2-Vertical垂直滚动条,3-Both都有;
- PasswordChar密码字符显示,textBox1.PasswordChar= “*”;
- SelectedText返回文本框选择的文本;
- ReadOnly是否只读;
- TextChanged事件,文本框内容改变触发;
- KeyPress,用户按键和松开键时触发;
- KeyUp,松开键时触发。
PictureBox控件
显示图形(来自位图、图标、元文件、增强元文件、jpeg、gif等),图像大会裁剪显示。
Image设置显示图片的图片框。
pictureBox1.Image = System.Drawing.Bitmap.FormFile("C:/bj.jpg");
Timer控件
时钟控件,计时器控件,主要用于计时。
常用属性与事件:
- Enable是否启用;
- Interval计时间隔,0~64767ms;
- Tick事件,Enable为true,Interval间隔定时触发Tick事件。
容器类控件
GoupBox(框架控件)常用属性Text,Visible。
Panel(面板控件)派生于ScrollableControl,常用属性:
- AutoScroll,默认true可滚动容器中控件。
- BorderStyle是否显示边框,默认None无边框,FixedSingle单实线,Fixed3D为3D边框
RadioButton控件
单选按钮,从多个选项中选择一项。
常用属性与事件:
- Checked是否选中;
- CheckedChanged选择触发事件。
Form1.Designer.cs
namespace test7_3
{
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.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.radioButton1 = new System.Windows.Forms.RadioButton();
this.radioButton2 = new System.Windows.Forms.RadioButton();
this.radioButton3 = new System.Windows.Forms.RadioButton();
this.radioButton4 = new System.Windows.Forms.RadioButton();
this.radioButton5 = new System.Windows.Forms.RadioButton();
this.radioButton6 = new System.Windows.Forms.RadioButton();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(260, 87);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(337, 15);
this.label1.TabIndex = 0;
this.label1.Text = "说明:中午选菜单程序,荤素搭配,吃吃更健康。";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(168, 364);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(55, 15);
this.label2.TabIndex = 1;
this.label2.Text = "label2";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(554, 374);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(55, 15);
this.label3.TabIndex = 2;
this.label3.Text = "label3";
//
// groupBox1
//
this.groupBox1.Controls.Add(this.radioButton3);
this.groupBox1.Controls.Add(this.radioButton2);
this.groupBox1.Controls.Add(this.radioButton1);
this.groupBox1.Location = new System.Drawing.Point(148, 138);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(217, 179);
this.groupBox1.TabIndex = 3;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "素菜";
//
// groupBox2
//
this.groupBox2.Controls.Add(this.radioButton6);
this.groupBox2.Controls.Add(this.radioButton5);
this.groupBox2.Controls.Add(this.radioButton4);
this.groupBox2.Location = new System.Drawing.Point(486, 147);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(225, 170);
this.groupBox2.TabIndex = 4;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "荤菜";
//
// radioButton1
//
this.radioButton1.AutoSize = true;
this.radioButton1.Location = new System.Drawing.Point(23, 36);
this.radioButton1.Name = "radioButton1";
this.radioButton1.Size = new System.Drawing.Size(73, 19);
this.radioButton1.TabIndex = 0;
this.radioButton1.TabStop = true;
this.radioButton1.Text = "大白菜";
this.radioButton1.UseVisualStyleBackColor = true;
this.radioButton1.CheckedChanged += new System.EventHandler(this.gb1_CheckedChanged);
//
// radioButton2
//
this.radioButton2.AutoSize = true;
this.radioButton2.Location = new System.Drawing.Point(23, 72);
this.radioButton2.Name = "radioButton2";
this.radioButton2.Size = new System.Drawing.Size(103, 19);
this.radioButton2.TabIndex = 1;
this.radioButton2.TabStop = true;
this.radioButton2.Text = "小葱拌豆腐";
this.radioButton2.UseVisualStyleBackColor = true;
this.radioButton2.CheckedChanged += new System.EventHandler(this.gb1_CheckedChanged);
//
// radioButton3
//
this.radioButton3.AutoSize = true;
this.radioButton3.Location = new System.Drawing.Point(23, 109);
this.radioButton3.Name = "radioButton3";
this.radioButton3.Size = new System.Drawing.Size(88, 19);
this.radioButton3.TabIndex = 2;
this.radioButton3.TabStop = true;
this.radioButton3.Text = "清水白菜";
this.radioButton3.UseVisualStyleBackColor = true;
this.radioButton3.CheckedChanged += new System.EventHandler(this.gb1_CheckedChanged);
//
// radioButton4
//
this.radioButton4.AutoSize = true;
this.radioButton4.Location = new System.Drawing.Point(49, 27);
this.radioButton4.Name = "radioButton4";
this.radioButton4.Size = new System.Drawing.Size(73, 19);
this.radioButton4.TabIndex = 0;
this.radioButton4.TabStop = true;
this.radioButton4.Text = "白切鸡";
this.radioButton4.UseVisualStyleBackColor = true;
this.radioButton4.CheckedChanged += new System.EventHandler(this.gb2_CheckedChanged);
//
// radioButton5
//
this.radioButton5.AutoSize = true;
this.radioButton5.Location = new System.Drawing.Point(49, 62);
this.radioButton5.Name = "radioButton5";
this.radioButton5.Size = new System.Drawing.Size(73, 19);
this.radioButton5.TabIndex = 1;
this.radioButton5.TabStop = true;
this.radioButton5.Text = "焖猪蹄";
this.radioButton5.UseVisualStyleBackColor = true;
this.radioButton5.CheckedChanged += new System.EventHandler(this.gb2_CheckedChanged);
//
// radioButton6
//
this.radioButton6.AutoSize = true;
this.radioButton6.Location = new System.Drawing.Point(49, 100);
this.radioButton6.Name = "radioButton6";
this.radioButton6.Size = new System.Drawing.Size(73, 19);
this.radioButton6.TabIndex = 2;
this.radioButton6.TabStop = true;
this.radioButton6.Text = "蒸排骨";
this.radioButton6.UseVisualStyleBackColor = true;
this.radioButton6.CheckedChanged += new System.EventHandler(this.gb2_CheckedChanged);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.groupBox2.ResumeLayout(false);
this.groupBox2.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.RadioButton radioButton3;
private System.Windows.Forms.RadioButton radioButton2;
private System.Windows.Forms.RadioButton radioButton1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.RadioButton radioButton6;
private System.Windows.Forms.RadioButton radioButton5;
private System.Windows.Forms.RadioButton radioButton4;
}
}
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace test7_3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void gb1_CheckedChanged(object sender, EventArgs e)
{
RadioButton rb = (RadioButton)sender;
if (rb.Checked)
label2.Text = "你选择的素菜是:" + rb.Text;
}
private void gb2_CheckedChanged(object sender, EventArgs e)
{
RadioButton rb = (RadioButton)sender;
if (rb.Checked)
label3.Text = "你选择的荤菜是:" + rb.Text;
}
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace test7_3
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
csc *.cs -t:winexe
CheckBox控件
复选框控件,选择一项或多项。
Form1.Designer.cs
namespace test7_4
{
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.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.checkBox2 = new System.Windows.Forms.CheckBox();
this.checkBox3 = new System.Windows.Forms.CheckBox();
this.checkBox4 = new System.Windows.Forms.CheckBox();
this.checkBox5 = new System.Windows.Forms.CheckBox();
this.checkBox6 = new System.Windows.Forms.CheckBox();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(237, 73);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(142, 15);
this.label1.TabIndex = 0;
this.label1.Text = "选择你感兴趣的板块";
//
// button1
//
this.button1.Location = new System.Drawing.Point(289, 318);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "确定";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// checkBox1
//
this.checkBox1.AutoSize = true;
this.checkBox1.Location = new System.Drawing.Point(230, 142);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(59, 19);
this.checkBox1.TabIndex = 2;
this.checkBox1.Text = "要闻";
this.checkBox1.UseVisualStyleBackColor = true;
//
// checkBox2
//
this.checkBox2.AutoSize = true;
this.checkBox2.Location = new System.Drawing.Point(230, 180);
this.checkBox2.Name = "checkBox2";
this.checkBox2.Size = new System.Drawing.Size(59, 19);
this.checkBox2.TabIndex = 3;
this.checkBox2.Text = "体育";
this.checkBox2.UseVisualStyleBackColor = true;
//
// checkBox3
//
this.checkBox3.AutoSize = true;
this.checkBox3.Location = new System.Drawing.Point(230, 234);
this.checkBox3.Name = "checkBox3";
this.checkBox3.Size = new System.Drawing.Size(59, 19);
this.checkBox3.TabIndex = 4;
this.checkBox3.Text = "娱乐";
this.checkBox3.UseVisualStyleBackColor = true;
//
// checkBox4
//
this.checkBox4.AutoSize = true;
this.checkBox4.Location = new System.Drawing.Point(351, 142);
this.checkBox4.Name = "checkBox4";
this.checkBox4.Size = new System.Drawing.Size(59, 19);
this.checkBox4.TabIndex = 5;
this.checkBox4.Text = "教育";
this.checkBox4.UseVisualStyleBackColor = true;
//
// checkBox5
//
this.checkBox5.AutoSize = true;
this.checkBox5.Location = new System.Drawing.Point(351, 180);
this.checkBox5.Name = "checkBox5";
this.checkBox5.Size = new System.Drawing.Size(59, 19);
this.checkBox5.TabIndex = 6;
this.checkBox5.Text = "军事";
this.checkBox5.UseVisualStyleBackColor = true;
//
// checkBox6
//
this.checkBox6.AutoSize = true;
this.checkBox6.Location = new System.Drawing.Point(351, 234);
this.checkBox6.Name = "checkBox6";
this.checkBox6.Size = new System.Drawing.Size(59, 19);
this.checkBox6.TabIndex = 7;
this.checkBox6.Text = "汽车";
this.checkBox6.UseVisualStyleBackColor = true;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.checkBox6);
this.Controls.Add(this.checkBox5);
this.Controls.Add(this.checkBox4);
this.Controls.Add(this.checkBox3);
this.Controls.Add(this.checkBox2);
this.Controls.Add(this.checkBox1);
this.Controls.Add(this.button1);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.CheckBox checkBox2;
private System.Windows.Forms.CheckBox checkBox3;
private System.Windows.Forms.CheckBox checkBox4;
private System.Windows.Forms.CheckBox checkBox5;
private System.Windows.Forms.CheckBox checkBox6;
}
}
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace test7_4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e){
string mk = "";
CheckBox[] array = { checkBox1,checkBox2,checkBox3,checkBox4,checkBox5,checkBox6};
for(int i = 0; i < array.Length; i++){
if(array[i].Checked)
mk += array[i].Text+",";
}
MessageBox.Show("选择你感兴趣的板块:" + mk);
}
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace test7_4
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
csc *.cs -t:winexe
MenuStrip控件
菜单基本作用:
- 人机对话接口,方便用户选择应用程序的各种功能;
- 管理应用程序,操作各种功能模块。
-
MenuStrip
下拉式菜单,单击菜单下拉出子菜单,选择命令执行相关操作。 -
ContextMenuStrip
右键菜单或弹出式菜单。
常用属性与事件:
- ShortCutKeys快捷键;
- ShowShortcutKeys是否显示菜单项快捷键;
- Checked判断返回菜单是否被选中;
- Click单击事件。
菜单栏单击鼠标右键,选择“编辑DropDownItems…”命令,弹出“项集合编辑器”对话框,可设置菜单项快捷键等属性。
RichTextBox控件,设置Dock属性为Fill,可填满整个窗体。
ContextMenuStrip菜单的Click事件属性可选择MenuStrip菜单的事件,实现复用。
对话框设计
通用预定义对话框:
- OpenFileDialog
- SaveFileDialog
- ColorDialog
- FontDialog
- PageSetupDialog打印页面设置
- PrintDialog打印机设置
- PrintPreviewDialog打印预览
- OpenFileDialog
从磁盘打开一个或多个文件。
常用属性与方法:
- Title对话框标题,默认“打开”;
- FileName获取或设置第一个对话框显示文件或用户选择的最后一个文件,OpenFileDialog.FileName = “test.txt”;
- Filter文件格式筛选器,限定文件类型,数显隔开,OpenFileDialog.Filter= “doc文档|*.doc|所有文档|*.*”;
- IntiaDirectiory初始目录,OpenFileDialog.IntiaDirectiory= “d:\”;
- MultiseLect是否多选文件;
- ShowDialog显示对话框,OpenFileDialog.ShowDialog()。
- SaveFileDialog
常用属性:
- CreatePrompt保存文件不存在时,询问是否创建;
- OverwritePromptt保存文件存在时,询问是否覆盖。
- FontDialog
常用属性:
- Font字体
- Color字体颜色
Form1.Designer.cs
namespace _5test
{
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.components = new System.ComponentModel.Container();
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.文件ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.新建ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.打开ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.保存ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
this.打印ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator();
this.退出ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.编辑ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.复制ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.剪切ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.粘贴ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripSeparator3 = new System.Windows.Forms.ToolStripSeparator();
this.字体ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.帮助ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.版本ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
this.复制ToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
this.剪切ToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
this.粘贴ToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
this.退出ToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
this.printDocument1 = new System.Drawing.Printing.PrintDocument();
this.menuStrip1.SuspendLayout();
this.contextMenuStrip1.SuspendLayout();
this.SuspendLayout();
//
// menuStrip1
//
this.menuStrip1.ImageScalingSize = new System.Drawing.Size(20, 20);
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.文件ToolStripMenuItem,
this.编辑ToolStripMenuItem,
this.帮助ToolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(582, 28);
this.menuStrip1.TabIndex = 0;
this.menuStrip1.Text = "menuStrip1";
//
// 文件ToolStripMenuItem
//
this.文件ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.新建ToolStripMenuItem,
this.打开ToolStripMenuItem,
this.保存ToolStripMenuItem,
this.toolStripSeparator1,
this.打印ToolStripMenuItem,
this.toolStripSeparator2,
this.退出ToolStripMenuItem});
this.文件ToolStripMenuItem.Name = "文件ToolStripMenuItem";
this.文件ToolStripMenuItem.Size = new System.Drawing.Size(53, 24);
this.文件ToolStripMenuItem.Text = "文件";
//
// 新建ToolStripMenuItem
//
this.新建ToolStripMenuItem.Name = "新建ToolStripMenuItem";
this.新建ToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.N)));
this.新建ToolStripMenuItem.Size = new System.Drawing.Size(224, 26);
this.新建ToolStripMenuItem.Text = "新建";
this.新建ToolStripMenuItem.Click += new System.EventHandler(this.新建ToolStripMenuItem_Click);
//
// 打开ToolStripMenuItem
//
this.打开ToolStripMenuItem.Name = "打开ToolStripMenuItem";
this.打开ToolStripMenuItem.Size = new System.Drawing.Size(224, 26);
this.打开ToolStripMenuItem.Text = "打开";
this.打开ToolStripMenuItem.Click += new System.EventHandler(this.打开ToolStripMenuItem_Click);
//
// 保存ToolStripMenuItem
//
this.保存ToolStripMenuItem.Name = "保存ToolStripMenuItem";
this.保存ToolStripMenuItem.Size = new System.Drawing.Size(224, 26);
this.保存ToolStripMenuItem.Text = "保存";
this.保存ToolStripMenuItem.Click += new System.EventHandler(this.保存ToolStripMenuItem_Click);
//
// toolStripSeparator1
//
this.toolStripSeparator1.Name = "toolStripSeparator1";
this.toolStripSeparator1.Size = new System.Drawing.Size(221, 6);
//
// 打印ToolStripMenuItem
//
this.打印ToolStripMenuItem.Name = "打印ToolStripMenuItem";
this.打印ToolStripMenuItem.Size = new System.Drawing.Size(224, 26);
this.打印ToolStripMenuItem.Text = "打印";
this.打印ToolStripMenuItem.Click += new System.EventHandler(this.打印ToolStripMenuItem_Click);
//
// toolStripSeparator2
//
this.toolStripSeparator2.Name = "toolStripSeparator2";
this.toolStripSeparator2.Size = new System.Drawing.Size(221, 6);
//
// 退出ToolStripMenuItem
//
this.退出ToolStripMenuItem.Name = "退出ToolStripMenuItem";
this.退出ToolStripMenuItem.Size = new System.Drawing.Size(224, 26);
this.退出ToolStripMenuItem.Text = "退出";
this.退出ToolStripMenuItem.Click += new System.EventHandler(this.退出ToolStripMenuItem_Click);
//
// 编辑ToolStripMenuItem
//
this.编辑ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.复制ToolStripMenuItem,
this.剪切ToolStripMenuItem,
this.粘贴ToolStripMenuItem,
this.toolStripSeparator3,
this.字体ToolStripMenuItem});
this.编辑ToolStripMenuItem.Name = "编辑ToolStripMenuItem";
this.编辑ToolStripMenuItem.Size = new System.Drawing.Size(53, 24);
this.编辑ToolStripMenuItem.Text = "编辑";
//
// 复制ToolStripMenuItem
//
this.复制ToolStripMenuItem.Name = "复制ToolStripMenuItem";
this.复制ToolStripMenuItem.Size = new System.Drawing.Size(224, 26);
this.复制ToolStripMenuItem.Text = "复制";
this.复制ToolStripMenuItem.Click += new System.EventHandler(this.复制ToolStripMenuItem_Click);
//
// 剪切ToolStripMenuItem
//
this.剪切ToolStripMenuItem.Name = "剪切ToolStripMenuItem";
this.剪切ToolStripMenuItem.Size = new System.Drawing.Size(224, 26);
this.剪切ToolStripMenuItem.Text = "剪切";
this.剪切ToolStripMenuItem.Click += new System.EventHandler(this.剪切ToolStripMenuItem_Click);
//
// 粘贴ToolStripMenuItem
//
this.粘贴ToolStripMenuItem.Name = "粘贴ToolStripMenuItem";
this.粘贴ToolStripMenuItem.Size = new System.Drawing.Size(224, 26);
this.粘贴ToolStripMenuItem.Text = "粘贴";
this.粘贴ToolStripMenuItem.Click += new System.EventHandler(this.粘贴ToolStripMenuItem_Click);
//
// toolStripSeparator3
//
this.toolStripSeparator3.Name = "toolStripSeparator3";
this.toolStripSeparator3.Size = new System.Drawing.Size(221, 6);
//
// 字体ToolStripMenuItem
//
this.字体ToolStripMenuItem.Name = "字体ToolStripMenuItem";
this.字体ToolStripMenuItem.Size = new System.Drawing.Size(224, 26);
this.字体ToolStripMenuItem.Text = "字体";
this.字体ToolStripMenuItem.Click += new System.EventHandler(this.字体ToolStripMenuItem_Click);
//
// 帮助ToolStripMenuItem
//
this.帮助ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.版本ToolStripMenuItem});
this.帮助ToolStripMenuItem.Name = "帮助ToolStripMenuItem";
this.帮助ToolStripMenuItem.Size = new System.Drawing.Size(53, 24);
this.帮助ToolStripMenuItem.Text = "帮助";
//
// 版本ToolStripMenuItem
//
this.版本ToolStripMenuItem.Name = "版本ToolStripMenuItem";
this.版本ToolStripMenuItem.Size = new System.Drawing.Size(224, 26);
this.版本ToolStripMenuItem.Text = "版本";
this.版本ToolStripMenuItem.Click += new System.EventHandler(this.版本ToolStripMenuItem_Click);
//
// richTextBox1
//
this.richTextBox1.ContextMenuStrip = this.contextMenuStrip1;
this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.richTextBox1.Location = new System.Drawing.Point(0, 28);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(582, 405);
this.richTextBox1.TabIndex = 1;
this.richTextBox1.Text = "";
//
// contextMenuStrip1
//
this.contextMenuStrip1.ImageScalingSize = new System.Drawing.Size(20, 20);
this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.复制ToolStripMenuItem1,
this.剪切ToolStripMenuItem1,
this.粘贴ToolStripMenuItem1,
this.退出ToolStripMenuItem1});
this.contextMenuStrip1.Name = "contextMenuStrip1";
this.contextMenuStrip1.Size = new System.Drawing.Size(109, 100);
//
// 复制ToolStripMenuItem1
//
this.复制ToolStripMenuItem1.Name = "复制ToolStripMenuItem1";
this.复制ToolStripMenuItem1.Size = new System.Drawing.Size(210, 24);
this.复制ToolStripMenuItem1.Text = "复制";
this.复制ToolStripMenuItem1.Click += new System.EventHandler(this.复制ToolStripMenuItem_Click);
//
// 剪切ToolStripMenuItem1
//
this.剪切ToolStripMenuItem1.Name = "剪切ToolStripMenuItem1";
this.剪切ToolStripMenuItem1.Size = new System.Drawing.Size(210, 24);
this.剪切ToolStripMenuItem1.Text = "剪切";
this.剪切ToolStripMenuItem1.Click += new System.EventHandler(this.剪切ToolStripMenuItem_Click);
//
// 粘贴ToolStripMenuItem1
//
this.粘贴ToolStripMenuItem1.Name = "粘贴ToolStripMenuItem1";
this.粘贴ToolStripMenuItem1.Size = new System.Drawing.Size(210, 24);
this.粘贴ToolStripMenuItem1.Text = "粘贴";
this.粘贴ToolStripMenuItem1.Click += new System.EventHandler(this.粘贴ToolStripMenuItem_Click);
//
// 退出ToolStripMenuItem1
//
this.退出ToolStripMenuItem1.Name = "退出ToolStripMenuItem1";
this.退出ToolStripMenuItem1.Size = new System.Drawing.Size(210, 24);
this.退出ToolStripMenuItem1.Text = "退出";
this.退出ToolStripMenuItem1.Click += new System.EventHandler(this.退出ToolStripMenuItem_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(582, 433);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.menuStrip1);
this.MainMenuStrip = this.menuStrip1;
this.Name = "Form1";
this.Text = "Form1";
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
this.contextMenuStrip1.ResumeLayout(false);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.MenuStrip menuStrip1;
private System.Windows.Forms.ToolStripMenuItem 文件ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 新建ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 打开ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 保存ToolStripMenuItem;
private System.Windows.Forms.ToolStripSeparator toolStripSeparator1;
private System.Windows.Forms.ToolStripMenuItem 打印ToolStripMenuItem;
private System.Windows.Forms.ToolStripSeparator toolStripSeparator2;
private System.Windows.Forms.ToolStripMenuItem 退出ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 编辑ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 复制ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 剪切ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 粘贴ToolStripMenuItem;
private System.Windows.Forms.ToolStripSeparator toolStripSeparator3;
private System.Windows.Forms.ToolStripMenuItem 字体ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 帮助ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 版本ToolStripMenuItem;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
private System.Windows.Forms.ToolStripMenuItem 复制ToolStripMenuItem1;
private System.Windows.Forms.ToolStripMenuItem 剪切ToolStripMenuItem1;
private System.Windows.Forms.ToolStripMenuItem 粘贴ToolStripMenuItem1;
private System.Windows.Forms.ToolStripMenuItem 退出ToolStripMenuItem1;
private System.Drawing.Printing.PrintDocument printDocument1;
}
}
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _5test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("确定要新建吗", "新建提示", MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
{
richTextBox1.Clear();
this.Text = "新建文本";
}
}
private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Copy();
}
private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Cut();
}
private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Paste();
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("确定要退出吗","退出操作",MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
{
Application.Exit();
}
}
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = "C:\\";
openFileDialog.Filter = "rtf(*.rtf)|*.rtf|txt(*.txt)|*.txt|doc(*.doc)|*.doc";
openFileDialog.FilterIndex = 1;
openFileDialog.RestoreDirectory = true;
if(openFileDialog.ShowDialog() == DialogResult.OK)
{
try
{
richTextBox1.LoadFile(openFileDialog.FileName);
}catch(Exception ex)
{
MessageBox.Show(openFileDialog.FileName+ex.Message);
}
}
}
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.InitialDirectory = "C:\\";
saveFileDialog.Filter = "rtf(*.rtf)|*.rtf|txt(*.txt)|*.txt|doc(*.doc)|*.doc";
saveFileDialog.RestoreDirectory = true;
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
richTextBox1.SaveFile(saveFileDialog.FileName);
}
}
private void 打印ToolStripMenuItem_Click(object sender, EventArgs e)
{
PrintDialog printDialog = new PrintDialog();
printDialog.Document = printDocument1;
if (printDialog.ShowDialog() == DialogResult.OK)
{
printDocument1.Print();
}
}
private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
{
FontDialog fontDialog = new FontDialog();
fontDialog.ShowColor = true;
fontDialog.Font = richTextBox1.SelectionFont;
fontDialog.Color = richTextBox1.SelectionColor;
if (fontDialog.ShowDialog() == DialogResult.OK)
{
richTextBox1.SelectionFont = fontDialog.Font ;
richTextBox1.SelectionColor = fontDialog.Color ;
}
}
private void 版本ToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("version1.0,版权所有:作者","版本及版权信息");
}
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _5test
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
csc *.cs -t:winexe