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

ASP.NET
asp.net下用服务器端代码解决浏览器兼容性问题
asp.net 安全的截取指定长度的html或者ubb字符串
asp.net 在线编辑word文档 可保存到服务器
asp.net 提高网站速度及如何利用缓存
asp.net 修改/删除站内目录操作后Session丢失问题
asp.net URL重写简化版 速学URL重写
asp.net EncryptHelper 加密帮助类
asp.net JSONHelper JSON帮助类
C# 调用存储过程简单完整的实例代码
vs2008 安装失败的总结与分享
HttpHandler HttpModule入门篇
ASP.NET(AJAX+JSON)实现对象调用
Asp.net 基于Cookie简易的权限判断
asp.net通过HttpModule自动在Url地址上添加参数
asp.net 字符串、二进制、编码数组转换函数
ASP.NET操作Excel备忘录
记录游客页面访问IP的简易实现代码 (asp.net+txt)
比较简单的将数据信息导入wrod文档方案(C# for word)
增加asp.net应用程序性能的20种方法(简单有效)
ASP.NET 图片防盗链的实现原理分析

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 38 ::
收藏到网摘: 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