| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 615 人关注过本帖
标题:怎么设置两个全局热键
只看楼主 加入收藏
香蕉0
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2010-10-22
结帖率:100%
收藏
 问题点数:0 回复次数:1 
怎么设置两个全局热键
我用以下这段代码来控制form1的显示和隐藏,想要再添加另外一个热键变量,来控制form2的关闭,要怎么添加

模块
Option Explicit

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

Public Const WM_HOTKEY = &H312
Public Const MOD_ALT = &H1
Public Const MOD_CONTROL = &H2
Public Const MOD_SHIFT = &H4
Public Const GWL_WNDPROC = (-4)

Public preWinProc As Long
Public Modifiers As Long, uVirtKey As Long, idHotKey As Long

Private Type taLong
    ll As Long
End Type

Private Type t2Int
    lWord As Integer
    hWord As Integer
End Type

Public Function Wndproc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    If Msg = WM_HOTKEY Then
        If wParam = idHotKey Then
            Dim lp As taLong, i2 As t2Int
            lp.ll = lParam
            LSet i2 = lp
            If (i2.lWord = Modifiers) And i2.hWord = uVirtKey Then
               If Form1.Visible Then
                    Form1.Hide
                       Else
                    Form1.Show
               End If
            End If
        End If
    End If
    Wndproc = CallWindowProc(preWinProc, hwnd, Msg, wParam, lParam)
End Function
窗体
Private Sub Form_Load()
    Dim ret As Long
    preWinProc = GetWindowLong(Me.hwnd, GWL_WNDPROC)
    ret = SetWindowLong(Me.hwnd, GWL_WNDPROC, AddressOf Wndproc)
    idHotKey = 1
    Modifiers = MOD_ALT
    uVirtKey = vbKeyQ
    ret = RegisterHotKey(Me.hwnd, idHotKey, Modifiers, uVirtKey)
End Sub

搜索更多相关主题的帖子: 热键 全局 
2010-10-22 20:41
bczgvip
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:贵宾
威 望:66
帖 子:1310
专家分:5312
注 册:2009-2-26
收藏
得分:0 
Private Sub Form_Load()
    Dim ret As Long
    preWinProc = GetWindowLong(Me.hwnd, GWL_WNDPROC)
    ret = SetWindowLong(Me.hwnd, GWL_WNDPROC, AddressOf Wndproc)
    'idHotKey = 1
    'Modifiers = MOD_ALT
    'uVirtKey = vbKeyQ
    ret = RegisterHotKey(Me.hwnd, 1, MOD_ALT, vbKeyQ)
    ret = RegisterHotKey(Me.hwnd, 2, MOD_ALT, vbKeyX)
End Sub

Public Function Wndproc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    If Msg = WM_HOTKEY Then
        'Dim lp As taLong, i2 As t2Int
        'lp.ll = lParam
        'LSet i2 = lp
        select case wParam
        case 1
               If Form1.Visible Then
                    Form1.Hide
                       Else
                    Form1.Show
               End If
        case 2
               unload Form1
        end select
    End If
    Wndproc = CallWindowProc(preWinProc, hwnd, Msg, wParam, lParam)
End Function
2010-10-23 04:22
快速回复:怎么设置两个全局热键
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.089682 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved