求各位达人,在C#里如何利用.framework的类实现在NT环境下关机!!! 用什么framework的类啊。调api快多了... 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 呵呵,是啊,但是如果要在NT下关机,那么还得认证,我是闲麻烦所以才。而且我也想知道如果才能用framework来实现。 一. 设计关闭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 ButClose_Click_1(object sender, System.EventArgs e ){ sh = ExitWindowsEx(REBOOT, dwReserved) ; } 看看这个。http://www.drcsharp.com/article_view.asp?id=21 各位好心人,请帮帮忙,遇到分页上的一个问题 关于控件继承问题 jmail组件 用 exchange 发送邮件失败 求经典算法,多多指教 新手委托问题 抓取论坛数据 为什么用了BUFFER用的时间还是一样的?不是说BUFFER可以提高性能吗? 在线等! DataGrid里面加的按钮列怎么删不掉数据,也不提示错误。我的sqlAdapter也创建了所有语句包括delete。不明白,非常急! c#中如何按下按钮后打开一个窗口,请指教! WPF引用自定义控件,xaml无法使用Name属性 请高手看一下,我的这个程序为什么不能关机? C#数据库中添加表
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 ButClose_Click_1(object sender, System.EventArgs e )
{ sh = ExitWindowsEx(REBOOT, dwReserved) ; }
http://www.drcsharp.com/article_view.asp?id=21