标题:[原创]定时关机程序
只看楼主
HaCk的旋律
Rank: 1
等 级:新手上路
威 望:1
帖 子:73
专家分:0
注 册:2006-12-1
 问题点数:0 回复次数:18 
[原创]定时关机程序

Option Explicit
Dim textime, timetext As Integer, mm As Integer, ss As Integer

Private Sub Command1_Click()
textime = Text1.Text
timetext = textime * 60
Timer1.Enabled = True

End Sub

Private Sub Form_Resize()
Form1.Height = 1095
Form1.Width = 4155
End Sub


Private Sub Timer1_Timer()

mm = timetext \ 60
ss = timetext Mod 60
Label1.Caption = Str(mm) & "分" & Str(ss) & "秒"
If mm = 0 And ss = 0 Then
Text1.Text = ""
Timer1.Enabled = False
Command1.Enabled = True
Shell "shutdown -S -t 00"
End If
timetext = timetext - 1
End Sub




这个是给我的小站的贺礼...

虽然礼很轻..但是..这可是我的进步啊...大家捧捧场..

[此贴子已经被作者于2007-2-20 20:18:06编辑过]

搜索更多相关主题的帖子: Sub timetext Integer 
2007-02-20 16:41
jrs123
Rank: 2
等 级:论坛游民
威 望:1
帖 子:627
专家分:14
注 册:2006-9-5
得分:0 

试了一下,好象关不掉,请问楼主,如何控制关闭时间?

2007-02-20 22:40
HaCk的旋律
Rank: 1
等 级:新手上路
威 望:1
帖 子:73
专家分:0
注 册:2006-12-1
得分:0 
If mm = 0 And ss = 0 Then '如果mm和ss都走到零了..那么就关闭嘎..
Text1.Text = ""
Timer1.Enabled = False
Command1.Enabled = True
Shell "shutdown -S -t 00" '调用SHELL函数 shutdown -S -t 00为关闭计算机的意思啊


不知道我这样解释是不是....

我在这边是可以关闭的啊...

做我所想! 欢迎大家加我好友@ Mailto: buxchang@
2007-02-20 22:55
121038
Rank: 1
等 级:新手上路
威 望:2
帖 子:414
专家分:0
注 册:2005-8-3
得分:0 

楼主的方法太复杂了,要实现自动关机是很简单的,7行代码,整个关机就可以了

Public ShutTime As String '用于存储关机的时间,格式是"小时-分钟"

Private Sub Form_Load()
ShutTime = "20-08" '晚上8点30关
Timer1.Interval = 60000
End Sub

Private Sub Timer1_Timer()
Dim s() As String
s = Split(ShutTime, "-")
If Hour(Now()) = Val(s(0)) And Minute(Now()) = Val(s(1)) Then
Shell "shutdown -S -t 00"
End If
End Sub


2007-02-21 20:08
121038
Rank: 1
等 级:新手上路
威 望:2
帖 子:414
专家分:0
注 册:2005-8-3
得分:0 
但是我的代码不是刚好那个时间关机的,可能会迟几秒,如果要很精确,非要0秒时就关机,可以在Timer1.Interval = 60000事件之上再加3行代码

Do While Second(Now()) <> 0
DoEvents
Loop

2007-02-21 20:11
zhulei1978
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:53
帖 子:1351
专家分:1200
注 册:2006-12-17
得分:0 
请问是DoEvents什么意思

其实我就是改变社会风气,提高少女素质,刺激电影市道,提高年轻人内涵,玉树临风,风度翩翩的整蛊专家,我名叫古晶,英文名叫JingKoo!
2007-02-21 20:19
121038
Rank: 1
等 级:新手上路
威 望:2
帖 子:414
专家分:0
注 册:2005-8-3
得分:0 
处理事件~在我这里没什么实际用处
在其他地方,比如吧,代码是搜索一个文件夹,如果直接运行,有时候窗口就会出现假死的现象,那么在代码前加行doevents就好了!

2007-02-21 23:40
Joforn
Rank: 6Rank: 6
等 级:贵宾
威 望:23
帖 子:1242
专家分:122
注 册:2007-1-2
得分:0 

'这个是关机控制模块
Option Explicit

