导读

1、什么是 Windows Forms

2、需要学Windows Forms 么?

3、如何手写一个简单的Windows Forms 程序

4、对上面程序的说明

5、Form 类与Control类

6、Windows Forms 中的事件处理及手写一个带鼠标移动事件的窗体


什么是Windows Forms

通常我们的说的Windows Forms 指的是一类GUI(图形用户界面)程序的统称,Windows Forms 是随着 .NET 1.0 一起发布的,但市面上的 Windows Forms 程序主要还是 .NET 2.0 以上版本的,.NET 3.5 后主推的GUI程序变为 WPF,Windows Forms 退居2线。

需要学Windows Forms 么?

这个问题的答案因人而异,如果对于学生而言,我建议直接跳过Windows Forms 学WPF,WPF的思维模式比Windows Forms的事件处理模式要优秀不少。对于找工作(或已工作)的程序员而言,这个就要取决于公司的需求,在很多做行业软件的公司,用Windows Form 比 WPF多,虽然WPF的界面比原生Windows Forms炫太多了,但是Windows Forms 第三方的控件库弥补了这个问题。

如何手写一个简单的Windows Forms 程序

直接上代码: FirstWinForm.cs

using System;
using System.Windows.Forms; namespace DemoWinForm
{
class App : Form
{
static void Main()
{
Application.Run(new App());
}
}
}

编译命令: csc /t:winexe FirstWinForm.cs

运行 效果

可以看到一个简单得不能再简单的Windows Form 程序跑起来了。

对上面程序的说明

从上面的代码可以看出,Windows Forms 是一个类(其实应该是Windows Forms 窗体类是一个类),一个类要运行得有一个 Main 方法,上面Main方法中有一个 Application.Run(new App()); 这句话貌似是让 Windows Forms 跑起来的语句(Run)。再看引用,System.Windows.Forms 从这命名上看这应该是 Windows Forms的程序集。好下面我们正式揭开谜底

Windows 窗体 是任何继承 Form 的类叫窗体

System.Windows.Forms 命名空间包含了Windows Forms 程序的核心内容,它包括Windows Forms 的核心架构可视化控件(设计时可见且运行时也可见)、组件(有设计时和运行是都可见的组件如ToolTip和仅设计时可见运行时不可见的组件如Timer)、对话框(公共对话框如OpenFileDialog)

System.Windows.Forms 命名空间中的核心类型

1、Application    该类封装了Windows 窗体应用程序运行时操作

2、常用控件类(Button、ComboBox、CheckBox、TextBox、Label、DateTimePicker、ListBox、PictureBox、TreeView)

3、Form 窗体类

4、布局控件类(Panel、Splitter 、GroupBox、TabControl)

5、菜单控件 (Menu、MenuItem 、ContextMenu)

6、各种对话框

System.Windows.Forms.Application 类需关注的方法
Run()          运行Windows Forms 窗体

DoEvents()  提供应用程序在冗长的操作期间处理当前排在消息队列里的信息的能力

Exit()          终止窗口应用程序并从承载的应用程序域中卸载

EnableVisualStyles()   配置应用程序以支持 Windows XP 界面外观

现在我们分析下上面的程序:

1、上面的代码写的是一个可执行程序(exe),所以它有一个Main方法。

2、App类是一个窗体类,因为它继承了 System.Windows.Forms.Form类

3、通过 Application.Run(Windows 窗体实例); 运行得到我们看到的Windows Forms 程序

上面的代码耦合度太强了,应用程序的运行(创建AppDomian)和窗体逻辑耦合到一起(说白了窗体类应该只关注窗体逻辑,不关注谁来加载这个窗体到AppDomain中运行)所以上面的代码最好改为如下的代码:

using System;
using System.Windows.Forms; namespace DemoWinForm
{
// 注意这里我用静态类
static class App
{
static void Main()
{
Application.Run(new MainForm());
}
} public class MainForm : Form{}
}

Form 类与Control类

我们先看下 System.Windows.Forms.Form 的继承关系图

System.Object
System.MarshalByRefObject

System.ComponentModel.Component

System.Windows.Forms.Control

System.Windows.Forms.ScrollableControl

