在平常写WinForm程序时,都是使用Visual Studio 的向导功能,选中项目类型为Windows Form Application,IDE就会为我们生成好代码框架。这无疑使非常方便的,但是却不利于我这样的新手了解程序的运行机理。

下面我试着,抛弃IDE生成的代码框架,从一个空的项目来创建一个Windows From的应用程序。

一 . 创建窗体

创建完Empty Project后,第一步就需要添加一些必要的引用:System , System.Drawing , System.Windows.Forms。然后向项目中添加一个名为HelloWorld的类 。

   1:  using System.Windows.Forms;
   2:   
   3:  namespace test
   4:  {
   5:      class HelloWord
   6:      {
   7:          public static void  Main() 
   8:          {
   9:              Form frm = new Form();
  10:              frm.Show();
  11:          }      
  12:      }
  13:  }

上面的代码中,在Main()方法中,通过实例化Form类来创建一个窗体,然后调用show()方法让其显示。在运行该程序前,需要先更改项目的输出类型为Windows Application。运行程序,一个窗口闪烁下就消失了。这是由于一个Windows应用程序结束时会销毁其所创建的所有窗口。我们知道,Windows应用程序时事件驱动,在没有触发关闭事件,应用程序是不应该被关闭的。显然的,上面的示例程序并没有进入到一个消息循环中,就已经结束了。

怎样让程序进入到消息循环?这时就需要一个神奇的方法”Run”了,它能让应用程序进入到消息循环中,接受输入事件。

改造上面的代码如下:

using System.Windows.Forms;namespace test
{class HelloWord{public static void Main(){Form frm = new Form();frm.Show();Application.Run();}}
}

运行程序,得到一个标准的窗口,能移动、最大化、最小化,但是在关闭窗口时却出现了问题,只是窗口消失了但是进程却并没有结束(在调试下运行会很容易的发现这点)。这是因为,关闭了窗口只是销毁了窗口,并不是结束程序。要想关闭窗口同时程序结束,将一个Form的实例作为参数传递给Run是一个很好的选择。

   1:  using System.Windows.Forms;
   2:   
   3:  namespace test
   4:  {
   5:      class HelloWord
   6:      {
   7:          public static void Main()
   8:          {
   9:              Form frm = new Form();
  10:              Application.Run(frm);
  11:          }
  12:      }
  13:  }

上面代码并没有调用show方法,这是因为当一个窗体作为参数时,Run会将该窗体设置为可见,并让该窗体进入到消息循环。当关闭窗体时,Run方法会返回到Main函数中,接着执行完Application.Run()后面的代码后,程序结束。

二.处理事件

Windows 应用程序是事件驱动的,上面已经创建好了窗体,下面来响应窗体的输入事件。

窗体一个比较重要的事件就是Paint事件,它在窗体被创建、窗体的 客户区失效或部分失效时会被触发。对该时间的处理是通过一个委托实现的。

public delegate void PaintEventHandler(object sender, PaintEventArgs e);

对Paint事件的处理,只需要定义一个和上面委托有相同的签名的静态的方法作为事件的处理程序,然后将该方法和Paint事件绑定即可。

   1:  using System.Windows.Forms;
   2:  using System.Drawing;
   3:   
   4:  namespace test
   5:  {
   6:      class HelloWord
   7:      {
   8:          public static void Main()
   9:          {
  10:              Form frm = new Form();
  11:              frm.Paint += new PaintEventHandler(frm_Paint); //注册事件处理程序
  12:              Application.Run(frm);
  13:          }
  14:   
  15:          //事件处理程序
  16:          static void frm_Paint(object sender, PaintEventArgs e)
  17:          {
  18:              Form frm = (Form)sender;
  19:              Graphics g = e.Graphics;
  20:              g.Clear(Color.Black);
  21:              g.DrawString("Hello Word", frm.Font, Brushes.White, new Point(20, 20));
  22:          }
  23:      }
  24:  }

