使用VB在应用程序中注册热键

<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

'窗体中Option Explicit

Private Sub Form_Load()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 = VBKeyQ          '注册的热键为Alt+Q    '注册热键    ret = RegisterHotKey(Me.hWnd, idHotKey, Modifiers, uVirtKey1)    If ret = 0 Then        MsgBox "注册热键失败,请使用其它热键!", VBCritical, "错误"    End IfEnd 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)End Sub

'模块中

'以下程序放在模块中Option Explicit

Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As LongDeclare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As LongDeclare 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 LongDeclare Function RegisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As LongDeclare Function UnregisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal id As Long) As Long

Public Const WM_HOTKEY = &H312Public Const MOD_ALT = &H1Public Const MOD_CONTROL = &H2Public Const MOD_SHIFT = &H4Public Const GWL_WNDPROC = (-4)

Public preWinProc As LongPublic Modifiers As Long, uVirtKey1 As Long, idHotKey As Long

Private Type taLong     ll As LongEnd Type

Private Type t2Int    lWord As Integer    hword As IntegerEnd Type    Public Function wndproc(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As LongDim 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 = Not Form1.Visible            End If        End If    End If    '如果不是热键信息则调用原来的程序    wndproc = CallWindowProc(preWinProc, hWnd, Msg, wParam, lParam)End Function

<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值