This article describes how to implement functions such as right-click menu, copy, and paste in a VB shielded text box. This function is to disable the right-click function of the text box, causing the right-click function of the mouse to be invalid. It is a very common type of practical function.
The specific function module code is as follows:
'================================================== ========='| Module name| TextBoxDisableAbility'| Description| Disable the function of the text box'========================== =================================Option ExplicitPrivate Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As LongPrivate 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 LongPrivate Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As LongPrivate Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As LongPrivate Const GWL_WNDPROC = (- 4)Private Const WM_CUT = &H300 '-------------Cut message Private Const WM_COPY As Long = &H301 '-------------Copy message Private Const WM_PASTE As Long = &H302 '-- -----------Paste message Private Const WM_CLEAR = &H303 '-------------Delete message [Delete from right-click menu]Private Const EM_UNDO = &HC7 '---- ---------Cancel messagePrivate Const WM_CONTEXTMENU = &H7B '-------------Right-click menuPrivate prevWndProc As LongPrivate Function WndProc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Select Case Msg Case WM_CUT , WM_COPY, WM_PASTE, WM_CLEAR, EM_UNDO, WM_CONTEXTMENU 'Handle custom events here, preferably empty Case Else 'Callback system function processing WndProc = CallWindowProc(prevWndProc, hwnd, Msg, wParam, lParam) End SelectEnd FunctionPublic Sub DisableAbility(TargetTextBox As TextBox) 'Call this when the program starts prevWndProc = GetWindowLong(TargetTextBox.hwnd, GWL_WNDPROC) SetWindowLong TargetTextBox.hwnd, GWL_WNDPROC, AddressOf WndProcEnd Sub
The function of this code blocks the right-click menu, copy, paste, etc. of the text box. These functions can be seen in many programs. Sharing it with all VB enthusiasts, I hope it will be helpful to everyone!