湖北恩施的天气预报:用VB编写关机程序怎么写呀?

来源:百度文库 编辑:高考问答 时间:2024/05/06 03:13:15

如何关闭/重新启动计算机?

16位Windows用ExitWindows() API函数,而32位Windows则用ExitWindowsEx().32位版本比16位版本多了更多的控制及选项,包括注销及关机。

Declare Function ExitWindows Lib "user" (ByVal uFlags As Long, ByVal _
dwReserved As integer) As integer
Const EW_REBOOTSYSTEM = &H43
Const EW_RESTARTWINDOWS = &H42

Sub Command1_Click()

Dim iAns As Integer
Dim rVal As Integer
Dim iButtonType as Integer

iButtonType = 4 + 32 ' vbYesNo + vbQuestion

' Ask if the user is sure they want to exit.
iAns = MsgBox("Are you sure you want to exit windows?", iButtonType, _
"Exit Windows")
If iAns = 6 Then ' Yes pressed
' Call the exit function to Reboot.
rVal = ExitWindows(EW_REBOOTSYSTEM, 0)
End If

End Sub

***** 32位的例子 *****
In a project with 1 commandbutton, place the following code:
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, _
ByVal dwReserved As Long) As Long
Private Const EWX_LOGOFF = 0
Private Const EWX_SHUTDOWN = 1
Private Const EWX_REBOOT = 2
Private Const EWX_FORCE = 4

Private Sub Command1_Click()

Dim iAns As Integer
Dim rVal As Long

' Ask if the user is sure they want to exit.
iAns = MsgBox("Are you sure you want to exit windows?", vbQuestion Or _
vbYesNo, "Exit Windows")
If iAns = vbYes Then
rVal = ExitWindowsEx(EWX_SHUTDOWN, 0&)
End If

End Sub

查看ExitWindowsEx的详细用法

Private Declare Function ExitWindowsEx Lib "user32"(ByVa
l uFlags As Long , ByVal dwReserved As Long) As Long
Dim uFlags As Long
Dim dwReserved As Long
Dim ShutDownTime
Dim X
Private Sub CmdBegin_Click()
ShutDownTime = txtTime.Text
If ShutDownTime = "" Then Exit Sub
If Not IsDate(ShutDownTime) Then
MsgBox "无效的时间格式!"
Else
’把文本格式的时间转化为曰期型
ShutDownTime = Cdate(ShutDownTime)
End If
End Sub
Private Sub Timer1_Timer()
’每隔一秒钟对标签上的时间进行刷新一次
lblTime.Caption = Time
If Time >= ShutDownTime Then
X = ExitWindowsEx(uFlags, dwReserved)
End If
End Sub
Private Sub Form_Load()
ShutDownTime = ""
uFlags = 1
dwReserved = 1
End Sub

shell "shutdown -s"

晕,找到那个用来关机的EXE文件。
shell调用一下,OK。