vb按钮如何设置快捷键,

vb按钮如何设置快捷键,,第1张

设置VB窗体按扭快捷键的 *** 作可以采用下面两种方法来实现:

1、在按扭的Capion 属性设置标题的时候,前面增加 "&K"字符就可以,K代表的是你准备使用的Alt+键盘字符,如“&O.退出”,程序运行的时候按下 Alt+O 即执行该按扭的 *** 作了

2、你可以利用该按扭所在的Form 窗体优先权获得键盘 *** 作功能来激发你要完成的按扭 *** 作,此时首先要设置Form 的KeyPreview 属性值为 True,然后在 Form中KeyDown事件中判断你需要的组合键盘值来激发你的按扭事件.

Form_KeyDown事件中的各功能键值组合比较全面,如Shift,Alt 及Ctr等,一般都能满足快捷键的组合需求了.

Exp:

利用Ctrl+O组合键激发上面所说的按扭键,在Form_KeyDown事件中添加如下代码

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

If Shift = 2 And KeyCode = 79 Then

btnQuit_Click()

End If

End Sub

btnQuit_Click()是退出程序按扭事件

使用API函数即可

代码如下:

窗体:

Option Explicit

Sub Form_Load()

Me.Hide

Dim ret As Long

preWinProc = GetWindowLong(Me.hwnd, GWL_WNDPROC)

ret = SetWindowLong(Me.hwnd, GWL_WNDPROC, AddressOf wndproc)

idHotKey = 1 'in the range &h0000 through &hBFFF

Modifiers = MOD_ALT

uVirtKey = vbKeyZ

ret = RegisterHotKey(Me.hwnd, idHotKey, Modifiers, uVirtKey)

End Sub

Private Sub Form_Unload(Cancel As Integer)

Dim ret As Long

'取消Message的截取,而使之又只送往原来的Window Procedure

ret = SetWindowLong(Me.hwnd, GWL_WNDPROC, preWinProc)

Call UnregisterHotKey(Me.hwnd, uVirtKey)

End Sub

模块:

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


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11073941.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-13
下一篇 2023-05-13

发表评论

登录后才能评论

评论列表(0条)

保存