最简单的方法是按Ctrl+E打开菜单编辑器,然后设置个菜单项为退出什么的,设置其快捷键为Ctrl+D,勾选掉“可见”属性(如果是“不可见”属性请勾上)。然后运行就可以了。
还有的方法:(代码)
方法①:
Private
Sub
Form_KeyDown(KeyCode
As
Integer,
Shift
As
Integer)
Me.KeyPreview
=
True
If
KeyCode
=
vbKeyD
And
Shift
=
2
Then
End
End
Sub
方法②:
Private
Sub
Form_KeyUp(KeyCode
As
Integer,
Shift
As
Integer)
Me.KeyPreview
=
True
If
KeyCode
=
vbKeyD
And
Shift
=
2
Then
End
End
Sub
方法③:
Private
Sub
Form_KeyPress(KeyAscii
As
Integer)
If
KeyAscii
=
4
Then
End
End
Sub
方法①②③的灵敏度不一样,③里面的KeyAscii=4是Ctrl+D的Ascii值,是测试出来的。
至于Ascii值测试代码,只要将方法③的语句换为MsgBox
KeyAscii就行了
很简单,在窗体的keydown事件中写
===================================
keydown事件不可行,因为这是全局热键,给你一个完整的代码:
'*************************************************************************
'**模 块 名:窗体代码
'**说 明:厦门大学化学系 版权所有2009 - 2010(C)
'**创 建 人:吴志明
'**日 期:2009-02-26 21:20:14
'**修 改 人:
'**日 期:
'**描 述:
'**版 本:V1.0.0
'*************************************************************************
Option Explicit
Private Sub Form_Load()
RegHotKey Me.hwnd
End Sub
Private Sub Form_Unload(Cancel As Integer)
UnRegHotKey Me.hwnd
End Sub
'*************************************************************************
'**模 块 名:模块代码
'**说 明:厦门大学化学系 版权所有2009 - 2010(C)
'**创 建 人:吴志明
'**日 期:2009-02-26 21:19:53
'**修 改 人:
'**日 期:
'**描 述:
'**版 本:V1.0.0
'*************************************************************************
Option Explicit
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
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
Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long) As Long
Public Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Public Const WM_HOTKEY = &H312
Public Const MOD_ALT = &H1
Public Const MOD_CONTROL = &H2
Public Const MOD_SHIFT = &H4
Public Const GWL_WNDPROC = (-4)
Public preWinProc As Long
Public Modifiers As Long, uVirtKey As Long, idHotKey As Long
Private Type taLong
ll As Long
End Type
Private Type t2Int
lWord As Integer
hWord As Integer
End Type
Private Function Wndproc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If Msg = WM_HOTKEY Then
If wParam = idHotKey Then
Dim lp As taLong, i2 As t2Int
lp.ll = lParam
LSet i2 = lp
If (i2.lWord = Modifiers) And i2.hWord = uVirtKey Then
frmMain.WindowState = 0
SetForegroundWindow frmMain.hwnd
End If
End If
End If
'如果不是热键信息则调用原来的程序
Wndproc = CallWindowProc(preWinProc, hwnd, Msg, wParam, lParam)
End Function
Public Sub RegHotKey(hwnd As Long)
Dim ret As Long
'记录原来的window程序地址
preWinProc = GetWindowLong(hwnd, GWL_WNDPROC)
'用自定义程序代替原来的window程序
ret = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf Wndproc)
idHotKey = 1
Modifiers = MOD_ALT 'Alt键
uVirtKey = vbKeyX 'X键
ret = RegisterHotKey(hwnd, idHotKey, Modifiers, uVirtKey)
End Sub
Public Sub UnRegHotKey(hwnd As Long)
Dim ret As Long
'取消Message的截取,使之送往原来的windows程序
ret = SetWindowLong(hwnd, GWL_WNDPROC, preWinProc)
Call UnregisterHotKey(hwnd, uVirtKey)
End Sub
'以下在模块中,在窗体调用SetHotKey,或者把SetHotKey中的内容放到窗体
'自己写的,反正能用,是全局快捷键.
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 GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex 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 RegisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long, ByVal fskey_Modifiers 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 Const WM_HOTKEY = &H312
Public Const MOD_ALT = &H1
Public Const MOD_CONTROL = &H2
Public Const MOD_SHIFT = &H4
Public Const GWL_WNDPROC = (-4)
Public preWinProc As Long
Public Modifiers As Long, uVirtKey1 As Long, idHotKey As Long
Public Function SetHotKey()
preWinProc = GetWindowLong(Form1.hwnd, GWL_WNDPROC)
SetWindowLong Form1.hwnd, GWL_WNDPROC, AddressOf Keywndproc
RegisterHotKey Form1.hwnd, 1, 0, vbKey1 '注册热键
RegisterHotKey Form1.hwnd, 2, 0, vbKey2 '注册热键
End Function
Public Function Keywndproc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If Msg = WM_HOTKEY Then
Select Case wParam
Case 1
MsgBox "1" '热键动作
Case 2
MsgBox "2" '热键动作
End Select
End If
Keywndproc = CallWindowProc(preWinProc, hwnd, Msg, wParam, lParam)
End Function
'模块中
Option Explicit
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
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
Declare Function RegisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
Declare Function UnregisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal id As Long) As Long
Public Const WM_HOTKEY = &H312
Public Const MOD_ALT = &H1
Public Const MOD_CONTROL = &H2
Public Const MOD_SHIFT = &H4
Public Const GWL_WNDPROC = (-4)
Public preWinProc As Long
Public Modifiers As Long, uVirtKey1 As Long, idHotKey As Long
Private Type taLong
ll As Long
End Type
Private Type t2Int
lWord As Integer
hword As Integer
End Type
Public Function wndproc(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim lp As taLong, i2 As t2Int
If Msg = WM_HOTKEY Then
If wParam = idHotKey Then
lp.ll = lParam
LSet i2 = lp
If (i2.lWord = Modifiers) And i2.hword = uVirtKey1 Then
'这里面是快捷键代码,你可以随便改
Form1.Visible = True
Form1.WindowState = 0
Form1.Visible = True
'------------------------------------------------------
End If
End If
End If
'如果不是热键信息则调用原来的程序
wndproc = CallWindowProc(preWinProc, hWnd, Msg, wParam, lParam)
End Function
'窗体中
Option Explicit
Const MAX_TOOLTIP As Integer = 64
Const NIF_ICON = &H2
Const NIF_MESSAGE = &H1
Const NIF_TIP = &H4
Const NIM_ADD = &H0
Const NIM_DELETE = &H2
Const WM_MOUSEMOVE = &H200
Const WM_LBUTTONDOWN = &H201
Const WM_LBUTTONUP = &H202
Const WM_LBUTTONDBLCLK = &H203
Const WM_RBUTTONDOWN = &H204
Const WM_RBUTTONUP = &H205
Const WM_RBUTTONDBLCLK = &H206
Const SW_RESTORE = 9
Const SW_HIDE = 0
Private Type NOTIFYICONDATA
cbSize As Long
hWnd As Long
uID As Long
uFlags As Long
uCallbackMessage As Long
hIcon As Long
szTip As String * MAX_TOOLTIP
End Type
Private Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, _
ByVal nCmdShow As Long) As Long
Private Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" _
(ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
Private nfIconData As NOTIFYICONDATA
Dim vDoc, vTag
Private Sub Form_Load()
nfIconData.hWnd = Me.hWnd
nfIconData.uID = Me.Icon
nfIconData.uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP
nfIconData.uCallbackMessage = WM_MOUSEMOVE
nfIconData.hIcon = Me.Icon.Handle
nfIconData.szTip = "这是你的软件名" & vbNullChar
nfIconData.cbSize = Len(nfIconData)
Call Shell_NotifyIcon(NIM_ADD, nfIconData)
Dim ret As Long
'记录原来的window程序地址
preWinProc = GetWindowLong(Me.hWnd, GWL_WNDPROC)
'用自定义程序代替原来的window程序
ret = SetWindowLong(Me.hWnd, GWL_WNDPROC, AddressOf wndproc)
idHotKey = 1 'in the range &h0000 through &hBFFF
Modifiers = MOD_ALT '辅助键为Alt
uVirtKey1 = vbKeyX '注册的热键为Alt+X
'注册热键
ret = RegisterHotKey(Me.hWnd, idHotKey, Modifiers, uVirtKey1)
If ret = 0 Then
MsgBox "注册热键失败,请使用其它热键!", vbCritical, "错误"
End If
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Dim ret As Long
'取消Message的截取,使之送往原来的window程序
ret = SetWindowLong(Me.hWnd, GWL_WNDPROC, preWinProc)
Call UnregisterHotKey(Me.hWnd, uVirtKey1)
ShowWindow Me.hWnd, SW_HIDE
End
End Sub
Private Sub Form_Resize()
If Me.WindowState = 1 Then
ShowWindow Me.hWnd, SW_HIDE
Me.Visible = False
End If
End Sub
自己写的。。不过我的原程序比这个多了一个功能。。如果你想要的话,我传给你,留下邮箱
alt+f4
这个不用,默认系统就是用来关闭当前程序的~
private
sub
form_keydown(keycode
as
integer,
shift
as
integer)
if
keycode
=
vbkeyf1
then
image1_click
if
keycode
=
vbkeyf2
then
image2_click
。。。。
end
sub