VB实现定时打开软件

VB实现定时打开软件,第1张

1. 启动VB 6,创建一新工程,选择“标准EXE”,单击“确定”;

2. 在窗体中放一文本控件,命名text1,用以输入定时关机的时间

3. 在窗体中放两个标签控件,命名分别为伍拦label1和label2. label1的caption属性为“请输入定时关机的时间”,Label2的 Caption属性为“00 00 00”,用以显示当前系统的时间;

4. 在窗体中放两个定时器控件,命名分别为timer1和timer2, interval属性都为1000;

5. 在窗体中放一命令按钮控件,命名command1。

■程序开发

首先显式声明下面三个API函数:

Private Declare Function ExitWindowsEx& Lib ″user32″ (ByVal uFlags&, ByVal dwReserved&) As long

′用于关闭或重新启动计算机,由uFlags的值决定。当uFlags=1时关闭计算机,当uFlags=2时重新启动计算机。

Private Declare Function FindWindow Lib ″user32″ Alias ″FindWindowA″ (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

′寻找指定窗口,如存在,则置为当前活动窗口。

Private Declare Function PostMessage Lib ″user32″ Alias ″PostMessageA″ (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Const WM—CLOSE = &H10

Const EWX—SHUTDOWN = 1

Dim s(10) As Long

Option Explicit

′命令按钮的click事件

Private Sub Command1—Click()

′声名日期型乱橘侍变量

Dim Time1 As Date

Dim Time2 As Date

′首先关闭定时器timer1

Timer1.Enabled = False

Timer2.Enabled = False

′取系统当前时间

Time2 = Time()

′判断用户输入时间的合法性

If Text1.Text = ″ ″ Then

MsgBox ″请输入定时关机时间!″, vbExclamation + vbOKCancel, ″警告信息″

Text1.SetFocus

Exit Sub

End If

If IsDate(Text1) = False Then

MsgBox ″非法时间!请重新输入!″, vbExclamation + vbOKCancel, ″警告信息″

Text1.SetFocus

Exit Sub

Else:

Time1 = CDate(Text1.Text)

′把输入的时间字符串转换为日期型

′ 把设定的关机时间转换为秒数值

s(0) = Val(Left(Trim(Text1.Text), 2)) 60 60

s(1) = Val(Mid(Trim(Text1.Text), 4, 2)) 60

s(2) = Val(Right(Trim(Text1.Text), 2))

s(3) = s(0) + s(1) + s(2)

End If

If Time1 〈= Time2 Then

MsgBox ″定时关机时间必须大于当前时间!″, vbExclamation + vbOKCancel, ″警告信息″

Text1.SetFocus

Exit Sub

Else:

Timer1.Enabled = True′打开定时器

Timer2.Enabled = True

Label1.Visible = False

Text1.Visible = False

 哗吵 Command1.Visible = False

Label2.Visible = True

End If

End Sub′自定义函数

Function exit_win() ′关机的函数

Dim a As Variant

a = ExitWindowsEx(EWX—SHUTDOWN, 0&) ′shut down the computer

End Function

Private Sub Form—Load()

Label2.Visible = False

End Sub′定时器1的事件

Private Sub Timer1—Timer() ′定时器触发事件

Dim cur—time As String

Dim winhwnd As Long

Dim retval As Long ′取系统当前时间

cur_time = CStr(Time())

′把系统当前时间值转换为秒数值

s(4) = Val(Left(cur—time, 2)) 60 60

s(5) = Val(Mid(cur—time, 4, 2)) 60

s(6) = Val(Right(cur—time, 2))

s(7) = s(4) + s(5) + s(6)

If s(7) = s(3) Then

′关机前关闭执行程序

winhwnd = FindWindow(vbNullString, ″定时关机″)

If winhwnd 〈〉 0 Then

retval = PostMessage(winhwnd, WM—CLOSE, 0&, 0&)

If retval = 0 Then

MsgBox ″发送消息错误.″

End If

End If

Beep ′报警

Beep

Call exit_win

End If

End Sub′定时器2的事件

Private Sub Timer2—Timer()

′把当前的系统时间显示在 label2上

Timer2.Enabled = False

If (Format$(Time, ″hh″) & ″:″ & Format$(Time, ″nn″) & ″:″ & Format$(Time, ″ss″)) 〈〉 ″00:00:00″ Then

Time = DateAdd(″s″, 1, Time)

Label2.Visible = False

Label2.Caption = Format$(Time, ″hh″) & ″:″ & Format$(Time, ″nn″) & ″:″ & Format$(Time, ″ss″)

Label2.Visible = True

Timer2.Enabled = True

End If

End Sub

程序在VB6.0下调试通过,如在较低版本下运行,可用notepad.exe编辑VBP工程文件,删除retained=0行即可

给你提供我用到的代厅迅码:\x0d\x0a'初始化全局变量\x0d\x0aOption Explicit\x0d\x0aPublic pathini As String\x0d\x0aPublic mark As Integer '控制是否族芹自启动的标志变量(1自启动,0不自启动)\x0d\x0a'添加删除兆伏毕自启动项目的API函数声明\x0d\x0aPublic Const HKEY_CLASSES_ROOT = &H80000000\x0d\x0aPublic Const HKEY_CURRENT_USER = &H80000001\x0d\x0aPublic Const HKEY_LOCAL_MACHINE = &H80000002\x0d\x0aPublic Const HKEY_USERS = &H80000003\x0d\x0aPublic Const HKEY_PERFORMANCE_DATA = &H80000004\x0d\x0aPublic Const HKEY_CURRENT_CONFIG = &H80000005\x0d\x0aPublic Const HKEY_DYN_DATA = &H80000006\x0d\x0aPublic Const REG_NONE = 0\x0d\x0aPublic Const REG_SZ = 1\x0d\x0aPublic Const REG_EXPAND_SZ = 2\x0d\x0aPublic Const REG_BINARY = 3\x0d\x0aPublic Const REG_DWORD = 4\x0d\x0aPublic Const REG_DWORD_BIG_ENDIAN = 5\x0d\x0aPublic Const REG_MULTI_SZ = 7\x0d\x0aDeclare Function RegOpenKey Lib 'advapi32.dll' Alias 'RegOpenKeyA' (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long\x0d\x0aDeclare Function RegCreateKey Lib 'advapi32.dll' Alias 'RegCreateKeyA' (ByVal hKey As Long,ByVal lpSubKey As String, phkResult As Long) As Long\x0d\x0aDeclare Function RegCloseKey Lib 'advapi32.dll' (ByVal hKey As Long) As Long\x0d\x0aDeclare Function RegSetValue Lib 'advapi32.dll' Alias 'RegSetValueA' (ByVal hKey As Long, ByVal lpSubKey As String, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long\x0d\x0aDeclare Function RegSetValueEx Lib 'advapi32.dll' Alias 'RegSetValueExA' (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long\x0d\x0aDeclare Function RegDeleteValue Lib 'advapi32.dll' Alias 'RegDeleteValueA' (ByVal hKey As Long, ByVal lpValueName As String) As Long\x0d\x0a'在注册表中添加删除自启动项目的模块\x0d\x0aSub Main()\x0d\x0a' 定义hKey 为句柄变量、SubKey为注册表RUN项目变量、Exe为程序名变量\x0d\x0aDim hKey As Long, SubKey As String, Exe As String\x0d\x0a'将程序设定成随 Windows 启动而启动\x0d\x0a'取程序所在路径,判断是否为根目录并分别加上程序全名并赋值给变量Exe\x0d\x0aIf Right(App.Path, 1) = '\' Then\x0d\x0aExe = App.Path App.EXEName '.exe' ' 根目录情况\x0d\x0aElse: Exe = App.Path '\' App.EXEName '.exe' ' 非根目录情况\x0d\x0aEnd If\x0d\x0aSubKey = 'Software\Microsoft\Windows\CurrentVersion\Run' ' 设置项目\x0d\x0aRegCreateKey HKEY_LOCAL_MACHINE, SubKey, hKey ' 取得句柄\x0d\x0aRegSetValueEx hKey, '时间助手', 0, REG_SZ, ByVal Exe, LenB(StrConv(Exe, vbFromUnicode)) 1 ' 写入键值\x0d\x0aRegCloseKey hKey ' 关闭句柄\x0d\x0aEnd Sub\x0d\x0a自己看吧,很简单的,修改一下就可以用了.


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12501325.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存