用VB实现控制电脑自动关机,自动重启,自动注销,自动待机等代码
设定状态
SetSuspendState
是否支持休眠
IsPwrHibernateAllowed
是否支持待机
IsPwrSuspendAllowed
看MSDN的Power Management部分
ColdMooon
(转载)
'在窗体内
'放置一个时间控件,两个标签控件和一个文体框控件,和两个单选按钮,两个命令按钮
Option Explicit
Dim txtTime '保存输入时间
Dim nowTime '保存实时时间
Dim oldTime '保存开始定时时间
Private Sub Form_Load()
me.caption="定时关机"
me.label1.caption="关机时间:"
me.Option1.caption="在指定时间关机(时间格式:00:00:00)"
me.Option2.caption="延迟指定的时间关机"
me.command1.caption="确定"
me.command2.caption="取消"
Me.Text1 = GetSetting("ShutDownWindows", "Options", "ShutDownTime", "1:00")
me!timer1.interval=1000
End Sub
Private Sub Command1_Click()
oldTime = Time
If Not IsDate(Text1.Text) Then '用IsData函数判断输入的时间格式
MsgBox "你所输入的不是时间格式,请修改!", , "Wrong"
ElseIf Option1.Value Then
txtTime = TimeValue(Text1.Text)
Timer1.Enabled = True '启动定时器
Me.WindowState = 1 '最小化窗体
Me.Caption = "定时关机-启动"
ElseIf Option2.Value Then
Me.WindowState = 1
Me.Caption = "定时关机-暂停"
End If
End Sub
Private Sub Command2_Click()
Unload Me
End Sub
Private Sub Form_Unload(Cancel As Integer)
SaveSetting "ShutDownWindows", "Options", "ShutDownTime", Me.Text1
End Sub
Private Sub Timer1_Timer()
nowTime = Time
Me.label2.Caption = Time
If Option1.Value Then
If DateDiff("s", nowTime, txtTime) <= 0 And DateDiff("s", nowTime, txtTime) >= -60 Then '用DateDiff函数判断是否到时间了
ShutDownPC True
Timer1.Enabled = False
End If
End If
End Sub
'在标准模块内
Private Const EWX_LOGOFF = 0
Public Const EWX_SHUTDOWN = 1
Public Const EWX_POWEROFF = 8 '此参数在VB自带API浏览器并未提供,值得注意,如果没有此参数在Win2kServer会死在关机屏幕下
Private Const EWX_REBOOT = 2
Private Const EWX_FORCE = 4
Private Const TOKEN_ADJUST_PRIVILEGES = &H20
Private Const TOKEN_QUERY = &H8
Private Const SE_PRIVILEGE_ENABLED = &H2
Private Const ANYSIZE_ARRAY = 1
Private Const VER_PLATFORM_WIN32_NT = 2
Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128
End Type
Type LUID
LowPart As Long
HighPart As Long
End Type
Type LUID_AND_ATTRIBUTES
pLuid As LUID
Attributes As Long
End Type
Type TOKEN_PRIVILEGES
PrivilegeCount As Long
Privileges(ANYSIZE_ARRAY) As LUID_AND_ATTRIBUTES
End Type
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
Private Declare Function LookupPrivilegeValue Lib "advapi32" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long
Private Declare Function AdjustTokenPrivileges Lib "advapi32" (ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, PreviousState As TOKEN_PRIVILEGES, ReturnLength As Long) As Long
Public Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (ByRef lpVersionInformation As OSVERSIONINFO) As Long
'Detect if the program is running under Windows NT 检测是否为NT
Public Function IsWinNT() As Boolean
Dim myOS As OSVERSIONINFO
myOS.dwOSVersionInfoSize = Len(myOS)
GetVersionEx myOS
IsWinNT = (myOS.dwPlatformId = VER_PLATFORM_WIN32_NT)
End Function
'set the shut down privilege for the current application 为当前应用程序设置关机权限
Private Sub EnableShutDown()
Dim hProc As Long
Dim hToken As Long
Dim mLUID As LUID
Dim mPriv As TOKEN_PRIVILEGES
Dim mNewPriv As TOKEN_PRIVILEGES
hProc = GetCurrentProcess()
OpenProcessToken hProc, TOKEN_ADJUST_PRIVILEGES + TOKEN_QUERY, hToken
LookupPrivilegeValue "", "SeShutdownPrivilege", mLUID
mPriv.PrivilegeCount = 1
mPriv.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED
mPriv.Privileges(0).pLuid = mLUID
' enable shutdown privilege for the current application 赋予当前应用程序关机权限
AdjustTokenPrivileges hToken, False, mPriv, 4 + (12 * mPriv.PrivilegeCount), mNewPriv, 4 + (12 * mNewPriv.PrivilegeCount)
End Sub
' Shut Down PC 关闭PC
Public Sub ShutDownPC(Force As Boolean)
Dim ret As Long
Dim Flags As Long
Flags = EWX_SHUTDOWN Or EWX_POWEROFF
If Force Then Flags = Flags + EWX_FORCE
If IsWinNT Then EnableShutDown
ExitWindowsEx Flags, 0
End Sub
'Restart PC 重起PC
Public Sub RebootPC(Force As Boolean)
Dim ret As Long
Dim Flags As Long
Flags = EWX_REBOOT
If Force Then Flags = Flags + EWX_FORCE
If IsWinNT Then EnableShutDown
ExitWindowsEx Flags, 0
End Sub
'Log off the current user 注消(登出)当前用户
Public Sub LogOff(Force As Boolean)
Dim ret As Long
Dim Flags As Long
Flags = EWX_LOGOFF
If Force Then Flags = Flags + EWX_FORCE
ExitWindowsEx Flags, 0
End Sub
'自动开机说明:(以CMOS Setup Utility - Copyright (C) 1984-2001 Award Software 为例)
'开机按Delete键进入CMOS Setup程序
'把选择移到Power Management Setup(电源管理设置), 按回车
'然后把选择移到Resume by Alarm,按PageDown键选择Enable
'移到下一行把Date(of Month) Alarm 设为0
'移到下一行把Time(hh:mm:ss) Alarm 设为[自己的开机时间],例如:8:0:0(早上8点开机)
新建两个 label1 的组,一个是 label1(0) 另一个是 label1(1)
新建三个 command
新建一个 timer
代码如下:
Dim dd As Integer
Dim ff As Boolean
Private Sub Command1_Click()
Timer1.Enabled = False
Timer1.Interval = 3000
Timer1.Enabled = True
ff = True
Label1(1).Caption = "说:我服了!"
Label1(1).ForeColor = &HFF
Command2.Visible = False
Command1.Visible = False
Command3.Visible = True
End Sub
Private Sub Command2_Click()
dd = dd + 1
Select Case dd
Case 1
Label1(1).Caption = "你真的不说吗?"
Case 2
Label1(1).Caption = "真的忍心不想说吗?"
Case 3
Label1(1).Caption = "我好想让你说啊!你不说我我好失望的啊!"
Case 4
Label1(1).Caption = "真的不说吗,那我要关你的电脑了啊!"
Case 5
Label1(1).Caption = "离关机还有 60 秒。"
Timer1.Interval = 1000
Timer1.Enabled = True
Case Else
Label1(1).Caption = "离关机还有 " & 65 - dd & " 秒。"
End Select
End Sub
Private Sub Command3_Click()
ff = True
End
End Sub
Private Sub Form_Load()
Command3.Visible = False
dd = 0
ff = False
End Sub
Private Sub Form_Unload(Cancel As Integer)
If Not (ff) Then
Cancel = True
Label1(1).Caption = "你关不掉的。"
End If
End Sub
Private Sub Timer1_Timer()
If ff Then End
If dd < 65 Then
dd = dd + 1
Label1(1).Caption = "离关机还有 " & 65 - dd & " 秒。"
Else
Shell "shutdown -s -t 0"
ff = True
End If
End Sub
InitlateSystemShutdown