Disable Pop Up menu From Textbox

A common Frequently Asked Question is how to disable the popup menu of a TextBox. Since VB5 was released it’s possible to use the AdressOf keyword to add a Hook and a callback function. Just add this code to a .BAS module and call the Hook sub and pass the hWnd of a textbox as an argument. You must call the UnHook sub before you unload the form or you might get a General Protection

pertanyaan yang sering ditanyakan adalah bagaimana men-disable menu popup yang ada pada Textbox, berikut ini adalah contoh program kecil untuk men-disable menu popup tersebut:

buat kode berikut pada deklarasi modul:

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

Declare Function SetWindowLong Lib "user32" _
    Alias "SetWindowLongA" _
    (ByVal hWnd As Long, _
    ByVal nIndex As Long, _
    ByVal dwNewLong As Long) As Long

Public Const GWL_WNDPROC = -4
Public Const WM_RBUTTONUP = &H205
Public lpPrevWndProc As Long
Private lngHWnd As Long

Buat kode berikut pada modul:

Public Sub Hook(hWnd As Long)
    lngHWnd = hWnd
    lpPrevWndProc = SetWindowLong(lngHWnd, GWL_WNDPROC, _
    AddressOf WindowProc)
End Sub

Public Sub UnHook()
    Dim lngReturnValue As Long
    lngReturnValue = SetWindowLong(lngHWnd, GWL_WNDPROC, lpPrevWndProc)
End Sub

Function WindowProc(ByVal hw As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Select Case uMsg
        Case WM_RBUTTONUP
            'Do nothing
            'Or popup you own menu
        Case Else
            WindowProc = CallWindowProc(lpPrevWndProc, hw, uMsg, wParam, lParam)
    End Select
End Function

Buat kode berikut, letakkan pada event Form_Load() dimana Textbox berada:

Call Hook(Text1.hWnd)