用VB打开电脑的应用程序是调用API函数来修改或增加注册表中的值,这样在系统启动时就会自动执行。
Option ExplicitConst REG_SZ As Long = 1
Const HKEY_LOCAL_MACHINE = &H80000002
'创建注册表项
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias _
"RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, _
phkResult As Long) As Long
'设置注册表项中的值
Private Declare 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
'打开注册表中的项
Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias _
"RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, _
ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
'获取子项
Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias _
"RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, _
ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
Const REG_DWORD = 4
Private Sub Form_Load()
'为text1设置默认路径程序
Text1.Text = "C:\Program Files\****\****.exe"'请修改自动启动程序的路径。
End Sub
Private Sub Command1_Click()
'设置开机启动
Dim hKey As Long
Dim myexe As String
Dim myint As Integer
myint = Len(Text1.Text) - InStrRev(Text1.Text, "\")'取得应用程序文件名字符串的字节数,含扩展名
myexe = Right(Text1.Text, myint)'取得应用程序文件名字符串,含扩展名
If Text1.Text <> "" Then
RegCreateKey HKEY_LOCAL_MACHINE, "Software\Microsoft\Windows\CurrentVersion\Run", hKey
'"HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run"是注册表中的路径
RegSetValueEx hKey, myexe, 0, REG_SZ, ByVal Text1.Text, 255
'myexe是上面注册表路径中添加的项,Text1.Text是该项的数据。
End If
MsgBox "设置完成,请您重新启动计算机!" & vbCrLf & vbCrLf & "你设置的程序将开机自动执行!", 64, "提示信息"
End Sub
Private Sub Command2_Click()
'查找自动启动的程序
DHK1.DialogTitle = "选择程序"'注意打开对话框的名称是DHK1
DHK1.Filter = "应用程序(*.exe)|*.exe"
DHK1.ShowOpen
If Len(DHK1.FileName) Then
Text1.Text = DHK1.FileName
End If
End Sub
5
Private Sub Command3_Click()
End
End Sub
PrivateSub
Form_Click()
Shell
"c:\windows\system32\calc.exe",
vbNormalFocus
'打开计算器程序逻(写上完整的路径)
Shell
"explorer
http://www.baidu.com",
vbNormalFocus
'打开百度首页
Shell
"cmd
/c
dir
>
c:\a.txt",
vbNormalFocus
'执行一个DOS命令
并把显示结果存到
c盘a.txt文件中
End
Sub
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)