
第1章 窗体与界面设计
● 菜单的使用
● 窗体的设计
● 对话框的使用
● MDI应用程序
1.1 菜单应用
我们经常需要对菜单进行操作,使菜单满足各种操作需求,使界面更加美观,软件更加人性化。本节主要介绍菜单的使用,通过以下实例,可以帮助用户实现菜单的创建、菜单事件的响应及一些特殊的菜单操作。
1.1.1 创建基本菜单
范例1-1 创建专业化菜单
实例位置:光盘\ch01\1-1
范例说明
About the Example
本范例讲解如何创建专业化的菜单,请注意菜单控件MenuStrip的使用。程序运行效果如图1-1和图1-2所示。

图1-1 基本菜单界面

图1-2 带级联的菜单界面
关键步骤
Key Steps
1.创建一个新的Windows工程。
2.把MenuStrip控件拖放到设计界面上,单击MenuStrip控件的Type Here文本区域,可以输入菜单项的名称,如&File,然后按下回车键。
3.在File下面的文本区域中输入如下内容,创建一个文件菜单。
&New &Open - &Font - &Save &Exit
创建完成后,菜单如图1-1所示,其中,“-”符号会自动变成菜单项之间的分隔线。
4.实际应用程序的菜单应该呈现级联样式,单击【Font】菜单项,在其右侧会出现子菜单。
可在其文本区域输入如下内容,创建一个关于字体的子菜单:
&Bold &Italic &Underline
最终效果如图1-2所示。
抛砖引玉
About the Guidance
可以按照上述创建过程创建多个主菜单和子菜单。
提示:
用户可以选择想要操作的菜单项,但由于菜单项没有制作相应的操作,所以,鼠标点击时没有任何反应。本书的范例1-2将详细讲解如何触发菜单的相应事件。
范例1-2 创建右键菜单
实例位置:光盘\ch01\1-2
范例说明
About the Example
本范例讲解如何创建右键菜单,重点请注意菜单控件ContextMenuStrip的使用。程序运行效果如图1-3和图1-4所示。

图1-3 程序设计界面

图1-4 程序运行界面
关键步骤
Key Steps
1.创建一个新的Windows工程。
2.把ContextMenuStrip控件拖放到设计界面上,Name属性设置为“cmMenu”。
3.单击ContextMenuStrip控件的Type Here文本区域,可以输入菜单项的名称,如&New,然后按下回车键。
4.创建完成后,菜单如图1-3所示。
5.添加RichTextBox控件,设置Name属性为“rchShow”。
6.设置Form和RichTextBox的ContextMenuStript属性为“cmMenu”。
7.双击ContextMenuStrip控件的菜单项,添加Open事件。
程序代码
Codes
private void miOpen_Click(object sender, EventArgs e) { OpenFileDialog dlgOpen = new OpenFileDialog(); dlgOpen.Filter = "文本文件(*.txt)|*.txt"; if (dlgOpen.ShowDialog() == DialogResult.OK) { this.rchShow.LoadFile(dlgOpen.FileName,RichTextBoxStreamType .PlainText); } }
范例1-3 在菜单中添加图标
实例位置:光盘\ch01\1-3
范例说明
About the Example
本范例实现的是为每个菜单项添加图标,使菜单更专业化,更像Office的应用程序菜单。程序运行效果如图1-5和图1-6所示。

图1-5 设置图标前的菜单界面

图1-6 设置图标后的菜单界面
关键步骤
Key Steps
1.创建一个新的Windows工程。
2.把MenuStrip控件拖放到设计界面上,创建【New】和【Open】菜单项,如图1-5所示。
3.单击【New】菜单项,把Name属性改为“miNew”,单击Image属性后面的图标,弹出选择资源对话框,可以挑选在菜单项上显示的图标,如图1-6所示。
4.【Open】菜单项的设置同上。
范例1-4 菜单事件的响应
实例位置:光盘\ch01\1-4
范例说明
About the Example
本范例是在前一例的基础上创建各个菜单项的响应事件,使用户单击菜单时能执行相应的操作。程序运行效果如图1-7和图1-8所示。

图1-7 基本菜单界面

图1-8 RichTextBox属性设置
关键步骤
Key Steps
1.创建一个新的Windows工程。
2.把MenuStrip控件拖放到设计界面上,按照前一例的方法创建菜单界面,如图1-7所示。
3.将RichTextBox控件拖放入设计界面,并在属性面板修改Name属性为“rchShow”,Dock属性改为“Fill”,如图1-8所示。
4.双击各个菜单项,会出现代码界面,并自动添加Click事件。输入代码,实现各个菜单功能。
5.输入所有菜单项的代码,运行程序,单击菜单即可实现相应功能。
程序代码
Codes
1.打开一个文件(双击【Open】菜单项)
private void miOpen_Click(object sender, EventArgs e) { try { //打开文件对话框 OpenFileDialog ofd = new OpenFileDialog(); //对话框标题 ofd.Title = "打开文件"; //设置打开文件类型 ofd.Filter = "TextDocument(*.rtf)|*.rtf"; if (ofd.ShowDialog() == DialogResult.OK) { //文本框显示文件内容 this.rchShow.LoadFile(ofd.FileName, RichTextBoxStreamType.PlainText); this.Text = ofd.FileName; } } catch(IOException ex) { MessageBox.Show(ex.Message); } }
2.设置字体为粗体(双击【Bold】菜单项)
private void miBold_Click(object sender, EventArgs e) { Font oldFont=this.rchShow.SelectionFont; Font newFont; if (oldFont.Bold) { newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Bold); } else { newFont = new Font(oldFont,oldFont.Style|FontStyle.Bold); } this.rchShow.SelectionFont = newFont; }
3.设置字体为斜体(双击【Italic】菜单项)
private void miItalic_Click(object sender, EventArgs e) { Font oldFont = this.rchShow.SelectionFont; Font newFont; if (oldFont.Italic) { newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Italic); } else { newFont = new Font(oldFont, oldFont.Style | FontStyle.Italic); } this.rchShow.SelectionFont = newFont; }
4.设置字体有下划线(双击【Underline】菜单项)
private void miUnderline_Click(object sender, EventArgs e) { Font oldFont = this.rchShow.SelectionFont; Font newFont; if (oldFont.Underline) { newFont = new Font(oldFont, oldFont.Style &~FontStyle.Underline); } else { newFont = new Font(oldFont, oldFont.Style | FontStyle.Underline); } this.rchShow.SelectionFont = newFont; }
5.退出程序(双击【Exit】菜单项)
private void miExit_Click(object sender, EventArgs e) { Application.Exit(); }
1.1.2 菜单的特殊操作
范例1-5 动态合并菜单
实例位置:光盘\ch01\1-5
范例说明
About the Example
开发过MDI窗体的读者可能知道,当MDI子窗体最大化时,子窗体和主窗体的菜单能够自动合并。本范例将实现这一个功能。程序运行效果如图1-9和图1-10所示。

