vb做好的程序如何使用快捷键打开
vb做好的程序如何使用快捷键打开,我做的一个小程序,不需要安装的,也不想打包,很小。我想点击一个按钮就在桌面创建一个快捷方式,然后自动设置一个快捷键比如F6,在没有打开程序时,按F6就可以打开我的程序,该怎么写代码,或者用别的方法也可以,写详细一点,我刚学VB,很多不懂的,谢谢了哈。
我随时看帖子,成功的话马上给分。
[解决办法]
给你看这个模块
Public Declare Function RegisterHotKey Lib "user32 " (ByVal hwnd As Long, _
ByVal ID As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
Public Declare Function UnregisterHotKey Lib "user32 " (ByVal hwnd As Long, _
ByVal ID As Long) As Long
Public Declare Function GetWindowLong Lib "user32 " Alias "GetWindowLongA " _
(ByVal hwnd As Long, ByVal nIndex As Long) As Long
Public Declare Function SetWindowLong Lib "user32 " Alias "SetWindowLongA " _
(ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function CallWindowProc Lib "user32 " Alias "CallWindowProcA " _
(ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Function SendMessage Lib "user32 " Alias "SendMessageA " (ByVal _
hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Const WM_HOTKEY = &H312
Public Const GWL_WNDPROC = -4
Public Const WM_SETHOTKEY = &H32
Public Const MOD_CTRL = &H2
Public Const MOD_SHFT = &H4
Public Const MOD_ALT = &H1
'Public Const HOTKEYF_SHIFT = &H1 '注释: shift键
'Public Const HOTKEYF_CONTROL = &H2 '注释:ctrl键
'Public Const HOTKEYF_ALT = &H4 '注释:alt键
'Public Const VK_NUMPAD0 = &H60
'Public Const VK_NUMPAD1 = &H61
'Public Const VK_NUMPAD2 = &H62
'Public Const VK_NUMPAD3 = &H63
'Public Const VK_NUMPAD4 = &H64
'Public Const VK_NUMPAD5 = &H65
'Public Const VK_NUMPAD6 = &H66
'Public Const VK_NUMPAD7 = &H67
'Public Const VK_NUMPAD8 = &H68
'Public Const VK_NUMPAD9 = &H69
'Public Const VK_MULTIPLY = &H6A
'Public Const VK_ADD = &H6B
'Public Const VK_SEPARATOR = &H6C
'Public Const VK_SUBTRACT = &H6D
'Public Const VK_DECIMAL = &H6E
'Public Const VK_DIVIDE = &H6F
Public Const VK_F1 = &H70
Public Const VK_F2 = &H71
Public Const VK_F3 = &H72
Public Const VK_F4 = &H73
Public Const VK_F5 = &H74
Public Const VK_F6 = &H75
Public Const VK_F7 = &H76
Public Const VK_F8 = &H77
Public Const VK_F9 = &H78
Public Const VK_F10 = &H79
Public Const VK_F11 = &H7A
Public Const VK_F12 = &H7B
Public Const VK_F13 = &H7C
Public Const VK_F14 = &H7D
Public Const VK_F15 = &H7E
Public Const VK_F16 = &H7F
Public Const VK_F17 = &H80
Public Const VK_F18 = &H81
Public Const VK_F19 = &H82
Public Const VK_F20 = &H83
Public Const VK_F21 = &H84
Public Const VK_F22 = &H85
Public Const VK_F23 = &H86
Public Const VK_F24 = &H87
'阻止关机
Private Const WM_QUERYENDSESSION = &H11
Global Const WM_CANCELMODE = &H1F
Private isShutDown As Boolean
Public 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
Public glWinRet As Long, preWinProc As Long
Public unloadHotKey As Integer
Public Function CallbackMsgs(ByVal wHwnd As Long, ByVal wMsg As Long, ByVal wp_id As Long, ByVal lp_id As Long) As Long
If wMsg = WM_HOTKEY Then
Call DoFunctions(wp_id)
CallbackMsgs = 1
Exit Function
ElseIf wMsg = WM_QUERYENDSESSION Then
If isShutDown Then
CallbackMsgs = CallWindowProc(glWinRet, wHwnd, WM_CANCELMODE, wp_id, lp_id)
Exit Function
End If
End If
CallbackMsgs = CallWindowProc(glWinRet, wHwnd, wMsg, wp_id, lp_id)
End Function
[解决办法]
转自zcsor的专栏blog
如何创建快捷方式(包括在任意磁盘根目录及子目录位置创建文件,目录甚至我的电脑的快捷方式)
首先保证你有权限:运行应用程序、建立目录
这里要说的不是API,是WScript.Shell对象。
创建文件及目录的快捷方式的源码如下:
创建模块并粘贴以下代码:
Option Explicit
'参数:快捷方式名称[.LNK];快捷方式图标所在文件及图标序号;文件原始路径;[文件名及扩展名];[快捷方式热键];[快捷方式备注];[快捷方式目标路径]
Public Sub mShellLnk(ByVal LnkName As String, IconFileIconIndex As String, ByVal FilePath As String, Optional ByVal FileName As String, Optional ByVal HookKey As String = " ", Optional ByVal StrRemark As String = " ", Optional ByVal strDesktop As String = " ")
Dim WshShell As Object, WScript As Object, oShellLink As Object
Set WshShell = CreateObject( "WScript.Shell ")
If strDesktop = " " Then strDesktop = WshShell.SpecialFolders( "Desktop ") '桌面路径
If UCase(Right(LnkName, 4)) = ".LNK " Then
Set oShellLink = WshShell.CreateShortcut(strDesktop & "\ " & LnkName) '创建快捷方式,参数为路径和名称
Else
Set oShellLink = WshShell.CreateShortcut(strDesktop & "\ " & LnkName & ".lnk ")
End If
oShellLink.TargetPath = FilePath & "\ " & FileName
oShellLink.WindowStyle = 1 '风格
oShellLink.Hotkey = HookKey '热键
oShellLink.IconLocation = IconFileIconIndex '图标
oShellLink.Description = StrRemark '快捷方式备注内容
oShellLink.WorkingDirectory = FilePath '源文件所在目录
oShellLink.Save '保存创建的快捷方式
Set WshShell = Nothing
Set oShellLink = Nothing
End Sub
调用方式如下:
创建窗体并粘贴以下代码:
Private Sub Form_Click()
'===========================================
'演示创建目录的桌面快捷方式
'===========================================
'创建C盘的桌面快捷方式
mShellLnk "演示程序创建的 C盘快捷方式.lnk ", "Explorer.exe, 0 ", "C:\ ", " ", "Ctrl+Shift+C ", "C盘 "
'===========================================
'演示创建文件的桌面快捷方式
'===========================================
'在C盘根目录创建记事本的桌面快捷方式
mShellLnk "演示程序创建的 记事本快捷方式.lnk ", "NOTEPAD.exe, 0 ", "NOTEPAD.EXE ", " ", "Ctrl+Shift+N ", "记事本 ", "D:\ "
End Sub