<b>VB的开机自动运路程序的代码</b>[VB编程]
本文“<b>VB的开机自动运路程序的代码</b>[VB编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
有两种办法,1是注册表方法
模块代码
Option Explicit
Public 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
Public Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Public Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
Public Const REG_SZ = 1
Public Const HKEY_LOCAL_MACHINE = &H80000002
'*************************************************************************
'**函 数 名: SetAutoRun
'**输 入: ByVal Autorun(Boolean) -
'**输 出: 无
'**功效描写: 随WINDOWS自动启动/撤消启动模块
'**全局变量:
'**调用办法: Call SetAutoRun(True/False)
'**作 者: mythhack
'**日 期: 2009-12-12 01:00:25
'**修 改 人:
'**日 期:
'**版 本: V1.0.0
'*************************************************************************
Public Sub SetAutoRun(ByVal Autorun As Boolean)
Dim KeyId As Long
Dim MyexePath As String
Dim regkey As String
MyexePath = App.Path & "\" & App.EXEName & ".exe" '获得程序位置
regkey = "Software\Microsoft\Windows\CurrentVersion\Run" '键值位置变量
Call RegCreateKey(HKEY_LOCAL_MACHINE, regkey, KeyId) '成立
If Autorun Then
RegSetValueEx KeyId, "MySoftware", 0&, REG_SZ, ByVal MyexePath, LenB(MyexePath)
Else
RegDeleteValue KeyId, "MySoftware"
End If
RegCloseKey KeyId
End Sub
调用办法
SetAutoRun(ByVal Autorun As Boolean)
2是操纵Vb5stkit.dll里面的函数
窗体部份代码,加入6个按钮
Option Explicit
Private Sub CmdAddStartup_Click() '在开始菜单的启动程序组下成立记事本的快速方法
Call OSfCreateShellLink("\启动", "记事本", GetWindowsPath & "\Notepad.exe", "")
End Sub
Private Sub CmdAddDeskTop_Click() '在桌面成立记事本的快速方法
Call OSfCreateShellLink("..\..\桌面", "记事本", GetWindowsPath & "\Notepad.exe", "")
End Sub
Private Sub CmdAddProgram_Click() '在程序菜单的Notepad程序组下成立记事本的快速方法
Call OSfCreateShellGroup("Notepad") '先成立程序组
Call OSfCreateShellLink("Notepad", "记事本", GetWindowsPath & "\Notepad.exe", "")
End Sub
Private Sub CmdAddStartMenu_Click()
Dim i As Long
For i = 1 To 5 '在开始菜单成立记事本的快速方法,必须用循环才能成立?
Call OSfCreateShellLink("..\..\「开始」菜单", "记事本", GetWindowsPath & "\Notepad.exe", "")
Next
End Sub
Private Sub CmdQuickLaunch_Click() '在快速工具栏下成立记事本的快速方法
Call OSfCreateShellLink("..\..\Application Data\Microsoft\Internet Explorer\Quick Launch", "记事本", GetWindowsPath & "\Notepad.exe", "")
End Sub
Private Sub CmdDelAllLink_Click()
Call OSfRemoveShellLink("..\..\「开始」菜单", "记事本") '删除开始菜单上的快速方法
Call OSfRemoveShellLink("..\..\桌面", "记事本") '删除桌面上的快速方法
'Call OSfRemoveShellLink("Notepad", "记事本") '删除Notepad程序组下的快速方法,这样不能删除程序组
Call RemoveShellGroup '删除Notepad程序组下的快速方法
Call OSfRemoveShellLink("\启动", "记事本") '删除启动菜单下的快速方法
Call OSfRemoveShellLink("..\..\Application Data\Microsoft\Internet Explorer\Quick Launch", "记事本") '删除快速工具栏下的快速方法
End Sub
Private Sub RemoveShellGroup()
On Error GoTo ToExit '翻开错误陷阱
'------------------------------------------------
'RmDir删除一个存在的目录或文件夹.语法RmDir Path
'必要的 path 参数是一个字符串表达式,用来指定要删除的目录或文件夹.path 可以包含驱动器.假如没有指定驱动器,则 RmDir 会在当前驱动器上删除目录或文件夹.
'阐明假如想要利用 RmDir 来删除一个含有文件的目录或文件夹,则会发生错误.在试图删除目录或文件夹之前,先利用 Kill 语句来删除全部文件.
Kill (GetProgarmPath(Me.hWnd) & "\Notepad\记事本.lnk")
RmDir (GetProgarmPath(Me.hWnd) & "\Notepad")
'------------------------------------------------
Exit Sub
'----------------
ToExit:
Resume Next
End Sub
'模块代码
Option Explicit
'-----------------------------------------------------
' 成立和删除快速方法
'-----------------------------------------------------
' 洪恩在线 求知无限
'-----------------------------------------------------
'------名称-------------------作用--------------------
' CmdAddStartup "成立启动程序组快速方法"
' CmdAddDeskTop "成立桌面快速方法"
' CmdAddStartMenu "成立开始菜单快速方法"
' CmdAddProgram "成立程序组下的快速方法"
' CmdQuickLaunch "成立快速工具栏的快速方法"
' CmdDelAllLink "删除全部快速方法"
'-----------------------------------------------------
'要在VB中成立Windows的快速方法,需求用到VB的一个动态链接库
'Vb5stkit.dll.在该动态链接库中供应了三个函数
'OSfCreateShellGroup、OSfCreateShellLink、OSfRemoveShellLink
'辨别用于成立快速方法程序组、成立快速方法和删除快速方法.
'-----------------------------------------------------
Declare Function OSfCreateShellGroup Lib "Vb5stkit.dll" _
Alias "fCreateShellFolder" (ByVal lpstrDirName As String) As Long
'lpstrDirName指定了程序组的名称
'-----------------------------------------------------
Declare Function OSfCreateShellLink Lib "Vb5stkit.dll" _
Alias "fCreateShellLink" (ByVal lpstrFolderName As String, _
ByVal lpstrLinkName As String, ByVal lpstrLinkPath As String, ByVal lpstrLinkArguments As String) As Long
'lpstrfoldername指定保存快速方法的文件夹
'lpstrlinkname指定快速方法的文件名
'lpstrLinkpathe指定快速方法所指向的利用程序或文件
'lpstrLinkArguments是程序运行所需的参数
'-----------------------------------------------------
Declare Function OSfRemoveShellLink Lib "Vb5stkit.dll" Alias _
"fRemoveShellLink" (ByVal lpstrFolderName As String, ByVal lpstrLinkName As String) As Long
'获得Windows目录
Private Declare Function GetWindowsDirectory Lib "kernel32" Alias _
"GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
'得到文件夹途径
Private Declare Function SHGetSpecialFolderPath Lib "shell32.dll" Alias "SHGetSpecialFolderPathA" (ByVal hwndOwner As Long, ByVal lpszPath As String, ByVal nFolder As Long, ByVal fCreate As Long) As Long
Private Const Max_Path = 260 '缓冲区大小
Private Const CSIDL_PROGRAMS = &H2 '程序组常量
'*************************************************************************
'**函 数 名: GetWindowsPath
'**输 入: 无
'**输 出: (String) -
'**功效描写: 得到Windows途径
'**全局变量:
'**调用模块:
'**作 者: mythhack
'**日 期: 2009-12-12 01:00:25
'**修 改 人:
'**日 期:
'**版 本: V1.0.0
'*************************************************************************
Public Function GetWindowsPath() As String
Dim ChrLen As Long, WinDir As String
WinDir = Space$(Max_Path)
ChrLen = GetWindowsDirectory(WinDir, Max_Path)
WinDir = Left$(WinDir, ChrLen)
GetWindowsPath = WinDir
End Function
'*************************************************************************
'**函 数 名: GetProgarmPath
'**输 入: frmHwnd(Long) -
'**输 出: (String) -
'**功效描写: 获得开始菜单程序组的途径
'**全局变量:
'**调用模块:
'**作 者: mythhack
'**日 期: 2009-12-12 01:00:00
'**修 改 人:
'**日 期:
'**版 本: V1.0.0
'*************************************************************************
Public Function GetProgarmPath(frmHwnd As Long) As String
Dim CSILD_NUM As Long, strBouff As String
strBouff = String$(Max_Path, 0)
SHGetSpecialFolderPath frmHwnd, strBouff, CSIDL_PROGRAMS, 0
GetProgarmPath = Left$(strBouff, InStr(1, strBouff, Chr$(0)) - 1)
End Function
办法3
先引用系统里面都有的WSHom.Ocx
Option Explicit
'*************************************************************************
'**函 数 名: SetAutoRun
'**输 入: ByVal Autorun(Boolean) -
'**输 出: 无
'**功效描写: 随WINDOWS自动启动/撤消启动模块
'**全局变量:
'**调用办法: Call SetAutoRun(True/False)
'**作 者: mythhack
'**日 期: 2009-12-12 01:00:25
'**修 改 人:
'**日 期:
'**版 本: V1.0.0
'*************************************************************************
Public Sub SetAutoRun(ByVal Autorun As Boolean)
'WshShell 对象
'ProgId Wscript.Shell
'文件名 WSHom.Ocx
Dim WshShell As WshShell
Set WshShell = CreateObject("Wscript.Shell")
If Autorun Then
WshShell.RegWrite "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\" & App.EXEName, App.Path & "\" & App.EXEName & ".exe"
Else
WshShell.RegDelete "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\" & App.EXEName
End If
Set WshShell = Nothing
End Sub
以上是“<b>VB的开机自动运路程序的代码</b>[VB编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |