怎么才能让程序开机自动启动时:
1 不在任务栏中显示出来?
2 关机是自动执行Form_unload过程?
注册表中提供了三个这样的键:
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunServices
这三个键字的区别是:
Run:此键字下的应用程序在系统启动的时候会自动运行;
RunOnce:此键字下的应用程序在系统下一次启动的时候会自动运行,以后不再运行;
RunServices:功能和“Run”一样,只是应用程序被启动的时候不同而已。
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function OSRegSetValueEx Lib "advapi32" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpszValueName As String, ByVal dwReserved As Long, ByVal fdwType As Long, lpbData As Any, ByVal cbData As Long) As Long
Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegisterServiceProcess Lib "kernel32" (ByVal ProcessID As Long, ByVal ServiceFlags As Long) As Long
Private Declare Function GetCurrentThread Lib "kernel32" () As Long
Private Const HKEY_LOCAL_MACHINE = &H80000002
Private Const REG_SZ = 1
Private Sub Form_Load()
Dim strAppPath As String
Dim lResult As Long
If Right(App.Path, 1) = "\" Then
strAppPath = App.Path & App.EXEName & ".exe"
Else
strAppPath = App.Path & "\" & App.EXEName & ".exe"
End If
添加到注册表,启动Windows时打开程序
RegOpenKey HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion\RunServices", lResult
OSRegSetValueEx lResult, App.EXEName, 0&, REG_SZ, ByVal strAppPath, LenB(strAppPath)
RegCloseKey lResult
不在任务栏内显示
RegisterServiceProcess GetCurrentProcessId, 1
End Sub
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
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 Note that if you declare the lpData parameter as String, you must pass it By Value.
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Const HKEY_CURRENT_USER = &H80000001
Private Const REG_SZ = 1 Unicode nul terminated string
Private Sub Command1_Click()
End Sub
Private Sub Form_Initialize()
Dim hKey As Long
Dim strRunCmd As String
Dim retval As Long
If UnloadMode = vbAppWindows Then
retval = ShellExecute(GetDesktopWindow(), "open", "C:\WINDOWS\Desktop\音频解霸.lnk", 0, 0, 1)
strRunCmd = "E:\wvbEXE\自动行程序.exe"
注册表Software\Microsoft\Windows\CurrentVersion\RunOnce保存开机启动程序。
Call RegCreateKey(HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Runonce", hKey)
Call RegSetValueEx(hKey, "zmhh", 0&, REG_SZ, ByVal strRunCmd, Len(strRunCmd) + 1)
Call RegCloseKey(hKey)
MsgBox "下次开机程序会自动启动"
End If
End
End Sub