和Paint事件类似,也可以处理窗体的鼠标点击事件

   1:  using System;
   2:  using System.Windows.Forms;
   3:  using System.Drawing;
   4:   
   5:  namespace test
   6:  {
   7:      class HelloWord
   8:      {
   9:          public static void Main()
  10:          {
  11:              Form frm = new Form();
  12:              frm.Paint += new PaintEventHandler(frm_Paint); //注册Paint事件处理程序
  13:              frm.Click += new System.EventHandler(frm_Click);//注册Click事件处理程序
  14:              Application.Run(frm);
  15:          }
  16:   
  17:          // Click事件处理程序
  18:          static void frm_Click(object sender, EventArgs e)
  19:          {
  20:              Form frm = (Form)sender;
  21:              //在标题栏显示鼠标单击的位置
  22:              frm.Text = Cursor.Position.X.ToString() + ":" + Cursor.Position.Y.ToString();
  23:          }
  24:   
  25:          //事件处理程序
  26:          static void frm_Paint(object sender, PaintEventArgs e)
  27:          {
  28:              Form frm = (Form)sender;
  29:              Graphics g = e.Graphics;
  30:              g.Clear(Color.Black);
  31:              g.DrawString("Hello Word", frm.Font, Brushes.White, new Point(20, 20));
  32:          }
  33:      }
  34:  }

运行效果

三.继承窗体

