vb 用快捷键来隐藏和显示窗体
无论窗体有没有焦点,快捷键都要能用。按一下就显示,再按一下就隐藏。代码怎么写呀?我是自己做了个。想用快捷键来隐藏和显示窗体1楼。。。。我汗。。2楼你错了。我不是要同时按键...
无论窗体有没有焦点,快捷键都要能用。按一下就显示,再按一下就隐藏。代码怎么写呀?
我是自己做了个。想用快捷键来隐藏和显示窗体1楼。。。。我汗。。 2楼你错了。我不是要同时按键Form1.Visible = True
Form1.Visible = False
也就是说热只实现这两个功能就可以了。 展开
我是自己做了个。想用快捷键来隐藏和显示窗体1楼。。。。我汗。。 2楼你错了。我不是要同时按键Form1.Visible = True
Form1.Visible = False
也就是说热只实现这两个功能就可以了。 展开
2个回答
展开全部
展开全部
添加两个按钮
Form1代码:
Private Sub Command1_Click()
Form1.Hide
End Sub
Private Sub Command2_Click()
Form1.Show
End Sub
Private Sub Form_Load()
EnableKBDHook
End Sub
Private Sub Form_Unload(Cancel As Integer)
UnHookKBD
End
End Sub
在添加一个模块:点工程在点添加模块
在模块代码里写:
Option Explicit
Public Const WH_KEYBOARD = 13
Public Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Public Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long
Public hNextHookProc 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 BM_CLICK = &HF5
Public Type KBDLLHOOKSTRUCT
vkCode As Long
scanCode As Long
flags As Long
time As Long
dwExtraInfo As Long
End Type
Public Sub UnHookKBD()
On Error Resume Next
If hNextHookProc <> 0 Then
UnhookWindowsHookEx hNextHookProc
hNextHookProc = 0
End If
End Sub
Public Function EnableKBDHook()
On Error Resume Next
If hNextHookProc <> 0 Then
Exit Function
End If
hNextHookProc = SetWindowsHookEx(WH_KEYBOARD, AddressOf MyKBHFunc, App.hInstance, 0)
If hNextHookProc <> 0 Then
EnableKBDHook = hNextHookProc
End If
End Function
Public Function MyKBHFunc(ByVal iCode As Long, ByVal wParam As Long, lParam As KBDLLHOOKSTRUCT) As Long
On Error Resume Next
MyKBHFunc = 0
If iCode < 0 Then
MyKBHFunc = CallNextHookEx(hNextHookProc, iCode, wParam, lParam)
Exit Function
End If
If lParam.vkCode = vbKeyHome Then
SendMessage Form1.Command1.hwnd, &HF5, 0, ByVal 0 '这里按下home键就等于按下form1的command1
ElseIf lParam.vkCode = vbKeyEnd Then
MyKBHFunc = 1
SendMessage Form1.Command2.hwnd, &HF5, 0, ByVal 0 '这里按下End键就等于按下form1的command2
MyKBHFunc = 1
Else
Call CallNextHookEx(hNextHookProc, iCode, wParam, lParam)
End If
End Function
你在试试
Form1代码:
Private Sub Command1_Click()
Form1.Hide
End Sub
Private Sub Command2_Click()
Form1.Show
End Sub
Private Sub Form_Load()
EnableKBDHook
End Sub
Private Sub Form_Unload(Cancel As Integer)
UnHookKBD
End
End Sub
在添加一个模块:点工程在点添加模块
在模块代码里写:
Option Explicit
Public Const WH_KEYBOARD = 13
Public Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Public Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long
Public hNextHookProc 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 BM_CLICK = &HF5
Public Type KBDLLHOOKSTRUCT
vkCode As Long
scanCode As Long
flags As Long
time As Long
dwExtraInfo As Long
End Type
Public Sub UnHookKBD()
On Error Resume Next
If hNextHookProc <> 0 Then
UnhookWindowsHookEx hNextHookProc
hNextHookProc = 0
End If
End Sub
Public Function EnableKBDHook()
On Error Resume Next
If hNextHookProc <> 0 Then
Exit Function
End If
hNextHookProc = SetWindowsHookEx(WH_KEYBOARD, AddressOf MyKBHFunc, App.hInstance, 0)
If hNextHookProc <> 0 Then
EnableKBDHook = hNextHookProc
End If
End Function
Public Function MyKBHFunc(ByVal iCode As Long, ByVal wParam As Long, lParam As KBDLLHOOKSTRUCT) As Long
On Error Resume Next
MyKBHFunc = 0
If iCode < 0 Then
MyKBHFunc = CallNextHookEx(hNextHookProc, iCode, wParam, lParam)
Exit Function
End If
If lParam.vkCode = vbKeyHome Then
SendMessage Form1.Command1.hwnd, &HF5, 0, ByVal 0 '这里按下home键就等于按下form1的command1
ElseIf lParam.vkCode = vbKeyEnd Then
MyKBHFunc = 1
SendMessage Form1.Command2.hwnd, &HF5, 0, ByVal 0 '这里按下End键就等于按下form1的command2
MyKBHFunc = 1
Else
Call CallNextHookEx(hNextHookProc, iCode, wParam, lParam)
End If
End Function
你在试试
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询