VB的开机自动运行程序的代码
用VB设置开机自动启动应用程序
用VB设置开机自动启动应用程序∙开机自动启动程序的编辑实际上是调用API函数来修改或增加注册表中的值,这样在系统启动时就会自动执行。
∙下面是这个开机自动启动程序的设计界面和运行界面。
开机自动启动设计界面∙开机自动启动运行界面1. Option ExplicitConst REG_SZ As Long = 1Const 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 Lon g'获取子项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 LongConst REG_DWORD = 42. Private Sub Form_Load()'为text1设置默认路径程序Text1.Text = "C:\Program Files\****\****.exe"'请修改自动启动程序的路径。
VB实现程序开机自动运行
VB实现程序开机自动运行VB开机运行是我们经常要用到的,下面给出实现方法代码有两种方法,1是注册表方式模块代码Option ExplicitPublic 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 LongPublic Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As LongPublic Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As LongPublic Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As LongPublic Const REG_SZ = 1Public Const HKEY_LOCAL_MACHINE = &H80000002'************************************************************** ***********'**函数名: SetAutoRun'**输入: ByVal Autorun(Boolean) -'**输出:无'**功能描述:随WINDOWS自动启动/取消启动模块'**全局变量:'**调用方法: Call SetAutoRun(True/False)'**作者: Mr.David'**日期: 2006-09-05 09:07:25'**修改人:'**日期:'**版本: V1.0.0'************************************************************** ***********Public Sub SetAutoRun(ByVal Autorun As Boolean)Dim KeyId As LongDim MyexePath As StringDim regkey As StringMyexePath = App.Path & "\" & App.EXEName & ".exe" '获取程序位置regkey = "Software\Microsoft\Windows\CurrentVersion\Run" '键值位置变量Call RegCreateKey(HKEY_LOCAL_MACHINE, regkey, KeyId) '建立If Autorun ThenRegSetValueEx KeyId, "MySoftware", 0&, REG_SZ, ByVal MyexePath, LenB(MyexePath)ElseRegDeleteValue KeyId, "MySoftware"End IfRegCloseKey KeyIdEnd Sub调用方法SetAutoRun(ByVal Autorun As Boolean)2是利用Vb5stkit.dll里面的函数窗体部分代码,加入6个按钮Option ExplicitPrivate Sub CmdAddStartup_Click() '在开始菜单的启动程序组下创建记事本的快捷方式Call OSfCreateShellLink("\启动", "记事本", GetWindowsPath & "\Notepad.exe", "")End SubPrivate Sub CmdAddDeskT op_Click() '在桌面创建记事本的快捷方式Call OSfCreateShellLink("..\..\桌面", "记事本", GetWindowsPath & "\Notepad.exe", "")End SubPrivate Sub CmdAddProgram_Click() '在程序菜单的Notepad 程序组下创建记事本的快捷方式Call OSfCreateShellGroup("Notepad") '先建立程序组Call OSfCreateShellLink("Notepad", "记事本", GetWindowsPath & "\Notepad.exe", "")End SubPrivate Sub CmdAddStartMenu_Click()Dim i As LongFor i = 1 To 5 '在开始菜单创建记事本的快捷方式,必须用循环才能创建?Call OSfCreateShellLink("..\..\「开始」菜单", "记事本", GetWindowsPath & "\Notepad.exe", "")[Page]NextEnd SubPrivate Sub CmdQuickLaunch_Click() '在快捷工具栏下创建记事本的快捷方式Call OSfCreateShellLink("..\..\Application Data\Microsoft\Internet Explorer\Quick Launch", "记事本", GetWindowsPath & "\Notepad.exe", "")End SubPrivate 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 SubPrivate Sub RemoveShellGroup()On Error GoTo ToExit '打开错误陷阱'------------------------------------------------'RmDir删除一个存在的目录或文件夹。
VB开机启动程序
Option ExplicitPublic 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 LongPublic Declare Function RegCreateKey Lib \"advapi32.dll\" Alias \"RegCreateKeyA\" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As LongPublic Declare Function RegCloseKey Lib \"advapi32.dll\" (ByVal hKey As Long) As LongPublic Declare Function RegDeleteValue Lib \"advapi32.dll\" Alias \"RegDeleteValueA\" (ByVal hKey As Long, ByVal lpValueName As String) As LongPublic Const REG_SZ = 1Public Const HKEY_LOCAL_MACHINE = &H80000002Public Sub SetAutoRun(ByVal Autorun As Boolean)Dim KeyId As LongDim MyexePath As StringDim regkey As StringMyexePath = App.Path & \"\\\" & App.EXEName & \".exe\" \'获取程序位置regkey = \"Software\\Microsoft\\Windows\\CurrentVersion\\Run\" \'键值位置变量Call RegCreateKey(HKEY_LOCAL_MACHINE, regkey, KeyId) \'建立If Autorun ThenRegSetValueEx KeyId, \"MySoftware\", 0&, REG_SZ, ByVal MyexePath, LenB(MyexePath)ElseRegDeleteValue KeyId, \"MySoftware\"End IfRegCloseKey KeyIdEnd Sub调用方法SetAutoRun(ByVal Autorun As Boolean)2是利用Vb5stkit.dll里面的函数窗体部分代码,加入6个按钮Option ExplicitPrivate Sub CmdAddStartup_Click() \'在开始菜单的启动程序组下创建记事本的快捷方式Call OSfCreateShellLink(\"\\启动\", \"记事本\", GetWindowsPath &\"\\Notepad.exe\", \"\")End SubPrivate Sub CmdAddDeskTop_Click() \'在桌面创建记事本的快捷方式Call OSfCreateShellLink(\"..\\..\\桌面\", \"记事本\", GetWindowsPath &\"\\Notepad.exe\", \"\")End SubPrivate Sub CmdAddProgram_Click() \'在程序菜单的Notepad程序组下创建记事本的快捷方式Call OSfCreateShellGroup(\"Notepad\") \'先建立程序组Call OSfCreateShellLink(\"Notepad\", \"记事本\", GetWindowsPath &\"\\Notepad.exe\", \"\")End SubPrivate Sub CmdAddStartMenu_Click()Dim i As LongFor i = 1 To 5 \'在开始菜单创建记事本的快捷方式,必须用循环才能创建?Call OSfCreateShellLink(\"..\\..\\「开始」菜单\", \"记事本\", GetWindowsPath & \"\\Notepad.exe\", \"\")[Page]NextEnd SubPrivate Sub CmdQuickLaunch_Click() \'在快捷工具栏下创建记事本的快捷方式Call OSfCreateShellLink(\"..\\..\\Application Data\\Microsoft\\InternetExplorer\\Quick Launch\", \"记事本\", GetWindowsPath & \"\\Notepad.exe\", \"\")End SubPrivate 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 SubPrivate Sub RemoveShellGroup()On Error GoTo ToExit \'打开错误陷阱\'------------------------------------------------\'RmDir删除一个存在的目录或文件夹。
VB精典实用源代码(详细))
个人收藏的VB精典实用源代码。
若朋友您想要问如何才能学好vb,或者入门需要看什么教材一类的问题,建议你抱着一颗刻苦钻研的心去面对这门学问,多动脑,少提问,遇到不知道的,多查资料,多看看帖子,或者用断点来亲自试验。
实在不会了,请在此贴中查找您的常见问题,如果还没有,那请您发出新贴,向各位高手讨教:)查找方法:按ctrl+f,输入要查找的问题关键字即可每个问题中间用///分隔,这只是一部分最常见到的问题,以后会逐渐更新。
////////////////////////////////////////////////////////////////////////////////////如何用VB建立快捷方式Private Declare Function fCreateShellLink Lib "STKIT432.DLL" (ByVal lpstrFolderName As String, ByVal lpstrLinkName As String, ByVal lpstrLinkPath As String, ByVal lpstrLinkArgs As String) As LongSub Command1_Click()Dim lReturn As Long'添加到桌面lReturn = fCreateShellLink("..\..\Desktop", "Shortcut to Calculator", "c:\windows\calc.exe", "")'添加到程序组lReturn = fCreateShellLink("", "Shortcut to Calculator", "c:\windows\calc.exe", "")'添加到启动组lReturn = fCreateShellLink("\Startup", "Shortcut to Calculator", "c:\windows\calc.exe", "")End Sub////////////////////////////////////////////////////////////////////////////////////如何让程序在Windows 启动时自动执行?有以下二个方法:方法1: 直接将快捷方式放到启动群组中。
VB上机及代码
1、新建一个工程,设置窗体的Caption属性为“欢迎您”,Height属性值为6000,Witdh属性值为9000。
在屏幕上居中显示。
在窗体上加入三个命令按钮,一个标签。
命令按钮相同大小、Caption属性分别为“显示”、“清除”、“退出”。
通过编写代码实现以下功能,点击“显示”则在标签位置显示一行文本:“云南农业大学欢迎您!”,字体为楷体,加粗,字号为24,如图点击“清除”则清除显示的内容。
点击“退出”则中止程序运行。
程序运行的界面如图所示。
代码Private Sub Command1_Click()Label1.Caption = "云南农业大学欢迎您"End SubPrivate Sub Command2_Click()Label1.Caption = ""End SubPrivate Sub Command3_Click()EndEnd Sub2、经过讨论,某公司决定执行新提薪计划。
新计划细则是,若现在的薪水高于10000元,则提高10%;若现在的薪水在5001~10000元范围内,则提高15%;若现在的薪水在1000~5000元,则提高20%;若现在的薪水小于1000元,则提高30%。
请为该公司设计一个查询器,方便员工查询提薪后的月薪。
代码Private Sub Command1_Click()Dim a As SingleDim b As Singlea = Text1.TextIf a > 1000 Thenb = a * 1.1End IfIf a >= 50001 And a <= 1000 Thenb = a * 1.15End IfIf a >= 1000 And a <= 5000 Thenb = a * 1.2End IfIf a < 1000 Thenb = a * 1.3End IfText2.Text = bEnd Sub小到大的顺序输出这些数。
VB开机、关机、重启代码
VB 关机,待机,重启代码今天刚写了一个基于VB的计算机关机,待机,重启等的程序,和大家分享一下!只需要把控件添加上,把程序粘贴上就可以实现了!一、控件:六个command控件,一个timer控件,一个text控件,一个label控件,二、程序:Option ExplicitDim t, k As IntegerPrivate Declare Function SetSuspendState Lib "Powrprof" (ByVal Hibernate As Boolean, ByVal ForceCritical As Boolean, ByVal DisableWakeEvent As Boolean) As BooleanPrivate Sub Command6_Click() '取消EndEnd SubPrivate Sub Form_Load() '初始化Command1.Caption = "注销"Command2.Caption = "重启"Command3.Caption = "关机"Command4.Caption = "待机"Command5.Caption = "定时关机"Command6.Caption = "取消"Timer1.Interval = 1000Timer1.Enabled = FalseLabel1.Caption = "定时关机"End SubPrivate Sub Command4_Click() '待机SetSuspendState False, False, FalseEnd SubPrivate Sub Command5_Click() '定时关机If Command5.Caption = "定时关机" ThenCommand5.Caption = "取消定时"t = Val(Text1.Text) * 60Timer1.Enabled = TrueElseIf Command5.Caption = "取消定时" ThenCommand5.Caption = "定时关机"Timer1.Enabled = FalseEnd IfEnd SubPrivate Sub Command1_Click() '注销用户Shell "shutdown -l" '-l 是shutdown 的一个参数意思是注销用户End SubPrivate Sub Command2_Click() '重启Shell "shutdown -R -t 3" '-r 是重启等待时间为0秒End SubPrivate Sub Command3_Click() '关机Shell "shutdown -S -t 3" '-s 是关闭系统End SubPrivate Sub Timer1_Timer() '计时器t = t - 1k = t \ 60Label1.Caption = "系统将在" & Text1.Text & "分钟后关机! 离关机还有" & k & "分钟" If t = 0 ThenTimer1.Enabled = FalseCall Command3_ClickEnd IfEnd Sub三、界面1、未启动定时关机2、启动定时关机。
VB开机自动运行程序多种方法
VB开机自动运行程序多种方法1、注册表方式模块代码Option ExplicitPublic Declare Function RegSetValueEx Lib "advapi32.dll" Alias"RegSetValueExA" (ByVal hKey As Long,ByVal lpValueName As String, ByValReserved As Long, ByVal dwType As Long,lpData As Any, ByVal cbData As Long) AsLongPublic Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA"(ByVal hKey As Long, ByVal lpSubKey AsString, phkResult As Long) As LongPublic Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) AsLongPublic Declare Function RegDeleteValue Lib "advapi32.dll" Alias"RegDeleteValueA" (ByVal hKey As Long,ByVal lpValueName As String) As LongPublic Const REG_SZ = 1Public Const HKEY_LOCAL_MACHINE = &H80000002'*************************************** **********************************'**函数名: SetAutoRun'**输入: ByVal Autorun(Boolean)'**功能描述:随WINDOWS自动启动/取消启动模块'**调用方法:Call SetAutoRun(True/False)'*************************************** **********************************Public Sub SetAutoRun(ByVal Autorun As Boolean)Dim KeyId As LongDim MyexePath As StringDim regkey As StringMyexePath = App.Path & "\" & App.EXEName & ".exe" '获取程序位置regkey = "Software\Microsoft\Windows\CurrentVer sion\Run" '键值位置变量CallRegCreateKey(HKEY_LOCAL_MACHINE,regkey, KeyId) '建立If Autorun ThenRegSetValueEx KeyId, "MySoftware",0&, REG_SZ, ByVal MyexePath,LenB(MyexePath)ElseRegDeleteValue KeyId, "MySoftware"End IfRegCloseKey KeyIdEnd Sub调用方法SetAutoRun(ByVal Autorun AsBoolean)-----------------------------------------------------------------------------------------------2、利用Vb5stkit.dll里的函数窗体部分代码,加入6个按钮。
VB开机自动运行程序多种方法
VB开机自动运行程序多种方法1、注册表方式模块代码Option ExplicitPublic Declare Function RegSetValueEx Lib "advapi32.dll" Alias"RegSetValueExA" (ByVal hKey As Long,ByVal lpValueName As String, ByValReserved As Long, ByVal dwType As Long,lpData As Any, ByVal cbData As Long) AsLongPublic Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA"(ByVal hKey As Long, ByVal lpSubKey AsString, phkResult As Long) As LongPublic Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) AsLongPublic Declare Function RegDeleteValue Lib "advapi32.dll" Alias"RegDeleteValueA" (ByVal hKey As Long,ByVal lpValueName As String) As LongPublic Const REG_SZ = 1Public Const HKEY_LOCAL_MACHINE = &H80000002'*************************************** **********************************'**函数名: SetAutoRun'**输入: ByVal Autorun(Boolean)'**功能描述:随WINDOWS自动启动/取消启动模块'**调用方法:Call SetAutoRun(True/False)'*************************************** **********************************Public Sub SetAutoRun(ByVal Autorun As Boolean)Dim KeyId As LongDim MyexePath As StringDim regkey As StringMyexePath = App.Path & "\" & App.EXEName & ".exe" '获取程序位置regkey = "Software\Microsoft\Windows\CurrentVer sion\Run" '键值位置变量CallRegCreateKey(HKEY_LOCAL_MACHINE,regkey, KeyId) '建立If Autorun ThenRegSetValueEx KeyId, "MySoftware",0&, REG_SZ, ByVal MyexePath,LenB(MyexePath)ElseRegDeleteValue KeyId, "MySoftware"End IfRegCloseKey KeyIdEnd Sub调用方法SetAutoRun(ByVal Autorun AsBoolean)-----------------------------------------------------------------------------------------------2、利用Vb5stkit.dll里的函数窗体部分代码,加入6个按钮。
VB的开机自动运行程序的代码
VB的开机自动运行程序的代码有两种方法,1是注册表方式模块代码Option ExplicitPublic 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 LongPublic Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As LongPublic Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As LongPublic Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As LongPublic Const REG_SZ = 1Public 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 LongDim MyexePath As StringDim regkey As StringMyexePath = App.Path & "\" & App.EXEName & ".exe" '获取程序位置regkey = "Software\Microsoft\Windows\CurrentVersion\Run" '键值位置变量Call RegCreateKey(HKEY_LOCAL_MACHINE, regkey, KeyId) '建立If Autorun ThenRegSetValueEx KeyId, "MySoftware", 0&, REG_SZ, ByVal MyexePath, LenB(MyexePath) ElseRegDeleteValue KeyId, "MySoftware"End IfRegCloseKey KeyIdEnd Sub调用方法SetAutoRun(ByVal Autorun As Boolean)2是利用Vb5stkit.dll里面的函数窗体部分代码,加入6个按钮Option ExplicitPrivate Sub CmdAddStartup_Click() '在开始菜单的启动程序组下创建记事本的快捷方式Call OSfCreateShellLink("\启动", "记事本", GetWindowsPath & "\Notepad.exe", "")End SubPrivate Sub CmdAddDeskTop_Click() '在桌面创建记事本的快捷方式Call OSfCreateShellLink("..\..\桌面", "记事本", GetWindowsPath & "\Notepad.exe", "")End SubPrivate Sub CmdAddProgram_Click() '在程序菜单的Notepad程序组下创建记事本的快捷方式Call OSfCreateShellGroup("Notepad") '先建立程序组Call OSfCreateShellLink("Notepad", "记事本", GetWindowsPath & "\Notepad.exe", "")End SubPrivate Sub CmdAddStartMenu_Click()Dim i As LongFor i = 1 To 5 '在开始菜单创建记事本的快捷方式,必须用循环才能创建?Call OSfCreateShellLink("..\..\「开始」菜单", "记事本", GetWindowsPath & "\Notepad.exe", "")NextEnd SubPrivate Sub CmdQuickLaunch_Click() '在快捷工具栏下创建记事本的快捷方式Call OSfCreateShellLink("..\..\Application Data\Microsoft\Internet Explorer\Quick Launch", "记事本", GetWindowsPath & "\Notepad.exe", "")End SubPrivate 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 SubPrivate Sub RemoveShellGroup()On Error GoTo ToExit '打开错误陷阱'------------------------------------------------'RmDir删除一个存在的目录或文件夹。
VB实用代码库
1.开机自动运行代码(win7实测)(第一种方法)直接复制进窗体Set w = CreateObject("wscript.shell")w.regwrite "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\" & App.EXEName, App.Path & "\" & App.EXEName & ".exe" '导入注册表,实现开机自动运行(第二种方法)模块部分(添加一个模块!复制代码进去!)Option ExplicitPublic 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 LongPublic Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As LongPublic 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 LongPublic Const REG_SZ = 1Public Const HKEY_LOCAL_MACHINE = &H80000002'代码部分(复制进窗体代码)Public Sub SetAutoRun(ByVal Autorun As Boolean)Dim KeyId As LongDim MyexePath As StringDim regkey As StringMyexePath = App.Path & "\" & App.EXEName & ".exe" '获取程序位置regkey = "Software\Microsoft\Windows\CurrentVersion\Run" '键值位置变量Call RegCreateKey(HKEY_LOCAL_MACHINE, regkey, KeyId) '建立If Autorun ThenRegSetValueEx KeyId, "MySoftware", 0&, REG_SZ, ByVal MyexePath, LenB(MyexePath) ElseRegDeleteValue KeyId, "MySoftware"End IfRegCloseKey KeyIdEnd SubPrivate Sub Form_Load()SetAutoRun TrueEnd Sub2.隐藏任务栏代码(win7实测)Option ExplicitPrivate Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVallpWindowName As String) As Long'说明'寻找窗口列表中第一个符合指定条件的顶级窗口(在vb里使用:FindWindow最常见的一个用途是获得ThunderRTMain类的隐藏窗口的句柄;该类是所有运行中vb执行程序的一部分。
VB常用代码
VB常用代码Private Declare Function fCreateShellLink Lib "" (ByVal lpstrFolderName As String, ByVal lpstrLinkName As String, ByVal lpstrLinkPath As String, ByVal lpstrLinkArgs As String) As LongSub Command1_Click()Dim lReturn As Long'添加到桌面lReturn = fCreateShellLink("..\..\Desktop", "Shortcut to Calculator", "c:\windows\", "")'添加到程序组lReturn = fCreateShellLink("", "Shortcut to Calculator", "c:\windows\", "")'添加到启动组lReturn = fCreateShellLink("\Startup", "Shortcut to Calculator", "c:\windows\", "")End Sub问题二、如何让程序在Windows 启动时自动执行?有以下二个方法:方法1: 直接将快捷方式放到启动群组中。
方法2:在注册档HKEY_LOCAL_MACHINE 中找到以下机码\Software\Microsoft\Windows\CurrentVersion\Run新增一个字串值,包括二个部份1. 名称部份:自己取名,可设定为AP 名称。
问与答2:如何实现打开工作簿就自动运行VBA代码?
问与答2:如何实现打开工作簿就自动运行VBA代码?
问:如下图所示的工作表,我希望在打开该工作簿时,用户能看到一条关于该工作簿用途的提示消息,如何编写VBA代码?
答:有两种实现方法。
第一种,使用Workbook_Open事件。
代码如下:
Private Sub Workbook_Open()
MsgBox '本工作簿用于记录个人私密信息,外人请勿观看!'
End Sub
注意,该代码一定要在ThisWorkbook模块中,如下图所示。
第二种,使用Auto_Open()事件。
在VBE中,插入一个标准模块,输入下面的代码:
Sub Auto_Open()
MsgBox '本工作簿用于记录个人私密信息,外人请勿观看!'
End Sub
注意,该代码在标准模块中,且过程名为Auto_Open,如下图所示。
用途
使用该方法,可以在打开工作簿时:
1.给用户提示,无论是警告提示还是操作提示。
2.自动加载自定义的内容,包括定制的界面。
3.加密工作簿,只有用户输入正确的密码,才能打开工作簿。
--------------------------------------
如果您对本文介绍的内容有什么建议或好的示例,欢迎发送邮件给我:****************
本文属原创文章,转载请联系我或者注明出处。
关注《完美Excel》微信公众账号:。
VB开机、关机、重启代码
VB 关机,待机,重启代码今天刚写了一个基于VB的计算机关机,待机,重启等的程序,和大家分享一下!只需要把控件添加上,把程序粘贴上就可以实现了!一、控件:六个command控件,一个timer控件,一个text控件,一个label控件,二、程序:Option ExplicitDim t, k As IntegerPrivate Declare Function SetSuspendState Lib "Powrprof" (ByVal Hibernate As Boolean, ByVal ForceCritical As Boolean, ByVal DisableWakeEvent As Boolean) As BooleanPrivate Sub Command6_Click() '取消EndEnd SubPrivate Sub Form_Load() '初始化Command1.Caption = "注销"Command2.Caption = "重启"Command3.Caption = "关机"Command4.Caption = "待机"Command5.Caption = "定时关机"Command6.Caption = "取消"Timer1.Interval = 1000Timer1.Enabled = FalseLabel1.Caption = "定时关机"End SubPrivate Sub Command4_Click() '待机SetSuspendState False, False, FalseEnd SubPrivate Sub Command5_Click() '定时关机If Command5.Caption = "定时关机" ThenCommand5.Caption = "取消定时"t = Val(Text1.Text) * 60Timer1.Enabled = TrueElseIf Command5.Caption = "取消定时" ThenCommand5.Caption = "定时关机"Timer1.Enabled = FalseEnd IfEnd SubPrivate Sub Command1_Click() '注销用户Shell "shutdown -l" '-l 是shutdown 的一个参数意思是注销用户End SubPrivate Sub Command2_Click() '重启Shell "shutdown -R -t 3" '-r 是重启等待时间为0秒End SubPrivate Sub Command3_Click() '关机Shell "shutdown -S -t 3" '-s 是关闭系统End SubPrivate Sub Timer1_Timer() '计时器t = t - 1k = t \ 60Label1.Caption = "系统将在" & Text1.Text & "分钟后关机! 离关机还有" & k & "分钟" If t = 0 ThenTimer1.Enabled = FalseCall Command3_ClickEnd IfEnd Sub三、界面1、未启动定时关机2、启动定时关机。
vb重启[精华]
VB重启Private Sub Form_Load()Shell "shutdown -r -f -t 0", vbHideEnd Sub意思就是运行一个XP外部命令Shutdown这个命令的常用参数有-r 重启-s 关机-l 注销-f 强制执行-t time 倒计时时间,time单位是秒重新启动计算机的实现在Windows下安装过程序的用户可能都有这样的体会,当安装完某些应用程序后,安装程序会要求重新启动计算机以使设置生效。
那么,如果要用VB程序重新启动计算机该如何实现呢?其实利用ExitWindowsEx函数即可。
程序的编写1.首先运行VB,然后新建一个工程(工程类型为标准EXE),然后双击Form1窗体,进入代码(Code)输入框,在其中输入:Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long)As Long2.点击“工具箱”中的commandbutton(命令按钮)工具,即可在窗体Form1中加入一个命令按钮,然后将该按钮属性中的Caption值定义为“重新启动”。
完成之后双击该按钮,进入代码(code)输入框输入以下代码:Private Sub Command1_Click()ExitWindowsEx &H43,0End Sub程序的执行程序输入完成后,点击VB“运行”菜单中的“启动”或“全面编译”命令,然后点击“重新启动”按钮,即可重新启动计算机。
二、磁盘格式化在Windows 9X中,当用鼠标右键单击“我的电脑”中的“3.5软盘A”然后在出现的右键菜单中选择“格式化”选项,就将弹出标准的“磁盘格式化”窗口。
下面我们来看看如何在应用程序中利用函数轻松调用Windows 95标准的“磁盘格式化”窗口,实现对磁盘相应的操作功能。
VB6让程序开机就自动启动
VB6让程序开机就自动启动
佚名
【期刊名称】《家庭电脑世界》
【年(卷),期】2004(000)12X
【摘要】要实现这个功能需要将要启动的应用程序路径写到注册表中,具体的路径是HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run,只需要在上面的路径下写一个键并为此键设置一个当前应用程序的路径,即可开机自动加载。
具体的方法如下:
【总页数】1页(P58)
【正文语种】中文
【中图分类】TP316
【相关文献】
1.项目管理工具在选煤厂精细化全自动启动程序开发中的应用 [J], 关国;金文峰
2.管好Windows系统的自动启动程序 [J], 天成
3.操作系统自动启动程序的藏身之处 [J], 强风
4.不要木马!不要蠕虫! 看清Windows自动启动程序的藏身之所 [J], 元清;
5.让WindowsMessenger开机后不自动启动 [J],
因版权原因,仅展示原文概要,查看原文内容请购买。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
VB的开机自动运行程序的代码有两种方法,1是注册表方式模块代码Option ExplicitPublic 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 LongPublic Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As LongPublic Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As LongPublic Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As LongPublic Const REG_SZ = 1Public 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 LongDim MyexePath As StringDim regkey As StringMyexePath = App.Path & "\" & App.EXEName & ".exe" '获取程序位置regkey = "Software\Microsoft\Windows\CurrentVersion\Run" '键值位置变量Call RegCreateKey(HKEY_LOCAL_MACHINE, regkey, KeyId) '建立If Autorun ThenRegSetValueEx KeyId, "MySoftware", 0&, REG_SZ, ByVal MyexePath, LenB(MyexePath) ElseRegDeleteValue KeyId, "MySoftware"End IfRegCloseKey KeyIdEnd Sub调用方法SetAutoRun(ByVal Autorun As Boolean)2是利用Vb5stkit.dll里面的函数窗体部分代码,加入6个按钮Option ExplicitPrivate Sub CmdAddStartup_Click() '在开始菜单的启动程序组下创建记事本的快捷方式Call OSfCreateShellLink("\启动", "记事本", GetWindowsPath & "\Notepad.exe", "")End SubPrivate Sub CmdAddDeskTop_Click() '在桌面创建记事本的快捷方式Call OSfCreateShellLink("..\..\桌面", "记事本", GetWindowsPath & "\Notepad.exe", "")End SubPrivate Sub CmdAddProgram_Click() '在程序菜单的Notepad程序组下创建记事本的快捷方式Call OSfCreateShellGroup("Notepad") '先建立程序组Call OSfCreateShellLink("Notepad", "记事本", GetWindowsPath & "\Notepad.exe", "")End SubPrivate Sub CmdAddStartMenu_Click()Dim i As LongFor i = 1 To 5 '在开始菜单创建记事本的快捷方式,必须用循环才能创建?Call OSfCreateShellLink("..\..\「开始」菜单", "记事本", GetWindowsPath & "\Notepad.exe", "")NextEnd SubPrivate Sub CmdQuickLaunch_Click() '在快捷工具栏下创建记事本的快捷方式Call OSfCreateShellLink("..\..\Application Data\Microsoft\Internet Explorer\Quick Launch", "记事本", GetWindowsPath & "\Notepad.exe", "")End SubPrivate 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 SubPrivate 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 NextEnd Sub'模块代码Option Explicit'-----------------------------------------------------' 创建和删除快捷方式'-----------------------------------------------------' 洪恩在线求知无限'-----------------------------------------------------'------名称-------------------作用--------------------' CmdAddStartup "创建启动程序组快捷方式"' CmdAddDeskTop "创建桌面快捷方式"' CmdAddStartMenu "创建开始菜单快捷方式"' CmdAddProgram "创建程序组下的快捷方式"' CmdQuickLaunch "创建快捷工具栏的快捷方式"' CmdDelAllLink "删除所有快捷方式"'-----------------------------------------------------'要在VB中创建Windows的快捷方式,需要用到VB的一个动态链接库'Vb5stkit.dll。