原始代码:
控件KeyPress事件(代码):
If KeyAscii > 64 And KeyAscii < 91 Then KeyAscii = KeyAscii + 32
BUG:先复制大写字符,粘贴到控件中后不会完全小写
修复代码:
Option Explicit
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, _
ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, _
ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Const GWL_STYLE = (-16)
Private Const ES_UPPERCASE = &H8& '大写
Private Const ES_LOWERCASE = &H10& '小写
Private Const ES_NUMBER = &H2000& '全部
Private Const ES_All = &H0& '全部可以输入
Private Function SetModel(tBox As TextBox, InputStyle As Long)
Dim DefaultStyle As Long, new_style As Long
DefaultStyle = GetWindowLong(tBox.hwnd, GWL_STYLE)
DefaultStyle = DefaultStyle And (Not ES_NUMBER) And Not ES_LOWERCASE And Not ES_UPPERCASE
If InputStyle <> ES_All Then DefaultStyle = DefaultStyle Or InputStyle
new_style = SetWindowLong(tBox.hwnd, GWL_STYLE, DefaultStyle)
tBox.Refresh
End Function
控件Change事件
SetModel Text1, ES_UPPERCASE
SetModel Text1, ES_LOWERCASE
SetModel Text1, ES_NUMBER
SetModel Text1, ES_All
BUG说明:
用户只是在程序中输入字符(KEYPRESS事件)才会被小写,因此。右键"粘贴"事件没有响应KEYPRESS事件!
文章来源:http://www.hulian.top,转载请注明!