VBGood网站全文搜索 Google

搜索VBGood全站网页(全文搜索)
首页 - 经验之谈 - "键盘"编程技巧集锦
发表评论(0)作者:影子, 平台:VB6.0+Win98, 阅读:11690, 日期:2001-01-01
"键盘"编程技巧集锦
作者 影子
一、引言
在我们用 VB 编程时,其中有许多的编程技巧涉及到了"键盘",下面我把涉及"键盘"编程都列出来,希望对广大的 VB 爱好者有帮助。
二、详细的介绍:
说明:当你编一数据库的输入程序时,当你在 text1 输入完数据后,按回车键,跳到 text2 (这比按 TAB 键更方便)
(1)与文本框有关的键盘操作。
例1:(使文本框有回车功能)
Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then Text2.SetFocus
End Sub

说明:当你在 text1 按回车键,就相关当于按了 Shift+End 键,是不是很酷呀,这就是键值转换呀。
例2:(转换键值)
Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then
SendKeys "+{END}"
End If
End Sub

说明:当你如果在 text1 中输入的不是数字(0-9),则抑制键入。(这个操作非常有用)
例3:(限制键盘的输入)
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0
End Sub

说明:当你使用例1的技巧后,是不是觉得每按一个回车都有产生“嘀”声不好呀,用这个技巧就能解决了。

例4:(避免按 [ENTER] 键产生的“嘀”声 )
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = Asc(vbCr) Then
KeyAscii = 0
End If
End Sub

说明:有时在你输入数据时,非要注意大小写,那么这两个技巧对你就有用了。
例5:(自动转换成小写)
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii >= 65 And KeyAscii <= 90 Then
KeyAscii = KeyAscii + 32
End If
End Sub

例6:(自动转换成大写)
Private Sub Text2_KeyPress(KeyAscii As Integer)
If KeyAscii >= 97 And KeyAscii <= 122 Then
KeyAscii = KeyAscii - 32
End If
End Sub

(2)窗口操作
说明:当然你编了一个不想让别人关闭的程序时,这个技巧就起作用了。
例1:(禁止使用 Alt+F4 关闭窗口)
Option Explicit
Private Declare Function DeleteMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long
Private Declare Function GetMenuItemCount Lib "user32" (ByVal hMenu As Long) As Long
Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long
Private Const MF_BYPOSITION = &H400&
Private Sub Form_Load()
Dim hwndMenu As Long
Dim c As Long
hwndMenu = GetSystemMenu(Me.hwnd, 0)
c = GetMenuItemCount(hwndMenu)
DeleteMenu hwndMenu, c - 1, MF_BYPOSITION
c = GetMenuItemCount(hwndMenu)
DeleteMenu hwndMenu, c - 1, MF_BYPOSITION
End Sub

(3)全局的键盘操作
说明:什么你不想让别人用这起个组合键,没有问题,用个技巧就能搞定。
例1:(拦截″Ctrl+Alt+Del″,″Ctrl+Esc″,″Alt+Tab″三组热键)
Option Explicit
Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Long, ByVal uParam As Long, ByVal lpvParam As Any, ByVal fuWinIni As Long) As Long
Const SPL_SCREENSAVERRUNNING = 97
注释:读取窗体时使热键失效
    Private Sub Form_Load()
    SystemParametersInfo SPL_SCREENSAVERRUNNING, True, ByVal 1&, 0
    End Sub
注释:程序退出时使热键有效
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    SystemParametersInfo SPL_SCREENSAVERRUNNING, False, ByVal 1&, 0
    End Sub

说明:我想在程序中锁定键盘怎么办,好办看看吧,就是这。
例2:(让Window不接受键盘输入)
Private Declare Function EnableWindow Lib "user32" (ByVal hwnd As Long, ByVal fEnable As Long) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub Command1_Click()
Call EnableWindow(Me.hwnd, 0)
Me.Caption = "现在拒绝KeyPress, MouseClick"
Dim i As Long
For i = 1 To 100
Call Sleep(100)
DoEvents 注释:虽有DoEvents,会发现,按Form的任何地方都没有反应
Next i
Me.Caption = "现在解除了"
Call EnableWindow(Me.hwnd, 1)
End Sub

说明:在软件中设定热键可是非常酷的事情,看着吧,这是这样。
例3:(在软件中设定热键)
Option Explicit
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Long) As Integer
Private Function MyHotKey(vKeyCode) As Boolean
MyHotKey = (GetAsyncKeyState(vKeyCode) < 0)
End Function
Private Sub Timer1_Timer()
If MyHotKey(vbKeyA) Then End 注释:如果热键为A,则退出程序,你可在这里设置自己的操作。
End Sub

     我以把几种常用的"键盘"编程的技巧介绍了,由于篇幅问题,不能详细的介绍相关的 API 函数,及其在实际编程的应用,如果你想要下载相关"键盘"编程的源程序,可到"VB 爱好者乐园 http://www.d1vb.com"来看看!!