Office中国论坛/Access中国论坛

标题: 用键盘钩子屏蔽CTRL+P,在ACCESS2010下通过,但在ACCESS2003却不起作用 [打印本页]

作者: leoyan76    时间: 2013-2-26 10:12
标题: 用键盘钩子屏蔽CTRL+P,在ACCESS2010下通过,但在ACCESS2003却不起作用
本帖最后由 leoyan76 于 2013-2-26 10:15 编辑

我正做个PDF文档管理软件,想通过键盘钩子屏蔽CTRL+P打印键,目前已在ACCESS2010下运行正常,但同样文件,在ACCESS2003下却不能屏蔽CTRL+P,但无错误信息,请教各位了,望不吝赐教!
模块代码如下:
Option Explicit


'通知Windows进行钩子操作并定义钩子函数
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
'idHook:拦截类型     lpfn:挂接函数链的首地址指针
'hmod:创建钩子函数实体的句柄,即程序本身的句柄
'dwThreadId:为监控代码,0表示全局监控,dwThreadId用于线程钩子VB中可以设置为App.ThreadID。

Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
'释放钩子
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, _
pSource As Any, ByVal cb As Long)
'将内存里的某一块数据pSource拷贝到另一个地址pDest,cb表示拷贝内容的字节大小

Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer

Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, _
ByVal nCode As Long, ByVal wParam As Long, _
ByVal lParam As Long) As Long
'挂钩函数拦截了某条消息后,由CallNextHookEx决定是否将这些消息送还给Windows系统

Private Type KBDLLHOOKSTRUCT  '键盘钩子的结构体
    vkCode As Long    '虚拟键码
    scanCode As Long   '扫描码
    flags As Long       '功能键状态
    time As Long
    dwExtraInfo As Long
End Type

Public KeyboardHandle As Long   '键盘钩子函数句柄
Private Const HC_ACTION = 0
Private Const LLKHF_EXTENDED = &H1
Private Const LLKHF_INJECTED = &H10
Private Const LLKHF_ALTDOWN = &H20
Private Const LLKHF_UP = &H80
Public Const VK_A = &H41
Public Const VK_P = &H50
Public Const VK_ENTER = &HD
Public Const VK_TAB = &H9
Public Const VK_CONTROL = &H11
Public Const VK_ESCAPE = &H1B
Public Const VK_DELETE = &H2E

'钩子函数的核心
Public Function KeyboardCallback(ByVal Code As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
'Code 表示拦截层次,之前我们已经说过,如果Code为0,则拦截所有窗口的键盘输入
'wParam 表示是何种Windows消息
'lParam表示某条Windows消息的具体内容的指针,它实际指向存储那个内容的内存地址

Static Hookstruct As KBDLLHOOKSTRUCT  '定义一个局部静态结构体实例
    If (Code = HC_ACTION) Then   '鉴别Windows的消息来源
        Call CopyMemory(Hookstruct, ByVal lParam, Len(Hookstruct))
        If (IsHooked(Hookstruct)) Then   '过滤消息
            KeyboardCallback = 1
            Exit Function
        End If
    End If
KeyboardCallback = CallNextHookEx(KeyboardHandle, Code, wParam, lParam)
'将消息释放,用CallNextHookEx交还给系统
End Function
Public Function IsHooked(ByRef Hookstruct As KBDLLHOOKSTRUCT) As Boolean
    'If (KeyboardHook Is Nothing) Then
    If KeyboardHandle = 0 Then
        IsHooked = False
        Exit Function
    End If
    '有时候CopyMemory也会发生意想不到的事情,所以,当KeyboardHook = Nothing (无值)的情况下,退出,略过该函数,以防不可预知的错误。

    MsgBox Hookstruct.vkCode
    MsgBox (GetAsyncKeyState(VK_CONTROL))

    If (Hookstruct.vkCode = VK_P) And _
       (GetAsyncKeyState(VK_CONTROL) < 0) Then       '屏蔽CTRL+P键组合

        IsHooked = True

        Exit Function
    End If
    '以上拦截了Alt+Tab的键盘组合,并将IsHooked返回True(就是1),表示本次按键确实符合了过滤原则,应该吞吃掉。
    'If (Hookstruct.vkCode = VK_ENTER) Then   '屏蔽ENTER键
        'IsHooked = True
        'Exit Function
   ' End If
    'If (Hookstruct.vkCode = VK_P) Then   '屏蔽A和a键
       ' IsHooked = True
        'Exit Function
    'End If
End Function

Public Sub UnhookKeyboard()   '释放钩子函数
'If (Hooked) Then
Call UnhookWindowsHookEx(KeyboardHandle)
'End If
End Sub

窗体代码如下:
Option Compare Database
Option Explicit

Private Const WH_KEYBOARD_LL = 13&
Public Sub HookKeyboard()
KeyboardHandle = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf KeyboardCallback, 0, 0&)
'WH_KEYBOARD_LL:拦截类型
'AddressOf KeyboardCallback:挂接函数链的首地址
'App.hInstance:程序本身的句柄
'0,表示全局拦截,意思就是拦截所有窗口下的键盘输入
End Sub



Private Sub Form_Close()
UnhookKeyboard
End Sub


Private Sub Form_Load()   
HookKeyboard
End Sub

作者: tmtony    时间: 2013-2-26 13:00
钩子在access不太稳定
直接使用 autokey 屏蔽ctr+p是否可以?
作者: leoyan76    时间: 2013-2-26 13:06
我使用pdfview.ocx控件,用autokey可以屏蔽表单上的CTRL+P,但当鼠标点击pdfview.ocx控件后,却无法屏蔽CTRL+P.




欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3