vb 事件触发

例如可以实现按空格键触发form_click吗??谢谢!!!... 例如可以实现按空格键触发 form_click 吗?? 谢谢!!! 展开
 我来答
zhenghanxi
2008-05-20 · TA获得超过422个赞
知道小有建树答主
回答量:268
采纳率:0%
帮助的人:257万
展开全部
'如果只在本窗体内触发就简单了,代码如下:

Private Sub Form_Load()
Me.KeyPreview = True
End Sub
Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = 32 Then Form_Click
End Sub
========================================================================
'如果要定义空格热键代码如下:

'以下保存在模块.bas中
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 fskey_Modifiers 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 GWL_WNDPROC = (-4)
Public Const WM_HOTKEY = &H312
Public preWinProc As Long, Modifiers As Long, uVirtKey As Long, idHotKey As Long
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
'在此填入 form_click代码
Form1.Show
MsgBox "此处为 Form_Click 代码。", vbInformation + vbOKOnly, "空格热键"
End If
wndproc = CallWindowProc(preWinProc, hwnd, Msg, wParam, lParam)
End Function

’以下为窗体代码:
Private Sub Form_Load()
preWinProc = GetWindowLong(Me.hwnd, GWL_WNDPROC) '获得窗口过程的地址
SetWindowLong Me.hwnd, GWL_WNDPROC, AddressOf wndproc '为窗口过程设定一个新的地址
idHotKey = 1 '定义热键的标识符
Modifiers = 0 '没有组合键
uVirtKey = vbKeySpace '定义热键的虚拟键码
RegisterHotKey Me.hwnd, idHotKey, Modifiers, uVirtKey '登记热键
End Sub
Private Sub Form_Unload(Cancel As Integer)
SetWindowLong Me.hwnd, GWL_WNDPROC, preWinProc '还原窗口过程地址
UnregisterHotKey Me.hwnd, uVirtKey '释放先前登记的热键
End Sub
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友2a1bae2
2008-05-20 · TA获得超过932个赞
知道小有建树答主
回答量:640
采纳率:0%
帮助的人:270万
展开全部
可以
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式