以下代码在模块------------
程序代码: | [ 复制代码 ] |
'http://www.cnng.net
'石陆软件屋
Public Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
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 Const HC_ACTION = 0
Public Const WH_KEYBOARD = 2
Public Function MyHotKey(vKeyCode) As Boolean
MyHotKey = ((GetAsyncKeyState(vKeyCode) < 0))
End Function
Public Sub UnHookKBD()
If hnexthookproc <> 0 Then
UnhookWindowsHookEx hnexthookproc
hnexthookproc = 0
End If
End Sub
Public Function EnableKBDHook()
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, ByVal lParam As Long) As Long
'wParam 是传入按了哪个key的virtual-key code
'如果直接使用下面两行代码则所有键盘的输入皆没有作用
'MyKBHFunc = 1 '吃掉讯息
'Exit Function
MyKBHFunc = 0 '讯息要处理
If iCode < 0 Then
MyKBHFunc = CallNextHookEx(hnexthookproc, iCode, wParam, lParam)
Exit Function
End If
If wParam = 115 Then '侦测 有没有按到选定的按键(这里是F4)
MyKBHFunc = 0 '先设定为处理信息
If MyHotKey(18) Then MyKBHFunc = 1 '如果当时也按了ALT就便吃掉这个讯息
If MyKBHFunc = 1 Then Form1.Caption = "石陆软件屋提示:你刚才的键盘输入为Alt+F4,表示要将程序关闭!"
Else
Call CallNextHookEx(hnexthookproc, iCode, wParam, lParam)
End If
End Function
'石陆软件屋
Public Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
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 Const HC_ACTION = 0
Public Const WH_KEYBOARD = 2
Public Function MyHotKey(vKeyCode) As Boolean
MyHotKey = ((GetAsyncKeyState(vKeyCode) < 0))
End Function
Public Sub UnHookKBD()
If hnexthookproc <> 0 Then
UnhookWindowsHookEx hnexthookproc
hnexthookproc = 0
End If
End Sub
Public Function EnableKBDHook()
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, ByVal lParam As Long) As Long
'wParam 是传入按了哪个key的virtual-key code
'如果直接使用下面两行代码则所有键盘的输入皆没有作用
'MyKBHFunc = 1 '吃掉讯息
'Exit Function
MyKBHFunc = 0 '讯息要处理
If iCode < 0 Then
MyKBHFunc = CallNextHookEx(hnexthookproc, iCode, wParam, lParam)
Exit Function
End If
If wParam = 115 Then '侦测 有没有按到选定的按键(这里是F4)
MyKBHFunc = 0 '先设定为处理信息
If MyHotKey(18) Then MyKBHFunc = 1 '如果当时也按了ALT就便吃掉这个讯息
If MyKBHFunc = 1 Then Form1.Caption = "石陆软件屋提示:你刚才的键盘输入为Alt+F4,表示要将程序关闭!"
Else
Call CallNextHookEx(hnexthookproc, iCode, wParam, lParam)
End If
End Function
以下代码在窗体------------
程序代码: | [ 复制代码 ] |
Private Sub Form_Load()
Call EnableKBDHook
End Sub
Private Sub Form_Unload(Cancel As Integer)
Call UnHookKBD
End Sub
Call EnableKBDHook
End Sub
Private Sub Form_Unload(Cancel As Integer)
Call UnHookKBD
End Sub
相关链接:vb | 屏蔽 | Alt+F4
[本日志由 admin 于 2013-11-01 10:50 PM 编辑]
引用通告地址 (0):
http://www.cnng.net/blog/trackback.asp?tbID=32
http://www.cnng.net/blog/trackback.asp?tbID=32&CP=GBK
http://www.cnng.net/blog/trackback.asp?tbID=32
http://www.cnng.net/blog/trackback.asp?tbID=32&CP=GBK