Private Declare Function ExitWindowsEx Lib "user32" (ByVal dwOptions As Long, ByVal dwReserved As Long) As Long
Private Declare Function GetLastError Lib "kernel32" () As Long
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 Sub SetLastError Lib "kernel32" (ByVal dwErrCode As Long)
Private Declare Function GetVersion Lib "kernel32" () As Long

Public glngWhichWindows32 As Long

'以下是调用ShutDownWin时Mode的值:
Public Const EWX_LogOff As Long = 0 '注销
Public Const EWX_SHUTDOWN As Long = 1 '关机
Public Const EWX_REBOOT As Long = 2 '重启
Public Const EWX_FORCE As Long = 4 '强行关闭正在运行的任务;可以用 EWX_Force OR EWX_SHUTDOWN 即强行关闭所有运行中的程序关闭计算机电源。

Private Type LUID
UsedPart As Long
IgnoredForNowHigh32BitPart As Long
End Type

Private Type LUID_AND_ATTRIBUTES
TheLuid As LUID
Attributes As Long
End Type

Private Type TOKEN_PRIVILEGES
PrivilegeCount As Long
TheLuid As LUID
Attributes As Long
End Type

Public Sub ShutDownWin(ByVal Mode As Integer)

'********************************************************************
'* 这个过程允许程序在Windows下关机或者重新启动、注销当前用户。
'* 注:在Win2K、WinXP+VB6下测试通过,Win98下待测试。
'* Joforn*Ron
'* Email:Joforn@sohu.com
'* QQ:42978116
'********************************************************************

Const TOKEN_ADJUST_PRIVILEGES = &H20
Const TOKEN_QUERY = &H8
Const SE_PRIVILEGE_ENABLED = &H2

Dim hdlProcessHandle As Long
Dim hdlTokenHandle As Long
Dim tmpLuid As LUID
Dim tkp As TOKEN_PRIVILEGES
Dim tkpNewButIgnored As TOKEN_PRIVILEGES
Dim lBufferNeeded As Long
Dim lngVersion As Long

lngVersion = GetVersion()
If ((lngVersion And &H80000000) = 0) Then
'使用SetLastError函数设置错误代码为0。
'这样做,GetLastError函数如果没有错误会返回0
SetLastError 0
' GetCurrentProcess函数设置 hdlProcessHandle变量
hdlProcessHandle = GetCurrentProcess()
If GetLastError <> 0 Then
MsgBox "GetCurrentProcess error==" & GetLastError
End If
OpenProcessToken hdlProcessHandle, (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), hdlTokenHandle
If GetLastError <> 0 Then
MsgBox "OpenProcessToken error==" & GetLastError
End If
' 获得关机优先权的LUID
LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid
If GetLastError <> 0 Then
MsgBox "LookupPrivilegeValue error==" & GetLastError
End If
tkp.PrivilegeCount = 1 ' 设置一个优先权
tkp.TheLuid = tmpLuid
tkp.Attributes = SE_PRIVILEGE_ENABLED
' 对当前进程使能关机优先权
AdjustTokenPrivileges hdlTokenHandle, False, tkp, Len(tkpNewButIgnored), tkpNewButIgnored, lBufferNeeded
If GetLastError <> 0 Then
MsgBox "AdjustTokenPrivileges error==" & GetLastError
End If
End If
ExitWindowsEx (Mode), &HFFFF
End Sub
'这段代码我在一个定时关机的程序中使用了一年多,经测试暂时还没有发现有问题,但其中有几个函数在Win98中没有,所以可能用Win98系统的可能会出问题。

[此贴子已经被作者于2007-2-22 12:08:06编辑过]


VB QQ群:47715789
2007-02-22 11:57
121038
Rank: 1
等 级:新手上路
威 望:2
帖 子:414
专家分:0
注 册:2005-8-3
得分:0 


你们都很喜欢复杂的代码吗??


2007-02-22 11:58
Joforn
Rank: 6Rank: 6
等 级:贵宾
威 望:23
帖 子:1242
专家分:122
注 册:2007-1-2
得分:0 
不是,因为楼主的方法如果打开了一些进程无法关闭的话就可能关到一半关不了机了。比如说编辑了一个OFFICE文档,当关机时就会提示是否存盘,这时电脑就卡住了,无法关闭。

VB QQ群:47715789
2007-02-22 12:10



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-121301-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.122676 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved