iewanna iewanna
关注数: 40 粉丝数: 77 发帖数: 1,159 关注贴吧数: 29
VB入门技巧N例 VB入门技巧N例 1. 如何消除textbox中按下回车时的beep声? Private Sub Text1_KeyPress(KeyAscii As Integer) If KeyAscii = 13 Then KeyAscii = 0 End If End Sub 2.Textbox获得焦点时自动选中。 Private Sub Text1_GotFocus() Text1.SelStart = 0 Text1.SelLength = Len(Text1.Text) End Sub 3.屏蔽textbox控件自身的右键菜单,并显示自己的菜单。 方法一: Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y _ As Single) If Button = 2 Then Text1.Enabled = False Text1.Enabled = True PopupMenu mymenu End If End Sub 方法二:回调函数 module: Option Explicit Public OldWindowProc As Long ' 保存默认的窗口函数的地址 Public Const WM_CONTEXTMENU = &H7B ' 当右击文本框时,产生这条消息 Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd _ As Long, ByVal nIndex As Long) As Long Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd _ As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long Private 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 Public Function SubClass_WndMessage(ByVal hWnd As Long, ByVal Msg As Long, ByVal wp _ As Long, ByVal lp As Long) As Long ' 如果消息不是WM_CONTEXTMENU,就调用默认的窗口函数处理 If Msg <> WM_CONTEXTMENU Then SubClass_WndMessage = CallWindowProc(OldWindowProc, hWnd, Msg, wp, lp) Exit Function End If SubClass_WndMessage = True End Function 窗体中: Private Const GWL_WNDPROC = (-4) Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y _ As Single) If Button = 1 Then Exit Sub oldWindowProc = GetWindowLong(Text1.hWnd, GWL_WNDPROC) ' 取得窗口函数的地址 ' 用SubClass_WndMessage代替窗口函数处理消息 Call SetWindowLong(Text1.hWnd, GWL_WNDPROC, AddressOf SubClass_WndMessage) End Sub Private Sub Text1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) If Button = 1 Then Exit Sub ' 恢复窗口的默认函数 Call SetWindowLong(Text1.hWnd, GWL_WNDPROC, OldWindowProc) PopupMenu mymenu End Sub 4. 设置TEXTBOX为只读属性 Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd _ As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd _ As Long, ByVal nIndex As Long) As Long Private Const GWL_STYLE = (-16) Private Const EM_SETREADONLY = &HCF Private Sub Command1_Click() Dim l As Long If (GetWindowLong(Text1.hwnd, GWL_STYLE) And &H800) Then Text1.Text = "This is a read/write text box." '文本窗口是只读窗口,设置为可读写窗口 l = SendMessage(Text1.hwnd, EM_SETREADONLY, False, vbNull) Text1.BackColor = RGB(255, 255, 255) '将背景设置为白色 Command1.Caption = "Read&Write"
1 下一页