|
这里,我们创建了一个按钮,并添加了一个名为 button1_Click 的处理程序方法,通过短短几行代码,在单击该按钮后,将调用这一方法。请注意,即使处理程序方法被标记为专用,创建这一挂钩的代码仍可以使用该方法,单击按钮后,按钮将能够激活这一方法的事件。
启动 Win Forms 项目的过程也得到了简化。使用 Visual Studio.NET 创建 Win Forms 项目的过程只会创建一个要编译的项目文件:Form1.cs。没有头文件,没有接口定义文件,没有引导程序文件,没有资源文件,没有库文件。项目所需的所有信息都包含在窗体的代码中。这样做有一个好处:项目由一个简单的单窗体应用程序扩展到复杂的、带有多个代码文件的多窗体应用程序要方便得多。链接过程不需要中间对象文件,只有代码和已构建的、受管理的所有 DLL。只要您习惯了这一方法,就能明显地感觉到创建 .NET Framework 应用程序和创建 C/C 应用程序之间复杂性的不同。因为信息仅仅包含在代码文件中,在 Visual Studio.NET 环境外创建版本的过程也非常容易,无论是 Visual Basic 代码、C# 代码,还是任何其它语言编写的针对 .NET Framework 的代码。
因为 Win Forms 建立在通用语言运行时的基础之上,开发人员可以任选目前针对通用语言运行时的众多语言中的一种,构建 Win32 应用程序。开发人员现在可以使用多种语言编写 Win Forms 应用程序(或 Web Forms 应用程序或 Data 应用程序):从 C# 到 COBOL 到 Eiffel 再到 Perl 等等,中间还有很多种(上一次计数是 17 种)。方便易用再加上广泛的应用场合相得益彰,为开发人员提供了深厚的基础,使他们能够迅速有效地使用 Win Forms 构建实用的应用程序。
布局
如果您曾尝试创建能够正常调整大小的窗体,您就会知道这一过程有多么困难。Microsoft Foundation Classes (MFC) 或早期的 Visual Basic 版本没有对这一功能提供内置的支持。然而现在只需几行代码(通常情况下您甚至不需要编写这些代码,因为在设计时就能通过 Property Browser 实现这些功能!),即可创建能够正常调整大小的对话框。
基本布局由两条组成:Anchoring 和 Docking。RichControl 有一个 Anchor 属性,它是一种枚举类型,可以用“或”操作将这些值组合在一起,以说明控件将与其父控件的某一边保持恒定距离。例如,如果您将一个按钮置于窗体上,并将 Anchor 属性设置为 AnchorStyles.BottomRight,则在调整按钮的大小时,按钮将与窗体的底边和右边保持同一距离。此外,如果将 Anchor 设置为 AnchorStyles.All,则按钮的各个边都与窗体的对应边保持同一距离,在调整按钮大小时仍要满足这些约束条件。
Docking 实际上是 Anchoring 的一个特殊情况。RichControl 的 Dock 属性说明控件要将自身固定到其父控件的哪一边。Docking 可以是 Top、Left、Right、Bottom 或 Fill。在每种情况下,控件都将移动到尽量靠近指定边,并调整其大小,以填满那一边。如果父控件的大小有所调整,这一状况仍将保持。将一个控件移动到父控件的底端,并将 Anchor 设置为 AnchorStyle.BottomLeftRight,可以模拟 Docking Bottom。在此处的示例中,列表框是 Docked Left,按钮与窗体的顶端、左边和右边保持恒定距离,由此它们保持了相对位置和大小。下面的示例对话框(图 2)完全使用 Visual Studio.NET 中的 Win Forms 设计器创建,只花了两分钟的时间,没有编写一行代码。
图 2. 使用 Win Forms 设计器所创建的可调整大小的对话框
// ResizableSample.cs
namespace ResizableSampleNamespace {
using System;
using System.Drawing;
using System.ComponentModel;
using System.WinForms;
/// <summary>
/// ResizableSample 的摘要说明。
/// </summary>
public class ResizableSample : System.WinForms.Form {
/// <summary>
/// 为 Win Forms 设计器所要求
/// </summary>
private System.ComponentModel.Container components;
private System.WinForms.Button button3;
private System.WinForms.Button button2;
private System.WinForms.Button button1;
private System.WinForms.ListBox listBox1;
public ResizableSample() {
// 为 Win Form 设计器支持所要求
InitializeComponent();
}
/// <summary>
/// 释放正在使用的所有资源
/// </summary>
public override void Dispose() {
base.Dispose();
components.Dispose();
}
/// <summary>
/// 应用程序的主入口点。
/// </summary>
public static void Main(string[] args) {
Application.Run(new ResizableSample());
}
/// <summary>
/// 设计器支持所要求的方法 — 不要用编辑器
/// 修改这一方法的内容
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.button2 = new System.WinForms.Button();
this.button3 = new System.WinForms.Button();
this.button1 = new System.WinForms.Button();
this.listBox1 = new System.WinForms.ListBox();
//@design this.TrayLargeIcon = false;
//@design this.TrayHeight = 0;
this.Text = "Resizable Dialog";
this.IMEMode = System.WinForms.IMEMode.Off;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(256, 173);
button2.Location = new System.Drawing.Point(152, 60);
button2.Size = new System.Drawing.Size(92, 32);
button2.TabIndex = 2;
button2.Anchor = System.WinForms.AnchorStyles.TopLeftRight;
button2.Text = "Cancel";
button3.Location = new System.Drawing.Point(152, 120);
button3.Size = new System.Drawing.Size(92, 44);
button3.TabIndex = 3;
button3.Anchor = System.WinForms.AnchorStyles.All;
button3.Text = "Filler";
button1.Location = new System.Drawing.Point(152, 8);
button1.Size = new System.Drawing.Size(92, 32);
button1.TabIndex = 1;
button1.Anchor = System.WinForms.AnchorStyles.TopLeftRight;
button1.Text = "OK";
listBox1.Size = new System.Drawing.Size(120, 173);
listBox1.Dock = System.WinForms.DockStyle.Left;
listBox1.TabIndex = 0;
listBox1.Items.All = new object[] {"Item One",
"Item Two",
"Item Three",
"Item Four"};
this.Controls.Add(button3);
this.Controls.Add(button2);
this.Controls.Add(button1);
this.Controls.Add(listBox1);
}
}
}
GDI
Win Forms 全面利用了 GDI 这一 Microsoft 下一代的二维图形系统。Win Forms 中的图形编程模式完全是面向对象的,各式各样的画笔、笔刷、图像和其它图形对象与 .NET Framework 的其它部分一样,遵循了简单易用的指导方针。开发人员目前可以使用相当不错的一些绘图新功能,如 alpha 混色、渐变色、纹理、消除锯齿以及采用除位图外的其它图像格式。与 Windows 2000 操作系统分层和透明的窗口功能配合使用,开发人员能够毫不费力地创建丰富的、更为图形化的 Win32 应用程序。
如果触发了控件的 OnPaint 事件,能够由 PaintEventArgs 访问的 System.Drawing.Graphics 对象就成为一个 GDI 图形对象。图形对象能够执行的所有操作都通过 GDI 实施。作为一个示例,使用 GDI 创建一个绘制渐变背景的按钮。
图 3. 使用 GDI 创建的按钮
以下是实现这一按钮的代码:
public class GradientButton : Button {
// 保留颜色设置的成员
private Color startColor;
private Color endColor;
// 书写文字时我们将需要它
private static StringFormat format = new StringFormat();
public GradientButton() : base() {
// 初始化颜色
startColor = SystemColors.InactiveCaption;
endColor = SystemColors.ActiveCaption;
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
}
/// <summary>
/// 渐变色的终止颜色
// </summary>
public Color EndColor {
get {
return this.endColor;
}
set {
this.endColor = value;
// 如有必要,则导致重新绘制
if (this.IsHandleCreated && this.Visible) {
Invalidate();
}
}
}
/// <summary>
/// 渐变色的起始颜色
// </summary>
public Color StartColor {
get {
return this.startColor;
}
set {
this.startColor = value;
// 如有必要,则导致重新绘制
if (this.IsHandleCreated && this.Visible) {
Invalidate();
}
}
上一页 [1] [2] [3] [4] 下一页
|