用API函数定义热键又一法

2008-02-23 06:54:01来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

土人的拙作《如何用API函数定义热键》在 VB编程乐园 发表后,很多网友纷纷来信,有的指出其中存在的问题并提出解决方的案(土人在此表示感谢!);有的则询问可不可以定义组合键等问题。土人不才,通过多方查找资料,才找到另一种定义热键的方法,在此把心得写出来,请各位网友指正!
三个API函数是必须的:
ReGISterHotKey —— 用于定义热键
UnregisterHotKey —— 用于注销热键
PeekMessage —— 用于检测热键(尚有其它的主要用途)
这里举个实例:程序启动后窗体不可见,按下 Ctrl F12 窗体弹出来。本例仅需要一个窗体,在 PWin98、VB6.0企业版下调试通过。

'申明 API 函数
Private Declare Function RegisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
Private Declare Function UnregisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal id As Long) As Long
Private Declare Function PeekMessage Lib "user32" Alias "PeekMessageA" (lpMsg As Msg, ByVal hWnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long, ByVal wRemoveMsg As Long) As Long

Private bCancel As Boolean '用于热键有效与否的变量

'全局变量
Private Const MOD_ALT = &H1
Private Const MOD_CONTROL = &H2
Private Const MOD_SHIFT = &H4
Private Const PM_REMOVE = &H1
Private Const WM_HOTKEY = &H312

Private Type Msg '定义Message类型
hWnd As Long
Message As Long
wParam As Long
lParam As Long
time As Long
End Type

Private Sub proMsg() '声明进程
Dim Message As Msg
' 循环直到 bCancel 为真
Do While Not bCancel
'检测到热键被按下则令窗体隐藏
If PeekMessage(Message, Me.hWnd, WM_HOTKEY, WM_HOTKEY, PM_REMOVE) Then
Me.Visible = True
End If
DoEvents
Loop
End Sub

Private Sub Form_Load() '窗体加载时初始化程序
Dim ret As Long
bCancel = False
'定义热键为Ctrl F12
ret = RegisterHotKey(Me.hWnd, &HBFFF&, MOD_CONTROL, vbKeyF12)
Me.Visible = False '窗体不可见
proMsg
End Sub

'退出时注销热键
Private Sub Form_Unload(Cancel As Integer)
bCancel = True
Call UnregisterHotKey(Me.hWnd, &HBFFF&)
End Sub

上一篇: 用Shell语句调出控制面板
下一篇: 检测磁盘类型的信息

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:在VB里巧用数据类型集合

下一篇:基于VB的通用折行打印程序