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

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

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

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

新建一个标准工程,向工程中增加一个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. if( this.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窗体有所帮助。

分享标题:C#程序设计关闭Windows窗体浅析
文章转载:http://www.hantingmc.com/qtweb/news27/351427.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联