上面的代码看上去已经实现了一个窗体,能为窗体赋于某些属性,响应一些事件的输入,实际则不然。因为我们只是通过创建Form类的一个实例来实现了一个窗体,这是不够,Form的某些功能是用protected保护的,以此要想实现窗体的全部功能就需要“变成”窗体,继承Form创建一个新的类则是一个很好的选择。

   1:  using System;
   2:  using System.Drawing;
   3:  using System.Windows.Forms;
   4:   
   5:  namespace test
   6:  {
   7:      class InheritClass:Form
   8:      {
   9:          public static void Main()
  10:          {
  11:              Application.Run(new InheritClass());
  12:          }
  13:   
  14:          public InheritClass()
  15:          {
  16:              Text = "通过继承创建窗体";
  17:              BackColor = Color.Black;
  18:          }
  19:      }

上面代码InheritClass继承自Form,然后实例化InheritClass来创建窗体。这也是使用Windows Form类库在C#中创建一个窗体的正式的方法。

通过继承来实现的窗体的好处之一就是能够访问Form中受保护的成员,例如可以重写(override)OnPaint方法,这样就不必处理Paint事件了。

   1:  using System;
   2:  using System.Drawing;
   3:  using System.Windows.Forms;
   4:   
   5:  namespace test
   6:  {
   7:      class InheritClass:Form
   8:      {
   9:          public static void Main()
  10:          {
  11:              Application.Run(new InheritClass());
  12:          }
  13:   
  14:          public InheritClass()
  15:          {
  16:              Text = "通过继承创建窗体";
  17:              BackColor = Color.Black;
  18:          }
  19:   
  20:          protected override void OnPaint(PaintEventArgs e)
  21:          {
  22:              //base.OnPaint(e);
  23:              Graphics g = e.Graphics;
  24:              g.DrawString("Hello World", Font, Brushes.Yellow, new Point(0, 0));
  25:          }
  26:      }
  27:  }

四.最后

上面的代码虽然也能工作,但是看起来总有些不够整洁,特别是Main方法的位置,总是让人有些别扭。对代码改造下,让其看起来整洁些。

再建立一个program类,将程序的入口Main方法放入到该类中

这是program类

   1:  using System;
   2:  using System.Windows.Forms;
   3:   
   4:  namespace test
   5:  {
   6:      class program
   7:      {
   8:          public static void Main()
   9:          {
  10:              Application.Run(new InheritClass());
  11:          }
  12:      }
  13:  }

InheritClass类

   1:  using System;
   2:  using System.Drawing;
   3:  using System.Windows.Forms;
   4:   
   5:  namespace test
   6:  {
   7:      class InheritClass:Form
   8:      {
   9:          public InheritClass()
  10:          {
  11:              Text = "通过继承创建窗体";
  12:              BackColor = Color.Black;
  13:          }
  14:   
  15:          protected override void OnPaint(PaintEventArgs e)
  16:          {
  17:              //base.OnPaint(e);
  18:              Graphics g = e.Graphics;
  19:              g.DrawString("Hello World", Font, Brushes.Yellow, new Point(0, 0));
  20:          }
  21:      }
  22:  }

是不是有点像IDE自动生成的代码了呢?

转载于:https://www.cnblogs.com/lovelq522/archive/2012/01/16/2324176.html

C#WindowsForm之创建窗体相关推荐

  1. 开始VC之路--创建窗体

    第一讲 用Create()方法新建一个窗体应用程序 一般来讲,大多数Windows应用程序的界面都是由一个或数个窗体构成. 而VC++中提供了丰富的类库,用于创建Windows窗体应用程序. 我们一般 ...

  2. Delphi 下用Windows API 创建窗体

    Delphi 下用Windows API 创建窗体 副标题: 作者:佚名 文章来源:大富翁 点击数:119 更新时间:2005-2-25     Delphi 下用Windows API 创建窗体 / ...

  3. 创建窗体时出错。有关详细信息,请参阅 Exception.InnerException

    创建窗体时出错.有关详细信息,请参阅 Exception.InnerException.错误为: 未能加载文件或程序集"xxx, Version=1.0.0.0, Culture=neutr ...

  4. java添加窗体中_java中利用JFrame创建窗体 【转】

    Java代码 publicclassTest(){ publicstaticvoidmain(String[] args){ JFrame frame = newJFrame(); JPanel pa ...

  5. access创建窗体特别慢_Access 2016 创建窗体

    Access2016有许多种简单的方式来创建窗体 将Access(和其他桌面数据库系统)从大多数客户端/服务器数据库管理系统(DBMSs)中区别开来的是创建窗体的能力.大多数客户端/服务器系统要求您使 ...

  6. win 32 APP 项目简单创建窗体

    /*Win 32 项目创建窗体的过程 *创建窗体主要分为这么几个过程 *1. 声明一个窗体类,并完成窗体基本元素(包括窗体背景颜色,光标等)的设置 *2. 注册这个窗体类 *3. 创建这个窗体 *4. ...

  7. java jframe创建窗体_Java中利用JFrame创建窗体

    1. 一个简单例子: Java代码   public class Test(){ public static void main(String[] args){ JFrame frame = new  ...

  8. java 怎么创建窗体_java中创建窗体的方法

    java中创建窗体的方法 发布时间:2020-06-16 11:26:04 来源:亿速云 阅读:102 作者:Leah 这篇文章给大家分享的是有关java中创建窗体的方法,小编觉得挺实用的,因此分享给 ...

  9. c#中在工作线程创建窗体并操作

    实例1 public void CycleShow()//循环绘图         { Task.Factory.StartNew(() =>             {          // ...

最新文章

  1. 周怎么换算成月_您每天需要多少能量,怎么知道自己摄入多少能量
  2. 比特币现金支持者为BCH引入了各种新概念
  3. concurrent (二)AQS
  4. Boost:字符串的RLE压缩的测试程序
  5. 数据清理最终实现了自动化
  6. python开源项目新手_10大Python开源项目推荐(Github平均star2135)
  7. 执行h2o的python命令时出现:TypeError: ‘NoneType‘ object is not callable
  8. python 数据写入json文件时中文显示Unicode编码问题
  9. HDU - 4607 Park Visit (树的直径)
  10. Vs code 通用插件
  11. java中多重循环和break、continue语句
  12. SAP系统 会计科目主数据详解
  13. 笔记本计算机的连接无线网络连接,笔记本电脑连接wifi的方法步骤
  14. 学生学籍系统 按班级查找按学号查找
  15. Gobelieve 架构(转载)
  16. window自带的计算机应用程序,Win10系统电脑不小心将自带的应用程序卸载了该怎么恢复...
  17. SpringBoot 整合 Elasticsearch
  18. PowerBI中使用SVG图标简单介绍
  19. FPGA入门到实战-学习笔记
  20. vue:将Element UI的时间选择器(DatePicker)的Sat Jan 01 2022 00:00:00 GMT+0800 (中国标准时间)转化为XXXX-XX-XX的格式

热门文章

  1. dos命令关闭所有dos窗口
  2. 使用深度V8.1 系统后打开部分文件夹缓慢
  3. python isalpha()
  4. 比較++和+的运算符优先级
  5. 约瑟夫问题的循环链表实现
  6. VB.Net中关于数组赋值
  7. 微软网站开始出现大量的关于Office 2007的相关下载
  8. bootstrap 树形表格渲染慢_bootstrap-table-treegrid数据量较大时渲染太久了
  9. python计算运动会某个参赛选手的得分。数据保存在文件中_Python基础教程之第二章---变量和字符串(1) 搬运...
  10. python调用数据库判断_python 访问数据库 笔记