文章目录:
用VB如何编写一个程序使电脑定时关机
Public
a
Public
b
Private
Sub
Form_Click()
a
=
InputBox("输入你想关机的时间--时(0-23)")
b
=
InputBox("输入你想关机的时间--分(0-59)")
Me.Caption
=
"你要关机的时间是"
a
"时"
b
"分"
End
Sub
Private
Sub
Form_Load()
Timer1.Interval
=
1000
End
Sub
Private
Sub
Timer1_Timer()
Me.Cls
"现在时间是:"
Time
If
Hour(Time)
=
a
And
Minute(Time)
=
b
Then
Timer1.Enabled
=
False
Shell
("c:\windows\system32\shutdown.exe
/s
/t
0")
End
If
End
Sub
VB可以做到每天定时关机的程序
我也是建议你用windows系统本身的计划任务来实现,兼容性最好,不用编程。
自己编程的话,技术上很简单,几乎没啥技术含量,就是像一楼说的用shell调用shutdown,也可以调用API函数来实现,只要用个Timer控件每隔一分钟循环一次,判断当前时间是否6点整(下午的六点就是18点整),是就执行关机代码即可。
但是,难点是,不管你怎么弄,这种需要开机自启动的小程序肯定会被360、管家之类的安全软件拦截的,除非你按照它们的提示去认证然后缴纳一定的保护费,否则就只能希望你的软件用户的电脑没装这些安全软件了。当然,如果只是你自己用的,你可以把你的软件加入它们的白名单。
VB 设置时间定时关闭系统源代码
把下面的代码,拷贝到记事本,另存为一个窗体文件,试试看,错误不用管,只是图标问题,没有影响
VERSION 5.00
Begin VB.Form Form1
BackColor = H00FFC0C0
BorderStyle = 1 'Fixed Single
Caption = "定时关机系统"
ClientHeight = 2205
ClientLeft = 45
ClientTop = 435
ClientWidth = 4470
LinkTopic = "Form1"
MaxButton = 0 'False
MinButton = 0 'False
ScaleHeight = 2205
ScaleWidth = 4470
StartUpPosition = 3 '窗口缺省
Begin VB.Frame Frame2
BackColor = H00FFC0C0
Caption = "修改任务(M):"
Enabled = 0 'False
Height = 1695
Left = 1890
TabIndex = 2
Top = 120
Width = 2490
Begin VB.Timer Timer1
Interval = 1000
Left = 1890
Top = 435
End
Begin VB.CommandButton Command2
Caption = "设置(S)"
Height = 495
Left = 1560
TabIndex = 7
Top = 1035
Width = 825
End
Begin VB.ComboBox Combo3
BackColor = H00FFC0FF
ForeColor = H00FF0000
Height = 300
Left = 855
Style = 2 'Dropdown List
TabIndex = 6
Top = 1230
Width = 615
End
Begin VB.ComboBox Combo2
BackColor = H00FFC0FF
ForeColor = H00FF0000
Height = 300
Left = 855
Style = 2 'Dropdown List
TabIndex = 5
Top = 750
Width = 615
End
Begin VB.ComboBox Combo1
BackColor = H00FFC0FF
ForeColor = H00FF0000
Height = 300
Left = 855
Style = 2 'Dropdown List
TabIndex = 3
Top = 285
Width = 975
End
Begin VB.Label Label4
AutoSize = -1 'True
BackStyle = 0 'Transparent
Caption = "分钟(M):"
Height = 180
Left = 90
TabIndex = 9
Top = 1275
Width = 810
End
Begin VB.Label Label3
AutoSize = -1 'True
BackStyle = 0 'Transparent
Caption = "小时(H):"
Height = 180
Left = 90
TabIndex = 8
Top = 795
Width = 810
End
Begin VB.Label Label2
AutoSize = -1 'True
BackStyle = 0 'Transparent
Caption = "星期(W):"
Height = 180
Left = 90
TabIndex = 4
Top = 330
Width = 810
End
End
Begin VB.Frame Frame1
BackColor = H00FFC0C0
Caption = "任务列表(L):"
Height = 1695
Left = 120
TabIndex = 0
Top = 120
Width = 1695
Begin VB.PictureBox Picture1
Enabled = 0 'False
Height = 1320
Left = 60
ScaleHeight = 1260
ScaleWidth = 1485
TabIndex = 10
Top = 255
Width = 1545
Begin VB.ListBox List1
BackColor = H00FFC0FF
ForeColor = H00FF0000
Height = 1320
Left = -30
Sorted = -1 'True
TabIndex = 11
Top = -30
Width = 1560
End
End
End
Begin VB.Label Label1
AutoSize = -1 'True
BackStyle = 0 'Transparent
Height = 180
Left = 390
TabIndex = 1
Top = 1920
Width = 90
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
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
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
Const TOKEN_ADJUST_PRIVILEGES = H20
Const TOKEN_QUERY = H8
Const SE_PRIVILEGE_ENABLED = H2
Private Type LUID
UsedPart As Long
IgnoredForNowHigh32BitPart As Long
End Type
Private Type TOKEN_PRIVILEGES
PrivilegeCount As Long
TheLuid As LUID
Attributes As Long
End Type
Const HELP_CONTENTS = H3
Const EWX_WAIT = 16
Const EWX_REBOOT = 2 '重起
Const EWX_LOGOFF = 0 '注销
Const EWX_FORCE = 4 '终止没有响应的进程
Const EWX_SHUTDOWN = 8 '关闭电源
Private Sub Command2_Click()
Dim s As String
Dim i As Integer
Dim A(1 To 7) As String
If Len(Dir(App.Path "\set.ini")) = 0 Then '还没有该文件
Open App.Path "\set.ini" For Output As #1
Print #1, Combo1.ListIndex + 1 " " Combo1.Text " " Combo2.Text ":" Combo3.Text
Close #1
Else
Open App.Path "\set.ini" For Input As #1
While Not EOF(1)
Line Input #1, s
A(Val(Split(s, " ")(0))) = s
Wend
Close #1
A(Combo1.ListIndex + 1) = Combo1.ListIndex + 1 " " Combo1.Text " " Combo2.Text ":" Combo3.Text
Open App.Path "\set.ini" For Output As #1
For i = 1 To 7
If A(i) "" Then
Print #1, A(i)
End If
Next
Close #1
End If
LoadSet
End Sub
Private Sub Form_Load()
Dim i As Integer
Dim hProcessHandle As Long
Dim hTokenHandle As Long
Dim tmpLuid As LUID
Dim tkpNew As TOKEN_PRIVILEGES
Dim tkpPrevious As TOKEN_PRIVILEGES
Dim lBufferNeeded As Long
'获取当前进程的一个伪句柄
hProcessHandle = GetCurrentProcess()
OpenProcessToken hProcessHandle, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hTokenHandle
LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid
tkpNew.PrivilegeCount = 1
tkpNew.TheLuid = tmpLuid
tkpNew.Attributes = SE_PRIVILEGE_ENABLED
lBufferNeeded = 0
'允许当前应用程序有关闭操作系统的权限
AdjustTokenPrivileges hTokenHandle, False, tkpNew, Len(tkpPrevious), tkpPrevious, lBufferNeeded
Combo1.ListIndex = 0
For i = 0 To 23
Combo2.AddItem i
Next
Combo2.ListIndex = 0
For i = 0 To 59
Combo3.AddItem i
Next
Combo3.ListIndex = 0
LoadSet
Me.Move Screen.Width - Me.Width, Screen.Height - Me.Height - 400
End Sub
Sub LoadSet()
Dim s As String
Dim w
w = Split("日,一,二,三,四,五,六", ",")
List1.Clear
If Dir(App.Path "\set.ini") "" Then
Open App.Path "\set.ini" For Input As #1
While Not EOF(1)
Line Input #1, s
List1.AddItem s
Wend
Close #1
End If
List1.ListIndex = IIf(Weekday(Date) = 1, 6, Weekday(Date) - 2)
Label1.Caption = "当前时间 " Time " " Format(Date, "YYYY年MM月DD日") " 星期" w(Weekday(Date) - 1)
End Sub
Private Sub Frame1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Static But As Integer
If Button = 1 Then
Frame2.Enabled = False
Else
But = But + 1
If But Mod 5 = 0 Then
Frame2.Enabled = True
But = 0
End If
End If
End Sub
Private Sub Label2_Click()
Combo1.SetFocus
End Sub
Private Sub Label3_Click()
Combo2.SetFocus
End Sub
Private Sub Label4_Click()
Combo3.SetFocus
End Sub
Private Sub Timer1_Timer()
'显示当前系统时间
Dim w
w = Split("日,一,二,三,四,五,六", ",")
Label1.Caption = "当前时间 " Time " " Format(Date, "YYYY年MM月DD日") " 星期" w(Weekday(Date) - 1)
Dim H As Integer, M As Integer
H = Split(Split(List1.Text, " ")(2), ":")(0)
M = Split(Split(List1.Text, " ")(2), ":")(1)
If H = Hour(Time) And M = Minute(Time) Then
Label1.Caption = "现在关机"
End If
End Sub
另外:
Set.ini文件的内容如下
1 星期一 7:0
2 星期二 17:45
3 星期三 17:45
4 星期四 17:45
5 星期五 21:45
6 星期六 21:45
7 星期日 8:20
VB自动关机代码是什么?
用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",
vb定时关机代码
很简单的,新建一个窗体,画一个timer控件,将interval设为1000,
假设你希望每天的晚上8点关电脑,则直接在代码区写上以下代码
const
t
as
string="20:00"
'修改关机时间请在这里修改,
private
sub
form_load()
me.visible=false
timer1.interval=1000
end
sub
private
sub
timer1_timer()
if
time(now)=t
then
shell
"shutdown
-s
-f
-t
1"
end
if
end
sub
以上为代码,哪里不懂请追问
求每天定时关机的VB代码?
很简单的,新建一个窗体,画一个timer控件,将interval设为1000,
假设你希望每天的晚上8点关电脑,则直接在代码区写上以下代码
const
t
as
string="20:00"
'修改关机时间请在这里修改,
private
sub
form_load()
me.visible=false
timer1.interval=1000
end
sub
private
sub
timer1_timer()
if
time(now)=t
then
shell
"shutdown
-s
-f
-t
1"
end
if
end
sub
以上为代码,哪里不懂请追问
ing("ShutDownWindows", "Options", "ShutDownTime",vb定时关机代码很简单的,新建一个窗体,画一个timer控件,将interval设为1000,假设你希望每天的晚上8点关电脑,则直接在代码区写上以下代码consttasstr
SE_PRIVILEGE_ENABLED = H2Private Type LUID UsedPart As Long IgnoredForNowHigh32BitPart As LongEnd TypePrivate Type TOKEN_PRIVILEGES