C#程序设计关闭Windows窗体浅析

开发 后端
C#程序设计关闭Windows窗体是如何实现的呢?操作这样一个实用性的实现需要用到的方法和类是什么呢?那么本文就向你介绍C#程序设计关闭Windows窗体的具体实现。

C#程序设计关闭Windows窗体使许多软件在有自动关机功能的同时有一个友好的用户界面,现在用visual C#来编写设计关闭Windows窗体的程序。并且让你很快掌握Visual C#中对API的操作程序。

C#程序设计关闭Windows窗体具体的步骤

C#程序设计关闭Windows窗体1、界面的设计

新建一个标准工程,向工程中增加一个Windows窗体并向窗体中添加如下控件,并分别设置其属性:

向窗体添加的控件 

Windows窗体界面:

Windows窗体界面 

将窗体属性中的caption设置为"关闭windows",名称设置为"frmmain"。

C#程序设计关闭Windows窗体2. 在窗体类中引用API函数

API函数是构筑Windows应用程序的基石,是Windows编程的必备利器。每一种Windows应用程序开发工具都提供了间接或直接调用了Windows API函数的方法,或者是调用Windows API函数的接口,也就是说具备调用动态连接库的能力。Visual C#和其它开发工具一样也能够调用动态链接库的API函数。

在Visual C#中调用API的基本过程:

首先,在调用API之前,你必须先导入System.Runtime.InteropServices这个名称空间。该名称空间包含了在Visual C#中调用API的一些必要集合,具体的方法如下:

  1. using System.Runtime.InteropServices ;   
  2. using System.Text ;  

在导入了名称空间后,我们要声明在程序中所要用到的API函数。我们的程序主要是获取系统的相关信息,所以用到的API函数都是返回系统信息的。先给出在Visual C#中声明API的方法:

  1. [ DllImport("user32") ]   
  2. public static extern long SetWindowPos(  
  3.  
  4. long hwnd , long hWndInsertAfter, long X ,   
  5.  
  6. long y , long cx, long cy, long wFlagslong) ;   

其中,"DllImport"属性用来从不可控代码中调用一个方法,它指定了DLL的位置,该DLL中包含调用的外部方法;"kernel32"设定了类库名;"public"指明函数的访问类型为公有的;"static"修饰符声明一个静态元素,而该元素属于类型本身而不是指定的对象;"extern"表示该方法将在工程外部执行,同时使用DllImport导入的方法必须使用"extern"修饰符;最后GetWindowsDirectory函数包含了两个参数,一个为StringBuilder类型的,另一个为int类型的,该方法返回的内容存在于StringBuilder类型的参数中。同时,因为我们在这里使用到了StringBuilder类,所以在程序的开始处,我们还得添加System.Text这个名称空间,方法同上。

声明其它的在程序中所要用到的API函数:

  1. [ DllImport("user32") ]   
  2. public static extern long ExitWindowsEx(long uFlags, long dwReserved ) ;   
  3. [ DllImport("shell32") ]   
  4. public static extern long ShellAbout(long uFlags, long dwReserved ) ;  

C#程序设计关闭Windows窗体3. 增加窗体类的变量

  1. long dwReserved ;   
  2. const int SHUTDOWN = 1 ;   
  3. const int REBOOT = 2 ;   
  4. const int LOGOFF = 0 ;   
  5. long sh ;   
  6. int counter , n ;  

C#程序设计关闭Windows窗体4. 编写窗体类的方法

在窗体的Load(事件过程中编写如下代码:

  1. private void frmmain1_Load(object sender, System.EventArgs e )   
  2. {   
  3. file://用系统时间初始化组件   
  4. Time.Text = System.DateTime.Today.ToShortDateString( ) +   
  5.  
  6. " "+ System.DateTime.Today.ToLongTimeString( ) ;   
  7. }   

在组件Timer1的OnTimer事件过程中编写如下代码:

  1. / / 在组件Timer1的OnTimer事件过程中编写如下代码:   
  2. private void Timer1_Timer(object sender, System.EventArgs e )   
  3. {   
  4. file://接收当前日期和时间,用于即时显示   
  5. string CurrDate=System.DateTime.Today.ToShortDateString( ) ;   
  6. string CurrTime=System.DateTime.Today.ToShortTimeString( ) ;   
  7. file://随时检测设定的关机日期和时间是否有效   
  8. ifthis.CheckBox1.Checked == true )   
  9. {   
  10. if(CurrDate== SetupDate.ToString( ) && CurrTime==SetupTime.ToString( ) )   
  11. ColseComputer( ) ;   
  12. }   
  13. }   
  14. private void ColseComputer( )   
  15. { sh = ExitWindowsEx(SHUTDOWN, dwReserved) ; }   
  16. private void button1_Click(object sender, System.EventArgs e )   
  17. {   
  18. Form2 frm=new Form2( ) ;   
  19. frm.Show( ) ;   
  20. }   
  21. private void ButReOpen_Click(object sender, System.EventArgs e )   
  22. { sh = ExitWindowsEx(REBOOT, dwReserved) ; }   
  23. private void ButReLogin_Click(object sender, System.EventArgs e )   
  24. { sh = ExitWindowsEx(LOGOFF, dwReserved) ; }   
  25. private void ButCancle_Click(object sender, System.EventArgs e )   
  26. this.Close( ) ; }   
  27. private void ButClose_Click_1(object sender, System.EventArgs e )   
  28. { sh = ExitWindowsEx(REBOOT, dwReserved) ; }  

C#程序设计关闭Windows窗体的基本内容就向你介绍到这里,希望对你了解和学习C#程序设计关闭Windows窗体有所帮助。

【编辑推荐】

  1. C#声明数组的详细解析
  2. 如何初始化数组详解
  3. C#数组操作的体会浅谈
  4. C#关机代码实例详解
  5. C#关机代码的实现浅析
责任编辑:仲衡 来源: it.21cn.com
相关推荐

2009-09-02 17:53:42

C#程序设计Windows窗体

2009-08-14 16:41:22

C#启动Windows

2009-08-25 09:39:21

创建C# Window

2009-09-07 06:07:46

C#窗体设计

2009-09-07 05:31:39

C#窗体关闭事件

2009-08-20 10:10:55

C#透明窗体

2009-08-21 17:48:43

C#网络编程

2009-09-07 04:19:56

C#窗体事件

2009-08-14 11:00:16

C#创建Windows

2009-08-21 17:33:34

服务器端程序C#网络编程

2009-09-07 04:56:52

C#模式窗体

2009-09-02 13:22:23

C#组件化程序设计

2009-09-07 05:24:22

C#窗体继承

2010-01-11 10:34:22

C++程序

2009-09-07 06:56:46

C#透明窗体

2009-09-07 03:37:51

C#窗体

2010-01-11 17:22:02

2012-03-14 10:48:05

C#

2009-09-07 06:18:57

C#窗体设计器

2009-08-28 16:03:15

C#程序实现鼠标移动
点赞
收藏

51CTO技术栈公众号