当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > Visual C#设计多功能关机程序

ASP.NET
DataList绑定到Row[]行集合的问题的方法
充分利用ASP.NET的三种缓存提高站点性能的注意方法
asp.net下文件上传和文件删除的代码
asp.net下日期加减的方法
asp.net动态载入用户控件的方法
asp.net下定制日期输出格式的代码
C#正则用法两例
asp.net图片上传生成缩略图的注意事项
ASP.NET中高质量缩略图的生成代码
DataList 中动态绑定服务器子控件的代码
asp.net下URL网址重写成.html格式、RSS、OPML的知识总结
使用UserControl做网站导航条的思路 分析
ASP.NET中使用AspnetAccessProvider
asp.net下实现URL重写技术的代码
为大家经常为md5加密过的常用admin,admin888,0000密码
利用MS AJAX注册Javascript命名空间并创建类
asp.net(c#)中取得文件物理路径
垃圾代码二三行 ASPX小马
.NET 2.0获取配置文件AppSettings和ConnectionStrings节数据的方法
.NET c# 单体模式(Singleton)

ASP.NET 中的 Visual C#设计多功能关机程序


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 154 ::
收藏到网摘: n/a

许多软件都有自动关机功能,特别是在长时间下载的时候,这个功能可是使你不用以守候在计算机前面,而电脑却能按照您事先的设定自动关闭。现在我们用visual C#来编写一个多功能的关机程序。该程序具有:定时关机、倒计时关机、关机提醒、系统信息获取等四项功能, 可设定关机时间精确到秒。并且让你很快掌握Visual C#中对API的操作程序。  一. 设计关闭Windows窗体  1. 界面的设计  新建一个标准工程,向工程中增加一个Windows窗体并向窗体中添加如下控件,并分别设置其属性: 控件名 类别 Text 控件名 类别 Text CheckBox1 CheckBox 自动关机 GroupBox1 GroupBox 当前系统时间 CheckBox1 CheckBox 倒计时执行操作 GroupBox2 GroupBox 设定时间 CheckBox1 CheckBox 定时报警 TxtTime TextBox ButCancle Button 取消 SetupTime DateTimePicker ButReOpen Button 重新启动 SetupDate DateTimePicker ButClose Button 关机 Timer1 Timer 100 ButSysInto Button 系统信息 ButReLogin Button 注消   Windows窗体界面:  将窗体属性中的caption设置为"关闭windows",名称设置为"frmmain"。   2. 在窗体类中引用API函数  API函数是构筑Windows应用程序的基石,是Windows编程的必备利器。每一种Windows应用程序开发工具都提供了间接或直接调用了Windows API函数的方法,或者是调用Windows API函数的接口,也就是说具备调用动态连接库的能力。Visual C#和其它开发工具一样也能够调用动态链接库的API函数。  在Visual C#中调用API的基本过程:  首先,在调用API之前,你必须先导入System.Runtime.InteropServices这个名称空间。该名称空间包含了在Visual C#中调用API的一些必要集合,具体的方法如下:  using System.Runtime.InteropServices ;  using System.Text ;   在导入了名称空间后,我们要声明在程序中所要用到的API函数。我们的程序主要是获取系统的相关信息,所以用到的API函数都是返回系统信息的。先给出在Visual C#中声明API的方法:[ DllImport("user32") ] public static extern long SetWindowPos(long hwnd , long hWndInsertAfter, long X , 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函数:[ DllImport("user32") ]public static extern long ExitWindowsEx(long uFlags, long dwReserved ) ;[ DllImport("shell32") ] public static extern long ShellAbout(long uFlags, long dwReserved ) ;   3. 增加窗体类的变量long dwReserved ;const int SHUTDOWN = 1 ;const int REBOOT = 2 ;const int LOGOFF = 0 ;long sh ;int counter , n ;   4. 编写窗体类的方法  在窗体的Load(事件过程中编写如下代码:private void frmmain1_Load(object sender, System.EventArgs e ){file://用系统时间初始化组件Time.Text = System.DateTime.Today.ToShortDateString( ) + " "+ System.DateTime.Today.ToLongTimeString( ) ;}  在组件Timer1的OnTimer事件过程中编写如下代码:/ / 在组件Timer1的OnTimer事件过程中编写如下代码: private void Timer1_Timer(object sender, System.EventArgs e ){ file://接收当前日期和时间,用于即时显示 string CurrDate=System.DateTime.Today.ToShortDateString( ) ;string CurrTime=System.DateTime.Today.ToShortTimeString( ) ;file://随时检测设定的关机日期和时间是否有效 if( this.CheckBox1.Checked == true ){if(CurrDate== SetupDate.ToString( ) && CurrTime==SetupTime.ToString( ) )ColseComputer( ) ;}}private void ColseComputer( ) { sh = ExitWindowsEx(SHUTDOWN, dwReserved) ; }private void button1_Click(object sender, System.EventArgs e ){Form2 frm=new Form2( ) ;frm.Show( ) ;}private void ButReOpen_Click(object sender, System.EventArgs e ){ sh = ExitWindowsEx(REBOOT, dwReserved) ; }private void ButReLogin_Click(object sender, System.EventArgs e ){ sh = ExitWindowsEx(LOGOFF, dwReserved) ; }private void ButCancle_Click(object sender, System.EventArgs e ){ this.Close( ) ; }private void But