本文實例講述了VB屏蔽文字方塊中的右鍵選單、複製、貼上等功能的實作方法。此功能就是禁止文字方塊的右鍵功能,造成點擊滑鼠右鍵無效。是非常常見的一類實用功能。
具體的功能模組程式碼如下:
'================================================== ========='| 模組名稱| TextBoxDisableAbility'| 說明| 禁止文字方塊的功能'========================= =================================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, ByValVallnd As Longdow 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 Longnd As Longdw, nIndex* Const GWL_WNDPROC = (-4)Private Const WM_CUT = &H300 '-------------剪下訊息Private Const WM_COPY As Long = &H301 '-------------複製訊息Private Const WM_PASTE As Long = &H302 '-------------貼上訊息Private Const WM_CLEAR = &H303 '-------------刪除訊息[右鍵選單的刪除]Private Const EM_UNDO = &HC7 '-------------撤銷訊息Private Const WM_CONTEXTMENU = &H7B '-------------右鍵選單Private 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 '這裡處理自訂的事件,最好為空Case Else '回呼系統函數處理WndProc = CallWindowProc(prevWndProc, hwnd, Msg, wParam, lParam) End SelectEnd FunctionPublic Sub DisableAbility(TargetTextBox As TextBox) '程式啟動時呼叫這個prevWndProc = GetWindowLong(TargetTextBox.hwnd, GWL_WNDPROC) SetWindowLong TargetTextBox.hwnd, GWL_WNDPROC, AddressOf WndProcEnd Sub
本程式碼的功能屏蔽文字方塊的右鍵選單、複製、貼上等,在不少的程式中都可以看到這些功能。共享給各位VB愛好者,希望能對大家有幫助!