VB实现定时打开软件
解决时间 2021-04-11 15:19
- 提问者网友:浩歌待明月
- 2021-04-11 07:00
我要的是 每隔幾分鐘 或者 幾秒鐘 打開的 那種 可以一直重複
不是 好多時間 打開的
最佳答案
- 五星知识达人网友:罪歌
- 2021-04-11 07:49
这个简单,以每二秒打开一次记事本程序为例:
1. 启动VB 6,创建一新工程,选择“标准EXE”,单击“确定”;
2.在窗口建立一个Timer控件,如图:
3.在点视图,选择代码窗口,如图:
4、然后在窗口中粘贴以下几条代码:
Private Sub Form_Load()
Timer1.Interval = 2000
End Sub
Private Sub Timer1_Timer()
Shell "c:\windows\system32\notepad.exe"
End Sub
然后按F5运行一下试试就行了。
这是第4步的图:
全部回答
- 1楼网友:骨子里都是戏
- 2021-04-11 10:14
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行即可
- 2楼网友:有你哪都是故乡
- 2021-04-11 08:38
不知道你懂不懂 VB的,
建立一个TIMER控件;设置TIMER属性
写入你要打开的程序
SHELL函数打开路径就可以了!
非常简单
我要举报
大家都在看
推荐资讯