System.Windows.Forms.ContainerControl

System.Windows.Forms.Form

标红的是我认为需要重点关注的类,所有可视化控件(如 Button 之类)都是继承自 System.Windows.Forms.Control,而组件则是继承自 System.ComponentModel.Component。

System.Windows.Forms.Control 类提供了一个可视化控件的绝大多数成员(控件名、Size、字体、前景和背景色、父容器、常用事件如鼠标相关、键盘相关等)详细内容参见MSDN

使用Control类的例子:

using System;
using System.Windows.Forms;
using System.Drawing; namespace DemoWinForm
{
static class App
{
static void Main()
{
Application.Run(new MainForm());
}
} public class MainForm : Form
{
public MainForm()
{
Text = "一个窗口";
Height = ;
Width = ;
BackColor = Color.Green;
Cursor = Cursors.Hand;
}
}
}

编译 csc /t:winexe UseControlDemo.cs

Windows Forms 中的事件处理及手写一个带鼠标移动事件的窗体

System.EventHandler
原型为

public delegate void EventHandler(object sender,EventArgs e);

它是 WinForm程序事件处理的最原始委托,sender 表示发送事件的对象,e 表示事件相关信息

Windows Forms 事件的相关委托定义都类似与System.EventHandler,如鼠标相关的委托MouseEventHandler,它的原型如下

public delegate void MouseEventHandler(object sender,MouseEventArgs e)

我们看下 MouseMove事件

所有与鼠标相关的事件(MouseMove MouseUp 等)与MouseEventHandler 委托结合工作,MouseEventArgs 扩展了EventArgs增加了一下属性

Button  获取哪个鼠标被单击

Clicks  获取鼠标被按下和释放的次数

X       鼠标单击处的 x 坐标

Y       鼠标单击处的 y 坐标

看代码

using System;
using System.Windows.Forms;
using System.Drawing; namespace DemoWinForm
{
static class App
{
static void Main()
{
Application.Run(new MainForm());
}
} public class MainForm : Form
{
public MainForm()
{
this.Text = "一个窗口";
this.Height = ;
this.Width = ;
BackColor = Color.Green;
Cursor = Cursors.Hand; this.MouseMove += new MouseEventHandler(MainForm_MouseMove);
} void MainForm_MouseMove(object sender, MouseEventArgs e)
{
this.Text = string.Format("当前鼠标坐标: [{0},{1}]",e.X,e.Y);
}
}
}

编译 csc /t:winexe MouseMoveDemo.cs

运行效果

本文完

最新文章

  1. [Spring] Spring配置文件中特殊字符的规定
  2. 6410实现网卡(DM9000A)收发功能及ARP协议实现
  3. git node(&npm)安装
  4. Intent 匹配规则
  5. laravel 外键schema RBAC
  6. jmeter学习预热
  7. boost muti-thread
  8. Jquery基础之动画操作
  9. 于Heroku平台部署maven webapp(java web)工程
  10. 快速构建Windows 8风格应用23-App Bar概述及使用规范
  11. mysql数据库主从备份
  12. 多人合作开发启动activity-----规范问题
  13. 分布式定时任务 -- elastic-job
  14. 关于SVN报错 svn: E170013 E125006: contains invalid filesystem format option 'addressing logical'
  15. Spring Security(三十六):12. Spring MVC Test Integration
  16. 有价值的IPFS博客
  17. Angular2 使用CLI创建新项目
  18. 使用Windows命令行启动关闭服务(net,sc用法)
  19. Python基础【day02】:元组和购物车练习的知识点
  20. 【*】深入理解redis主从复制原理

热门文章

  1. redis list 使用
  2. 搭建sentry(一个分布式日志聚合系统)
  3. delphi Components[i]清除所有edit控件中的内容
  4. Mesos 配置项解析
  5. Linux环境变量的修改(永久,暂时)
  6. uint8_t / uint16_t / uint32_t /uint64_t 是什么数据类型(转)
  7. 美H1B签证额满,硕士以上学位仍可申请
  8. TCP/IP协议原理与应用笔记18:构成子网和超网
  9. html笔记02:html,body { ……}
  10. Curl上传文件