看例子...运行时窗体隐藏,用Ctrl+F12切换显示与隐藏 Private Declare Function RegisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long Private Declare Function UnregisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal id As Long) As Long Private Declare Function PeekMessage Lib "user32" Alias "PeekMessageA" (lpMsg As Msg, ByVal hWnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long, ByVal wRemoveMsg As Long) As Long Private bCancel As Boolean '用于热键有效与否的变量 '全局变量 Private Const MOD_ALT = &H1 Private Const MOD_CONTROL = &H2 Private Const MOD_SHIFT = &H4 Private Const PM_REMOVE = &H1 Private Const WM_HOTKEY = &H312 Private Type Msg '定义Message类型 hWnd As Long Message As Long wParam As Long lParam As Long time As Long End Type Private Sub proMsg() '声明进程 Dim Message As Msg ' 循环直到 bCancel 为真 Do While Not bCancel '检测到热键被按下则令窗体隐藏 If PeekMessage(Message, Me.hWnd, WM_HOTKEY, WM_HOTKEY, PM_REMOVE) Then Me.Visible = Not Me.Visible End If DoEvents Loop End Sub Private Sub Form_Load() '窗体加载时初始化程序 Dim ret As Long bCancel = False '定义热键为Ctrl+F12 ret = RegisterHotKey(Me.hWnd, &HBFFF&, MOD_CONTROL, vbKeyF12) 'Me.Visible = False '窗体不可见 proMsg End Sub '退出时注销热键 Private Sub Form_Unload(Cancel As Integer) bCancel = True Call UnregisterHotKey(Me.hWnd, &HBFFF&) End Sub
首先将窗体的KeyPriview 属性设置为True,这样窗体可以优先接收键盘事件,然后在模块中定义一个公有变量,判别是显示窗体还是隐藏窗体。最后,对Form_KeyDown(KeyCode As Integer, Shift As Integer)方法进行编码即可。 Public Kshow as boolean Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) If KeyCode = vbKeyF8 and Kshow= True Then me.Visible =False Kshow=False else If KeyCode = vbKeyF8 and Kshow= False Then me.Visible =True Kshow=True End If End If End Sub
Private Declare Function UnregisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal id As Long) As Long
Private Declare Function PeekMessage Lib "user32" Alias "PeekMessageA" (lpMsg As Msg, ByVal hWnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long, ByVal wRemoveMsg As Long) As Long Private bCancel As Boolean '用于热键有效与否的变量 '全局变量
Private Const MOD_ALT = &H1
Private Const MOD_CONTROL = &H2
Private Const MOD_SHIFT = &H4
Private Const PM_REMOVE = &H1
Private Const WM_HOTKEY = &H312 Private Type Msg '定义Message类型
hWnd As Long
Message As Long
wParam As Long
lParam As Long
time As Long
End Type Private Sub proMsg() '声明进程
Dim Message As Msg
' 循环直到 bCancel 为真
Do While Not bCancel
'检测到热键被按下则令窗体隐藏
If PeekMessage(Message, Me.hWnd, WM_HOTKEY, WM_HOTKEY, PM_REMOVE) Then
Me.Visible = Not Me.Visible
End If
DoEvents
Loop
End Sub Private Sub Form_Load() '窗体加载时初始化程序
Dim ret As Long
bCancel = False
'定义热键为Ctrl+F12
ret = RegisterHotKey(Me.hWnd, &HBFFF&, MOD_CONTROL, vbKeyF12)
'Me.Visible = False '窗体不可见
proMsg
End Sub '退出时注销热键
Private Sub Form_Unload(Cancel As Integer)
bCancel = True
Call UnregisterHotKey(Me.hWnd, &HBFFF&)
End Sub
Public Kshow as boolean
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyF8 and Kshow= True Then
me.Visible =False
Kshow=False
else
If KeyCode = vbKeyF8 and Kshow= False Then
me.Visible =True
Kshow=True
End If
End If
End Sub