图1-9 父窗体界面

图1-10 子窗体界面
关键步骤
Key Steps
1.创建一个新的Windows工程。
2.选择窗体,修改相应属性。注意将IsMdiContainer的属性修改为“True”。
3.添加两个菜单控件MenuStrip,添加如下菜单项,并且设置相应属性。
4.在项目上单击鼠标右键,选择【添加】→【添加新项目】命令,输入名称frmEditor,添加MDI子窗口。
程序代码
Codes
1.父窗体构造函数
public frmContainer() { InitializeComponent(); //显示孩子窗口 frmEditor child = new frmEditor(this); child.Show(); }
2.子窗口构造函数
public frmEditor(frmContainer parent) { InitializeComponent(); //设置子窗口的父窗口 this.MdiParent = parent; }
提示:
设置为MatchOnly的菜单项不能在菜单之间移动,但对于&File,两个菜单项的文本匹配,它们的菜单项是可以合并的。【File】菜单项是根据MergeIndex属性进行合并的。MergeIndex为MatchOnly的菜单项原地不动,其他菜单项的MergeIndex属性为“Insert”。
范例1-6 存放历史信息的菜单
实例位置:光盘\ch01\1-6
范例说明
About the Example
本范例将实现以下功能:打开一个新的对话框,可以在菜单下显示打开历史信息,并且安排它们的位置。程序运行效果如图1-11和图1-12所示。

图1-11 父窗体界面

图1-12 子窗体界面
关键步骤
Key Steps
1.创建一个新的Windows工程。
2.选择窗体,修改相应属性。注意将IsMdiContainer的属性修改为“True”。
3.添加菜单控件MenuStrip,在项目上单击鼠标右键,选择【添加】→【添加新项目】命令,输入名称FrmContainer,创建父窗体,输入名称frmChild,创建子窗体,如图1-11和图1-12所示。
程序代码
Codes
1.PEnd Sub双击frmContainer窗体,声明一个私有变量,用于统计子窗体的个数
private int count;
2.修改frmContainer的构造函数
public frmContainer() { InitializeComponent(); count = 1; frmChild newChild = new frmChild(this, count); newChild.Show(); }
3.双击【打开一个新的子窗口】菜单,输入代码
private void miNew_Click(object sender, EventArgs e) { frmChild newChild = new frmChild(this,++count); newChild.Show(); }
4.修改frmChild的构造函数
public frmChild(frmContainer parent,int count) { InitializeComponent(); this.MdiParent = parent; this.Text = "子窗口" + count.ToString(); }
范例1-7 显示和隐藏菜单
实例位置:光盘\ch01\1-7
范例说明
About the Example
本范例是通过菜单项的Checked和CheckOnClick属性实现菜单的显示和隐藏功能。程序运行效果如图1-13和图1-14所示。

图1-13 箭头形窗体运行界面

图1-14 箭头形窗体界面
关键步骤
Key Steps
1.创建一个新的Windows工程。
2.把MenuStrip控件拖放到设计界面上,并添加【操作】和【帮助】菜单,如图1-13所示。
3.给【操作】菜单添加【显示帮助菜单】菜单项,并且修改其属性CheckOnClick为“True”,修改属性Checked为“True”。如图1-14所示。
4.选择【显示帮助菜单】菜单的CheckedChanged事件,双击添加事件处理程序,以实现菜单的显示和隐藏功能。
程序代码
Codes
显示和隐藏菜单
private void miShowHelp_CheckedChanged(object sender, EventArgs e) { //参数sender传入的参数设置为ToolStripMenuItem控件 ToolStripMenuItem mi = sender as ToolStripMenuItem; miHelp.Visible = mi.Checked; }
范例1-8 显示菜单中的选中标记
实例位置:光盘\ch01\1-8
范例说明
About the Example
本范例实现用户自定义显示或隐藏菜单项。程序运行效果如图1-15和图1-16所示。

图1-15 窗体界面

图1-16 属性设置界面
关键步骤
Key Steps
1.创建一个新的Windows工程。
2.把MenuStrip控件拖放到设计界面上,并添加【Show Help Menu】和【Help】菜单,前一个菜单项的Name属性为“miShow”,后一个菜单项的Name属性为“miHelp”,如图1-15所示。
3.选中【Show Help Menu】菜单,设置属性Checked为“True”,CheckOnClick为“True”,CheckState为“Checked”。如图1-16所示。
4.选择【Show Help Menu】菜单的CheckedChanged事件,双击添加事件处理程序。
程序代码
Codes
private void miShow_CheckedChanged(object sender, EventArgs e) { ToolStripMenuItem tsmi = sender as ToolStripMenuItem; this.miHelp.Visible = tsmi.Checked; }
1.2 窗体应用
一个完善的界面不仅更加友好,而且可以使软件使用的效率得到极大的提高。本节主要介绍窗体的常用属性、设计方法及窗体的基本操作。
1.2.1 窗体的外观设置
范例1-9 设置窗体的标题栏
实例位置:光盘\ch01\1-9
范例说明
About the Example
本范例向用户展示如何自定义窗体标题和修改标题栏图标。程序运行效果如图1-17~图1-19所示。

图1-17 窗体界面

图1-19 窗体图片属性设置

图1-18 窗体标题属性设置
关键步骤
Key Steps
1.创建一个新的Windows工程。
2.单击窗体界面设计器,属性Text可显示窗体标题,属性Icon可显示窗体图标,如图1-17至图1-19所示。
注意:
1.应将窗体标题栏中的文字设置为有意义的文字,而且不是所有窗体都有标题栏的。
2.标题文字可以按以下几种情况命名。
(1)程序的名称:当窗体唯一时,可以用此种命名方式。
(2)窗口的目的:最常见的命名方式。
(3)窗体的名称:不要只用窗体的名称,而应该将其转换为具有意义的名称。
范例1-10 窗体的最大化和最小化
实例位置:光盘\ch01\1-10
范例说明
About the Example
本范例通过两种方式实现窗体的最大化和最小化:一种是为窗体添加最大化、最小化按钮,另一种是通过修改窗体属性来实现。程序运行效果如图1-20~图1-24所示。

图1-20 窗体界面

图1-24 最大和最小界面比例设置
关键步骤
Key Steps
1.创建一个新的Windows工程。
2.在默认情况下,最大化和最小化按钮是存在的,可以单击窗体,按照下表修改属性,去除最大化和最小化按钮。修改结果如图1-20和图1-21所示。如果不希望用户使用【关闭】按钮,可以将ControlBox的属性设置为“False”。但是,需要注意的是,如果ControlBox为False,最大化和最小化按钮也会被自动隐藏。如图1-22和图1-23所示。

图1-21 最大化和最小化属性设置

图1-22 窗体界面

图1-23 属性设置
3.用户也可以通过修改属性MinimumSize和MaximumSize来限制窗体最大化和最小化的大小。如图1-24所示。
范例1-11 设置窗体的外观和行为
实例位置:光盘\ch01\1-11
范例说明
About the Example
本范例通过设置FormBorderStyle属性修改窗体边框的外观和行为。程序运行效果如图1-25所示。

图1-25 窗体外观属性设置
关键步骤
Key Steps
1.创建一个新的Windows工程。
2.单击窗体,在属性窗口中有FormBorderStyle属性,包含7个属性值,分别是None、FixedSingle、Fixed3D、FixedDialog、Sizable、FixedToolWindow和SizableToolWindow。如图1-25所示。
3.设置每一个属性值,观察窗口的外观。
● None:窗体边框消失,标题栏也消失了,窗体不可以最大最小化,无法显示标题栏文本。
● FixedToolWindow:窗体的标题栏比正常显示要小,文字的字体也会变小,标题栏除标题栏文字外,只有一个【关闭】按钮。
● Sizable:最常用的窗体外观样式。
● FixedSingle:与Sizable类似。
● Fixed3D:窗体边框呈现三维立体外观。
● FixedDialog:标题栏的图标无法显示。
● SizeableToolWindows:与FixedToolWindows类似。
范例1-12 创建和卸载窗体
实例位置:光盘\ch01\1-12
范例说明
About the Example
本范例向用户展示了创建和卸载窗体的过程,并理解两种窗体模式:无模式窗体和模式窗体的不同。程序运行效果如图1-26所示。

图1-26 窗体界面
关键步骤
Key Steps
1.创建一个新的Windows工程。
2.在窗体界面上拖放Label和Button控件,实现图1-26所示的程序界面。
3.双击每个按钮访问Click事件,输入相应代码,实现创建和卸载窗体功能。
程序代码
Codes
1.创建窗体
Form newform;
2.“无模式窗体”标签下的“显示窗体”代码
private void btnOpen_Click(object sender, EventArgs e) { newform= new Form(); newform.MaximizeBox = false; newform.MinimizeBox = false; //显示窗体 newform.Show(); this.btnClose.Enabled = true; this.btnOpen.Enabled = false; }
3.“无模式窗体”标签下的“卸载窗体”代码
private void btnClose_Click(object sender, EventArgs e) { this.Close(); this.btnClose.Enabled = false; this.btnOpen.Enabled = true; }
4.“模式窗体”标签下的“显示窗体”代码
private void btnOpenDialog_Click(object sender, EventArgs e) { newform = new Form(); newform.MaximizeBox = false; newform.MinimizeBox = false; //显示窗口 newform.ShowDialog(); this.btnCloseDialog.Enabled = true; this.btnOpenDialog.Enabled = false; }
5.“模式窗体”标签下的“卸载窗体”代码
private void btnCloseDialog_Click(object sender, EventArgs e) { this.Close(); this.btnCloseDialog.Enabled = false; this.btnOpenDialog.Enabled = true; }
注意:
用户显示窗体有两种形式:模式窗体和无模式窗体。无模式窗体不会使其他窗体无法使用(无模式窗体的显示代码为Show()方法)。无模式窗体的典型例子就是Word中的“查找和替换”窗口。当此窗口可见时,仍然可以操作其他窗口。模式窗口则相反,它处于显示状态时,其他应用窗体都不可以使用,除非模式窗体关闭(模式窗体的显示代码为ShowDialog()方法)。模式窗体的常用应用是对话框,如Word的打印对话框。
范例1-13 显示和隐藏窗体
实例位置:光盘\ch01\1-13
范例说明
About the Example
本范例是向用户展示窗体的显示和隐藏过程,并理解使窗体“消失”的两种含义:隐藏窗体和卸载窗体。程序运行效果如图1-27所示。

图1-27 窗体界面
关键步骤
Key Steps
1.创建一个新的Windows工程。
2.在窗体界面上拖放Label和Button控件,实现图1-27所示的程序界面。
3.双击每个按钮访问Click事件,输入相应代码,实现显示和隐藏窗体功能。
程序代码
Codes
1.创建窗体代码
Form newform= new Form(); public Form1() { InitializeComponent(); this.btnShow.Enabled = true; this.btnUnShow.Enabled = false; }
2.【显示窗体】按钮代码
private void btnShow_Click(object sender, EventArgs e) { //显示窗体 newform.Visible = true; this.btnUnShow.Enabled = true; this.btnShow.Enabled = false; }
3.【隐藏窗体】按钮代码
private void btnUnShow_Click(object sender, EventArgs e) { //隐藏窗体 newform.Visible = false; this.btnShow.Enabled = true; this.btnUnShow.Enabled = false; }
注意:
此窗体“消失”有两种方式:第一种是隐藏,这时窗体不关闭也不释放资源,只是用户不可见。实现窗体隐藏的代码是Visible属性为“false”。被隐藏的窗体仍然驻留在内存中,并且窗体中的所有控件和变量都保持其值,当需要再次显示窗体时,只要Visible属性为“true”即可。第二种是卸载,这时窗体被关闭并释放所占资源,用户同样不可见。实现窗体卸载的代码是Close()方法。
范例1-14 指定窗体的初始位置
实例位置:光盘\ch01\1-14
范例说明
About the Example
本范例通过设置窗体的StartPosition属性来控制窗体第一次在屏幕上显示的位置。
关键步骤
Key Steps
1.创建一个新的Windows工程。
2.单击窗体,StartPosition属性有如下5个值,如表1-1所示。
表1-1 StartPosition属性

3.分别选择这5个值,观察运行程序在屏幕中的位置。
范例1-15 获取桌面大小
实例位置:光盘\ch01\1-15
范例说明
About the Example
本范例是通过读取C#的Screen对象相关属性,获取屏幕信息。程序运行效果如图1-28所示。

图1-28 程序界面
关键步骤
Key Steps
1.创建一个新的Windows工程。
2.拖放Label控件和Button控件,实现图1-28所示的程序界面。
3.双击【获取】按钮,输入代码,实现获取屏幕宽和高的信息。
程序代码
Codes
private void btnGet_Click(object sender, EventArgs e) { this.lblWidth.Text = Screen.PrimaryScreen.WorkingArea.Width.ToString(); this.lblHeight.Text = Screen.PrimaryScreen.WorkingArea.Height.ToString(); }
范例1-16 使窗体标题栏文字右对齐
实例位置:光盘\ch01\1-16
范例说明
About the Example
本范例设置窗体的RightToLeft属性,实现窗体标题的右对齐。程序运行效果如图1-29所示。

图1-29 标题栏设置
关键步骤
Key Steps
1.创建一个新的Windows工程。
2.单击窗体,修改RightToLef属性为“yes”,设置Text属性为“标题显示在右边”,如图1-29所示。
范例1-17 设置窗体的背景图片
实例位置:光盘\ch01\1-17
范例说明
About the Example
本范例设置窗体的BackGroundImage属性,将图像添加到窗体背景中。程序运行效果如图1-30和图1-31所示。

图1-30 窗体大小属性设置

图1-31 设置背景图片
关键步骤
Key Steps
1.创建一个新的Windows工程。
2.单击窗体,修改窗体大小为“600,500”,如图1-30所示。
3.修改BackGroundImage属性,单击 按钮,弹出选择资源对话框,单击“本地资源”单选按钮,如图1-31所示。
4.单击【导入】按钮,导入任意符合格式的图片文件。
5.返回选择资源对话框,单击【确定】按钮,图片便可以显示在窗体中。
范例1-18 随机更换主界面背景
实例位置:光盘\ch01\1-18
范例说明
About the Example
本范例是通过使用ImageList控件和Random函数实现窗体背景的随机变换。程序运行效果如图1-32所示。

图1-32 添加图片
关键步骤
Key Steps
1.创建一个新的Windows工程。
2.把ImageList控件拖放到设计界面上,选择属性Images,添加8张图片,如图1-32所示。
3.双击窗体,激发Load事件,输入代码,实现随机变换背景图片的功能。
程序代码
Codes
private void Form1_Load(object sender, EventArgs e) { //产生一个随机数 Random random = new Random(); int i = random.Next(imageList1.Images.Count); this.BackgroundImage=imageList1.Images[i]; }
范例1-19 带分割栏的窗体
实例位置:光盘\ch01\1-19
范例说明
About the Example
本范例实现一个带有分割栏的窗体。程序运行效果如图1-33和图1-34所示。

图1-33 窗体菜单界面

图1-34 窗体分割界面
关键步骤
Key Steps
1.创建一个新的Windows工程。
2.把Menu控件拖放到设计界面上,添加【File】菜单,如图1-33所示。
3.把splitContainer控件拖放到设计界面上,实现窗体的分割,如图1-34所示。
范例1-20 控件随窗体自动调整
实例位置:光盘\ch01\1-20
范例说明
About the Example
本范例讲解如何通过设置Anchor属性实现控件随着窗体自动调整。程序运行效果如图1-35和图1-36所示。

图1-35 窗体界面

图1-36 拉大窗体后界面
关键步骤
Key Steps
1.创建一个新的Windows工程。
2.把Button控件拖放到设计界面上,设置Name属性为“btnText”,设置Text属性为“控件随窗体自动调整”,设置Anchor属性为“Left,Right,Top,Button”,如图1-35所示。
3.运行程序,拉大窗体,按钮随之增大,效果如图1-36所示。
范例1-21 使用任意组件拖动窗体
实例位置:光盘\ch01\1-21
范例说明
About the Example
本范例通过按钮控件的MouseDown()方法和MouseMove()方法实现拖动窗体的功能。程序运行效果如图1-37所示。

图1-37 程序界面
关键步骤
Key Steps
1.创建一个新的Windows工程。
2.把Button控件拖放到设计界面上,设置Name属性为“btnDraw”,设置Text属性为“此按钮可以用来随意拖动窗体”,双击MouseDown和MouseMove事件,添加代码,如图1-37所示。
程序代码
Codes
1.定义两个变量分别存放鼠标产生按下事件时的(x,y)坐标
private int startX; private int startY;
2.鼠标按下时产生的事件
private void btnDraw_MouseDown(object sender, MouseEventArgs e) { //如果按的是鼠标左键 if (e.Button == MouseButtons.Left) { //获得鼠标在产生事件时的x坐标 startX = e.X; //获得鼠标在产生事件时的y坐标 startY = e.Y; } }
3.鼠标拖动时产生的事件
private void btnDraw_MouseMove(object sender, MouseEventArgs e) { //如果单击鼠标左键 if (e.Button == MouseButtons.Left) { //窗体的左边缘重新定位 this.Left += e.X - startX; //窗体的上边缘重新定位 this.Top += e.Y - startY; } }
范例1-22 动态设置提示字体
实例位置:光盘\ch01\1-22
范例说明
About the Example
本范例通过使用ToolTip控件实现动态显示提示字体的功能。程序运行效果如图1-38所示。

图1-38 程序界面
关键步骤
Key Steps
1.创建一个新的Windows工程。
2.把ToolTip控件拖放到设计界面上,设置Name属性为“tt”,双击Draw事件,添加代码,如图1-38所示。
3.添加Label控件拖放到设计界面上,设置Name属性为“lblToolTip”,Text属性为“鼠标移动到标签上,显示提示字”,tt的ToolTip属性为“Show Tooltip”。
程序代码
Codes
private void tt_Draw(object sender, DrawToolTipEventArgs e) { //使用系统颜色绘制提示字的背景色 e.DrawBackground(); //使用系统边框颜色绘制提示字的边框 e.DrawBorder(); using (StringFormat sf = new StringFormat()) { //文本的垂直对齐 sf.Alignment = StringAlignment.Center; //文本的水平对齐 sf.LineAlignment = StringAlignment.Center; sf.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.None; sf.FormatFlags = StringFormatFlags.NoWrap; using (Font f = new Font("宋体", 9)) { //绘制提示字 e.Graphics.DrawString(e.ToolTipText, f, SystemBrushes. ActiveCaptionText, e.Bounds, sf); } } }
1.2.2 窗体效果
范例1-23 创建半透明渐显窗体
实例位置:光盘\ch01\1-23
范例说明
About the Example
本范例是使用窗体的Opacity属性和Timer控件实现窗体的半透明渐显效果。程序运行效果如图1-39~图1-41所示。

图1-39 设置背景图片属性

图1-41 设置计数器属性
关键步骤
Key Steps
1.创建一个新的Windows工程。
2.单击窗体,设置BackGroundImage属性,如图1-39所示。
3.设置Opacity属性为“50%”,表示程序刚运行时为半透明,如图1-40所示。

图1-40 设置透明度属性
4.添加Timer控件,设置属性Enabled为“True”;设置Interval属性为“1000”,表示计数间隔为1秒,如图1-41所示。
5.双击Timer控件的Tick事件,添加代码,以便实现自动计数功能,使透明度可以自动变化。
程序代码
Codes
//设置计数器的计数函数 private void timer1_Tick(object sender, EventArgs e) { this.Opacity += 0.1; }
范例1-24 创建透明窗体
实例位置:光盘\ch01\1-24
范例说明
About the Example
本范例是窗体的Opacity属性创建透明窗体。程序运行效果如图1-42所示。

图1-42 透明窗体
关键步骤
Key Steps
1.创建一个新的Windows工程。
2.设置窗体的BackColor属性为“Chartreuse”,如图1-42所示。
3.设置窗体的Opacity属性为“48%”,这样,程序运行时,窗体就可以显示透明度为48%的外观样式了。
范例1-25 创建可滚动窗体
实例位置:光盘\ch01\1-25
范例说明
About the Example
本范例通过设置窗体的AutoScroll属性实现窗体的可滚动效果。程序运行效果如图1-43和图1-44所示。

图1-43 窗体界面

图1-44 窗体运行效果
关键步骤
Key Steps
1.创建一个新的Windows工程。
2.拖入一个Label控件,输入文本为滚动窗体实例,如图1-43所示。
3.设置属性AutoScroll为“True”,这样,程序运行时,拖动窗体的右下角使窗体缩小,就能发现窗体上的一些控件看不见了,窗体右边和下面可以出现滚动条,如图1-44所示。
范例1-26 关闭窗口前加入关闭对话框
实例位置:光盘\ch01\1-26
范例说明
About the Example
本范例是对窗体添加FormClosing事件,当用户要关闭窗体时,会弹出关闭确认对话框以便用户确认。程序运行效果如图1-45和图1-46所示。

图1-45 窗体界面

图1-46 窗体运行效果
关键步骤
Key Steps
1.创建一个新的Windows工程。
2.拖入一个Label控件,输入“关闭对话框确认实例”,如图1-45所示。
3.双击窗体事件FormClosing事件。输入代码,运行程序时就能出现关闭程序对话框。
程序代码
Codes
private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if(MessageBox.Show("确认要关闭此窗体吗?","确认对话",MessageBoxButtons.YesNo) ==DialogResult.Yes) { e.Cancel=false; } else { e.Cancel=true; } }
范例1-27 防止窗体在任务栏里出现
实例位置:光盘\ch01\1-27
范例说明
About the Example
本范例是通过窗体的ShowInTaskbar属性设置窗体是否出现在任务栏中。程序运行效果如图1-47所示。

图1-47 设置显示在任务栏属性
关键步骤
Key Steps
1.创建一个新的Windows工程。
2.设置窗体的ShowInTaskbar属性为“False”,如图1-47所示。
3.运行程序,就可以看到窗体不会出现在任务栏中,但用户可以按【Alt+Tab】组合键切换到该窗体。
范例1-28 窗体中的滚动字幕
实例位置:光盘\ch01\1-28
范例说明
About the Example
本范例是使用Timer控件控制字体的循环滚动。程序运行效果如图1-48所示。

图1-48 窗体界面
关键步骤
Key Steps
1.创建一个新的Windows工程。
2.拖放Label控件和Button控件,实现图1-48所示的程序界面。
3.添加Timer控件,设置属性,如表1-2所示。
表1-2 Timer属性设置

4.双击Timer控件的Ticker事件,输入代码,实现字幕的滚动。
5.双击【开始】和【停止】按钮的Click事件,实现字母滚动的开始和停止。
程序代码
Codes
1.Ticker事件
private void timer1_Tick(object sender, EventArgs e) { this.lblWord.Top -= 10; if (this.lblWord.Bottom < 0) { this.lblWord.Top = this.Height; } }
2.【开始】按钮
private void btnBegin_Click(object sender, EventArgs e) { timer1.Enabled = true; }
3.【停止】按钮
private void btnStop_Click(object sender, EventArgs e) { timer1.Enabled = false; }
1.3 对话框应用
本节主要讲解常见的几种对话框的使用:打开文件对话框、保存文件对话框、浏览文件夹对话框、颜色对话框和字体对话框。
1.3.1 打印对话框
范例1-29 PrintSetupDialog对话框的使用
实例位置:光盘\ch01\1-29
范例说明
About the Example
本范例通过使用打印设置对话框控件PrintSetupDialog帮助用户对需要打印的文件进行设置,程序运行效果如图1-49和图1-50所示。

图1-49 窗体界面

图1-50 页面设置界面
关键步骤
Key Steps
1.创建一个新的Windows工程。
2.把MenuStrip控件和RichTextBox控件拖放到设计界面上,并添加【File】、【Open】、【Print Preview】、【Print Setup】和【Print】菜单,如图1-49所示。
3.双击工具箱对话框中的OpenFileDialog控件,设置属性,如表1-3所示。
表1-3 OpenFileDialog属性设置

4.双击【打开文件】菜单项,输入代码,实现打开文件对话框的弹出和用户选择文件的功能。
5.双击“工具箱”打印栏中的PrintDocument控件,设置Name属性为“pd”,双击PrintPage事件,输入代码实现打印处理过程。
6.双击“工具箱”打印栏中的PrintSetupDialog控件,设置Name属性为“dlgPS”,设置Document属性为“pd”,使要打印的代码和对话框关联起来。
程序代码
Codes
1.打开文件
private void miOpen_Click(object sender, EventArgs e) { try { if (this.dlgOpen.ShowDialog() == DialogResult.OK) { this.rchShow.LoadFile(this.dlgOpen.FileName, RichTextBoxStreamType.PlainText); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
2.打印设置
private void miPS_Click(object sender, EventArgs e) { this.dlgPS.ShowDialog(); }
3.打印页面
private void pd_PrintPage(object sender, PrintPageEventArgs e) { int x = e.MarginBounds.Left; int y = e.MarginBounds.Top; lines = this.rchShow.Text.Split(param); while (linesPrinted < lines.Length) { e.Graphics.DrawString(lines[linesPrinted++], new Font("Arial", 10), Brushes.Black, x, y); y += 15; if (y >= e.PageBounds.Height -80) { e.HasMorePages = true; return; } } linesPrinted = 0; e.HasMorePages = false; }
范例1-30 PrintPreviewDialog对话框的使用
实例位置:光盘\ch01\1-30
范例说明
About the Example
本范例通过使用打印设置对话框控件PrintPreviewDialog帮助用户对需要打印的文件进行预览,程序运行效果如图1-51和图1-52所示。

图1-51 窗体界面

图1-52 页面预览界面
关键步骤
Key Steps
1.创建一个新的Windows工程。
2.把MenuStrip控件和RichTextBox控件拖放到设计界面上,并添加【File】、【Open】、【Print Preview】、【Print Setup】和【Print】菜单,如图1-51所示。
3.双击工具箱对话框中的OpenFileDialog控件,设置属性,如表1-3所示。
表1-3 OpenFileDialog属性设置

4.双击【打开文件】菜单项,输入代码,实现打开文件对话框的弹出和用户选择文件的功能。
5.双击“工具箱”打印中的PrintDocument控件,设置Name属性为“pd”,双击PrintPage事件,输入代码实现打印处理过程。
6.双击“工具箱”打印中的PrintPreviewDialog控件,设置Name属性为dlgpp,设置Document属性为“pd”,使要打印的代码和对话框关联起来。
程序代码
Codes
1.打开文件
private void miOpen_Click(object sender, EventArgs e) { try { if (this.dlgOpen.ShowDialog() == DialogResult.OK) { this.rchShow.LoadFile(this.dlgOpen.FileName, RichTextBoxStreamType.PlainText); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
2.打印预览
private void miPp_Click(object sender, EventArgs e) { this.dlgpp.ShowDialog(); }
3.打印页面
char[] param ={ '\n' }; private string[] lines; private int linesPrinted; private void pd_PrintPage(object sender, PrintPageEventArgs e) { int x = e.MarginBounds.Left; int y = e.MarginBounds.Top; lines = this.rchShow.Text.Split(param); while (linesPrinted < lines.Length) { e.Graphics.DrawString(lines[linesPrinted++], new Font("Arial", 10), Brushes.Black, x, y); y += 15; if (y >= e.PageBounds.Height -80) { e.HasMorePages = true; return; } } linesPrinted = 0; e.HasMorePages = false; }
范例1-31 PrintDialog对话框的使用
实例位置:光盘\ch01\1-31
范例说明
About the Example
本范例通过使用打印设置对话框控件PrintDialog帮助用户打印文件,程序运行效果如图1-53和图1-54所示。

图1-53 窗体界面

图1-54 页面打印界面
关键步骤
Key Steps
1.创建一个新的Windows工程。
2.把MenuStrip控件和RichTextBox控件拖放到设计界面上,并添加【File】、【Open】、【Print Preview】、【Print Setup】和【Print】菜单,如图1-53所示。
3.双击工具箱对话框中的OpenFileDialog控件,设置属性,如表1-3所示。
表1-3 OpenFileDialog属性设置

4.双击【打开文件】菜单项,输入代码,实现打开文件对话框的弹出和用户选择文件的功能。
5.双击“工具箱”打印中的PrintDocument控件,设置Name属性为“pd”,双击PrintPage事件,输入代码实现打印处理过程。
6.双击“工具箱”打印中的PrintPreviewDialog控件,设置Name属性为“dlgpp”,设置Document属性为“pd”,使要打印的代码和对话框关联起来。
程序代码
Codes
1.打开文件
private void miOpen_Click(object sender, EventArgs e) { try { if (this.dlgOpen.ShowDialog() == DialogResult.OK) { this.rchShow.LoadFile(this.dlgOpen.FileName, RichTextBoxStreamType.PlainText); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
2.打印页面
char[] param ={ '\n' }; private string[] lines; private int linesPrinted; private void pd_PrintPage(object sender, PrintPageEventArgs e) { int x = e.MarginBounds.Left; int y = e.MarginBounds.Top; lines = this.rchShow.Text.Split(param); while (linesPrinted < lines.Length) { e.Graphics.DrawString(lines[linesPrinted++], new Font("Arial", 10), Brushes.Black, x, y); y += 15; if (y >= e.PageBounds.Height -80) { e.HasMorePages = true; return; } } linesPrinted = 0; e.HasMorePages = false;
3.打印文件
private void miPrint_Click(object sender, EventArgs e) { try { if (this.dlgprint.ShowDialog() == DialogResult.OK) { pd.Print(); } } catch (InvalidPrinterException ex) { MessageBox.Show(ex.Message); } }
1.3.2 打开文件对话框
范例1-32 设置打开文件类型
实例位置:光盘\ch01\1-32
范例说明
About the Example
本范例通过使用打开文件对话框控件OpenFileDialog帮助用户浏览文件系统,选择要操作的文件。并且使用Filter属性设置要打开的文件类型。程序运行效果如图1-55和图1-56所示。

图1-55 窗体界面

图1-56 选择文件界面
关键步骤
Key Steps
1.创建一个新的Windows工程。
2.把MenuStrip控件和RichTextBox控件拖放到设计界面上,并添加【操作】和【打开文件】菜单,如图1-55所示。
3.双击工具箱对话框中的OpenFileDialog控件,设置属性,如表1-4所示。
表1-4 OpenFileDialog属性设置

4.双击【打开文件】菜单项,输入代码,实现打开文件对话框的弹出和用户选择文件的功能。
程序代码
Codes
private void miOpen_Click(object sender, EventArgs e) { try { //判断是否选择了文件 if (dlgOpenFile.ShowDialog() == DialogResult.OK) {//将文件加载到RichTextBox中 this.rchShow.LoadFile(dlgOpenFile.FileName, RichTextBoxStreamType.PlainText); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
范例1-33 设置起始路径
实例位置:光盘\ch01\1-33
范例说明
About the Example
本范例通过使用打开文件对话框控件OpenFileDialog帮助用户浏览文件系统,选择要操作的文件。并且使用InitialDirectory属性设置要打开的起始路径。程序运行效果如图1-57和图1-58所示。

图1-57 窗体界面

图1-58 运行界面
关键步骤
Key Steps
1.创建一个新的Windows工程。
2.把MenuStrip控件和RichTextBox控件拖放到设计界面上,并添加【操作】和【打开文件】菜单,如图1-57所示。
3.双击工具箱对话框中的OpenFileDialog控件,设置属性,如表1-5所示。
表1-5 OpenFileDialog属性设置

4.双击【打开文件】菜单项,输入代码,实现打开文件对话框的弹出和用户选择文件的功能。
程序代码
Codes
private void miOpen_Click(object sender, EventArgs e) { try { //判断是否选择了文件 if (dlgOpenFile.ShowDialog() == DialogResult.OK) {//将文件加载到RichTextBox中 this.rchShow.LoadFile(dlgOpenFile.FileName, RichTextBoxStreamType.PlainText); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
范例1-34 选择多个文件
实例位置:光盘\ch01\1-34
范例说明
About the Example
本范例通过使用打开文件对话框控件OpenFileDialog帮助用户浏览文件系统,选择要操作的文件。并且使用Multiselect属性设置是否允许选择多个文件。程序运行效果如图1-59和图1-60所示。

图1-59 窗体界面

图1-60 运行界面
关键步骤
Key Steps
1.创建一个新的Windows工程。
2.把MenuStrip控件和RichTextBox控件拖放到设计界面上,并添加【File】和【打开多个文件】菜单,如图1-59所示。
3.双击工具箱对话框中的OpenFileDialog控件,设置属性,如表1-6所示。
表1-6 OpenFileDialog属性设置

4.双击【打开文件】菜单项,输入代码,实现打开文件对话框的弹出和用户选择文件的功能。
程序代码
Codes
private void miMultiSel_Click(object sender, EventArgs e) { try { if (this.dglOpen.ShowDialog() == DialogResult.OK) { StringBuilder sb=new StringBuilder(); foreach (string s in this.dglOpen.FileNames) { sb.Append(File.ReadAllText(s)).AppendLine(); } if (sb.Length > 0) { this.rchShow.Text = sb.ToString(); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
1.3.3 保存文件对话框
范例1-35 设置保存文件类型
实例位置:光盘\ch01\1-35
范例说明
About the Example
本范例通过使用保存文件对话框控件SaveFileDialog帮助用户保存文件,并且使用Filter属性设置要保存的文件类型。程序运行效果如图1-61和图1-62所示。

图1-61 窗体界面

图1-62 选择保存类型界面
关键步骤
Key Steps
1.创建一个新的Windows工程。
2.把MenuStrip控件和RichTextBox控件拖放到设计界面上,并添加【操作】和【打开文件】和【保存文件】菜单,如图1-61所示。
3.双击工具箱对话框中的OpenFileDialog控件,设置属性,如表1-7所示。
表1-7 OpenFileDialog属性设置

4.双击【打开文件】菜单项,输入代码实现保存文件对话框的弹出和用户选择文件的功能。
5.双击工具箱对话框中的SaveFileDialog控件,设置属性,如表1-8所示。
表1-8 SaveFileDialog属性设置

6.双击【保存文件】菜单项,输入代码,实现保存文件对话框的弹出和用户保存文件的功能。
程序代码
Codes
1.【打开文件】菜单项代码
private void miOpen_Click(object sender, EventArgs e) { try { if (dlgOpenFile.ShowDialog() == DialogResult.OK) { this.rchShow.LoadFile(dlgOpenFile.FileName, RichTextBoxStreamType.PlainText); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
2.【保存文件】菜单项代码
private void miClose_Click(object sender, EventArgs e) { try { if (dlgSaveFile.ShowDialog() == DialogResult.OK) { this.rchShow.SaveFile(dlgSaveFile.FileName, RichTextBoxStreamType.PlainText); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
范例1-36 设置保存文件类型
实例位置:光盘\ch01\1-36
范例说明
About the Example
本范例通过使用保存文件对话框控件SaveFileDialog帮助用户保存文件,并且使用InitialDirectory属性设置要保存的文件类型。程序运行效果如图1-63和图1-64所示。

图1-63 窗体界面

图1-64 选择保存类型界面
关键步骤
Key Steps
1.创建一个新的Windows工程。
2.把MenuStrip控件和RichTextBox控件拖放到设计界面上,并添加【操作】和【打开文件】和【保存文件】菜单,如图1-63所示。
3.双击工具箱对话框中的OpenFileDialog控件,设置属性,如表1-9所示。
表1-9 OpenFileDialog属性设置

4.双击【打开文件】菜单项,输入代码实现保存文件对话框的弹出和用户选择文件的功能。双击工具箱对话框中的SaveFileDialog控件,设置属性,如表1-10所示。
表1-10 SaveFileDialog属性设置

5.双击【保存文件】菜单项,输入代码,实现保存文件对话框的弹出和用户保存文件的功能。
程序代码
Codes
1.【打开文件】菜单项代码
private void miOpen_Click(object sender, EventArgs e) { try { if (dlgOpenFile.ShowDialog() == DialogResult.OK) { this.rchShow.LoadFile(dlgOpenFile.FileName, RichTextBoxStreamType.PlainText); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
2.【保存文件】菜单项代码
private void miClose_Click(object sender, EventArgs e) { try { if (dlgSaveFile.ShowDialog() == DialogResult.OK) { this.rchShow.SaveFile(dlgSaveFile.FileName, RichTextBoxStreamType.PlainText); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
1.3.4 浏览文件夹对话框
范例1-37 设置不显示新建文件夹
实例位置:光盘\ch01\1-37
范例说明
About the Example
本范例通过使用文件夹浏览对话框控件FolderBrowerDialog帮助用户选择文件。程序运行效果如图1-65和图1-66所示。

图1-65 窗体界面

图1-66 含新建文件夹的程序界面
关键步骤
Key Steps
1.创建一个新的Windows工程。
2.把MenuStrip控件和RichTextBox控件拖放到设计界面上,并添加【File】和【打开文件夹】菜单,如图1-65所示。
3.双击工具箱对话框中的FolderBrowerDialog控件,设置属性,如表1-11所示。
表1-11 FolderBrowerDialog属性设置

4.单击【打开文件夹】菜单项,输入代码实现保存文件对话框的弹出和用户选择文件的功能。
程序代码
Codes
private void miOpen_Click(object sender, EventArgs e) { try { if (this.dlgFB.ShowDialog() == DialogResult.OK) { this.rchShow.Text = "选择的文件夹名为:" + this.dlgFB.SelectedPath; } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
范例1-38 设置根文件夹
实例位置:光盘\ch01\1-38
范例说明
About the Example
本范例通过使用文件夹浏览对话框控件FolderBrowerDialog帮助用户选择文件。程序运行效果如图1-67和图1-68所示。

图1-67 窗体界面

图1-68 择保存类型界面
关键步骤
Key Steps
1.创建一个新的Windows工程。
2.把MenuStrip控件和RichTextBox控件拖放到设计界面上,并添加【File】和【打开文件夹】菜单,如图1-67所示。
3.双击工具箱对话框中的FolderBrowerDialog控件,设置属性,如表1-12所示。
表1-12 FolderBrowerDialog属性设置

4.单击【打开文件夹】菜单项,输入代码实现保存文件对话框的弹出和用户选择文件的功能。
程序代码
Codes
private void miOpen_Click(object sender, EventArgs e) { try { if (this.dlgFB.ShowDialog() == DialogResult.OK) { this.rchShow.Text="选择的文件夹名为:"+this.dlgFB.SelectedPath; } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
1.3.5 其他
范例1-39 字体对话框
实例位置:光盘\ch01\1-39
范例说明
About the Example
本范例通过使用字体对话框控件FontDialog帮助用户编辑文本字体。程序运行效果如图1-69所示。

图1-69 窗体界面
关键步骤
Key Steps
1.创建一个新的Windows工程。
2.把MenuStrip控件和RichTextBox控件拖放到设计界面上,并添加【操作】和【打开文件】和【字体】菜单,如图1-69所示。
3.双击工具箱对话框中的OpenFileDialog控件,设置属性,如表1-13所示。
表1-13 OpenFileDialog属性设置

4.双击【打开文件】菜单项,输入代码实现保存文件对话框弹出和用户选择文件的功能。
5.双击工具箱对话框中的FontDialog控件,并设置Name属性为“dlgFont”。
6.双击【字体】菜单项,输入代码,实现文件内容的字体编辑。
程序代码
Codes
1.【打开文件】菜单项代码
private void miOpen_Click(object sender, EventArgs e) { try { if (dlgOpenFile.ShowDialog() == DialogResult.OK) { this.rchShow.LoadFile(dlgOpenFile.FileName, RichTextBoxStreamType.PlainText); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
2.“字体”设置
private void miFont_Click(object sender, EventArgs e) { if (dlgFont.ShowDialog() == DialogResult.OK) { this.rchShow.Font = dlgFont.Font; } }
范例1-40 颜色对话框
实例位置:光盘\ch01\1-40
范例说明
About the Example
本范例通过使用颜色对话框控件ColorDialog帮助用户编辑字体颜色。程序运行效果如图1-70所示。

图1-70 窗体界面
关键步骤
Key Steps
1.创建一个新的Windows工程。
2.把MenuStrip控件和RichTextBox控件拖放到设计界面上,并添加【操作】、【打开文件】和【颜色设置】菜单,如图1-70所示。
3.双击工具箱对话框中的OpenFileDialog控件,设置属性,如表1-14所示。
表1-14 OpenFileDialog属性设置

4.双击【打开文件】菜单项,输入代码实现保存文件对话框的弹出和用户选择文件的功能。
5.双击工具箱对话框中的ColorDialog控件,并设置Name属性为“dlgColor”。
6.双击【颜色设置】菜单项,输入代码,实现文件内容的字体编辑。
程序代码
Codes
1.【打开文件】菜单项代码
private void miOpen_Click(object sender, EventArgs e) { try { if (dlgOpenFile.ShowDialog() == DialogResult.OK) { this.rchShow.LoadFile(dlgOpenFile.FileName, RichTextBoxStreamType.PlainText); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
2.“颜色”设置
private void miFont_Click(object sender, EventArgs e) { try { if(dlgColor.ShowDialog()==DialogResult.OK) { this.rchShow.SelectionColor = dlgColor.Color; } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
1.4 MDI应用程序
本节着重介绍MDI多文档应用程序的创建和使用。
范例1-41 创建一个简单的MDI应用程序
实例位置:光盘\ch01\1-41
范例说明
About the Example
本范例通过创建一个MDI应用程序,实现多文档操作,并且比较三种Windows应用程序的不同。程序运行效果如图1-71和图1-72所示。

图1-71 父窗体界面

图1-72 创建子窗体界面
关键步骤
Key Steps
1.创建一个新的Windows工程。
2.单击窗体,设置属性,如表1-15所示。设置结果如图1-71所示。
表1-15 属性设置

3.在frmParent窗体单击鼠标右键,单击【查看代码】菜单项,进入代码编辑页,修改窗体的构造函数,实现父窗体和子窗体的绑定。
4.在MDIAppEX项目单击鼠标右键,选择【添加】→【Windows窗体】菜单项,弹出添加新项对话框,如图1-72所示。
5.在frmChild窗体单击鼠标右键,单击【查看代码】菜单项,进入代码编辑页,修改窗体的构造函数,使子窗体绑定到父窗体上。
程序代码
Codes
1.父窗体的构造函数
public frmChild(frmParent parent) { InitializeComponent(); this.MdiParent = parent; }
2.子窗体的构造函数
public frmParent() { InitializeComponent(); frmChild child = new frmChild(this); child.Show(); }
注意:
1.Windows一般可以编写3种应用程序
基于对话框的应用程序—给用户提供一个对话框,该对话框提供了用户所需要的功能,如字体对话框、颜色对话框。
单一文档界面(SDI)—给用户提供一个菜单、一个或多个工具栏和一个窗口,用户可以执行功能,如写字板。
多文档界面(MDI)—给用户提供一个菜单、一个或多个工具栏和多个窗口,用户可以执行功能,如Word应用程序。
2.MDI应用程序的相关知识
MDI应用程序至少由两个窗口组成:一个窗口叫MDI容器(Container),另一个窗口叫MDI子窗口。一个MDI容器可以放置多个MDI子窗口,实现多文档状态。
范例1-42 创建一个MDI文本编辑器
实例位置:光盘\ch01\1-42
范例说明
About the Example
本程序运行效果如图1-73和图1-74所示。范例通过MDI窗体和以前所学的关于菜单和窗体的知识,创建一个简单的MDI文本编辑器。

图1-73 父窗体界面

图1-74 子窗体界面
关键步骤
Key Steps
1.创建一个新的Windows工程。
2.单击窗体,设置属性,如表1-16所示。设置结果如图1-73所示。
表1-16 属性设置

3.在frmContainer窗体上单击鼠标右键,单击【查看代码】菜单项,进入代码编辑页,修改窗体的构造函数,实现父窗体和子窗体的绑定。
4.在MDIEditorEX项目上单击鼠标右键,选择【添加】→【Windows窗体】菜单项,弹出添加新项对话框。
5.在frmEditor窗体上单击鼠标右键,单击【查看代码】菜单项,进入代码编辑页,修改窗体的构造函数,使子窗体绑定到父窗体上。
6.把MenuStrip控件拖放到frmEditor设计界面上,并添加【File】和【Format】菜单和相应的子菜单,按照表1-17设置各个菜单项,设置后界面如图1-74所示。
表1-17 属性设置

7.双击各个菜单项,实现各个菜单项的功能。

程序代码
Codes
1.父窗体构造函数
public frmContainer() { InitializeComponent(); frmEditor child = new frmEditor(this); child.Show(); }
2.子窗体构造函数
public frmEditor(frmContainer parent) { InitializeComponent(); this.MdiParent = parent; }
3.新建文件
private void miNew_Click(object sender, EventArgs e) { this.rchtxtShow.Clear(); }
4.打开文件
private void miOpen_Click(object sender, EventArgs e) { try { if (dlgOpen.ShowDialog() == DialogResult.OK) { if (dlgOpen.FilterIndex == 1) { this.rchtxtShow.LoadFile(dlgOpen.FileName, RichTextBoxStreamType.RichText); } } } catch(Exception ex) { MessageBox.Show(ex.Message); } }
5.保存文件
private void miSave_Click(object sender, EventArgs e) { try { if (dlgSave.ShowDialog() == DialogResult.OK) { if (dlgSave.FilterIndex == 1) { this.rchtxtShow.SaveFile(dlgSave.FileName, RichTextBoxStreamType.RichText); } } } catch(Exception ex) { MessageBox.Show(ex.Message); } }
6.关闭子窗体
private void miClose_Click(object sender, EventArgs e) { this.Close(); }
7.设置粗体字体
private void miBold_Click(object sender, EventArgs e) { Font oldFont = this.rchtxtShow.SelectionFont; Font newFont; if (oldFont.Bold) { newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Bold); } else { newFont = new Font(oldFont, oldFont.Style | FontStyle.Bold); } this.rchtxtShow.SelectionFont = newFont; }
8.设置斜体字体
private void miItalic_Click(object sender, EventArgs e) { Font oldFont = this.rchtxtShow.SelectionFont; Font newFont; if (oldFont.Italic) { newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Italic); } else { newFont = new Font(oldFont, oldFont.Style | FontStyle.Italic); } this.rchtxtShow.SelectionFont = newFont; }
9.设置下画线字体
private void miUnderline_Click(object sender, EventArgs e) { Font oldFont = this.rchtxtShow.SelectionFont; Font newFont; if (oldFont.Underline) { newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Underline); } else { newFont = new Font(oldFont, oldFont.Style | FontStyle.Underline); } this.rchtxtShow.SelectionFont = newFont; }
1.5 本章小结
一个好的界面是一个软件成功的关键因素。纵观Office、QQ和IE,这些软件的流行与它们优秀的设计界面是分不开的,在开发时可以参照这些界面,熟悉操作会让用户更快地掌握软件的使用方法。