Results 1 to 3 of 3

Thread: Miscellaneous Textbox Functions

  1. #1

    Thread Starter
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527

    Miscellaneous Textbox Functions

    Suppressing default popup menu on a text box

    VB Code:
    1. Private Declare Function LockWindowUpdate Lib "user32" _
    2. (ByVal hwndLock As Long) As Long
    3.  
    4. Private Sub TextBox_MouseDown(Button As Integer, Shift As Integer, _
    5. X As Single, Y As Single)
    6.     If Button = vbRightButton Then
    7.         ' Avoid the 'disabled' gray text by locking updates
    8.         LockWindowUpdate TextBox.hwnd
    9.        
    10.         ' A disabled TextBox will not display a context menu
    11.         TextBox.Enabled = False
    12.        
    13.         ' Give the previous line time to complete
    14.         DoEvents
    15.        
    16.         ' Display our own context menu
    17.         PopupMenu mnuPopup
    18.        
    19.         ' Enable the control again
    20.         TextBox.Enabled = True
    21.        
    22.         ' Unlock updates
    23.         LockWindowUpdate 0&
    24.     End If
    25. End Sub

  2. #2

    Thread Starter
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    Disabling context menu

    In a module:
    VB Code:
    1. Declare Function SetWindowLong& Lib "user32" _
    2. Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As _
    3. Long, ByVal dwNewLong As Long)
    4.  
    5. Declare Function CallWindowProc Lib "user32" _
    6. Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal _
    7. hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal _
    8. lParam As Long) As Long
    9.  
    10. Const GWL_WNDPROC = (-4)
    11. Const WM_CONTEXT = &H7B
    12.  
    13. Private WndProcOld As Long
    14.  
    15. Public Function WindProc(ByVal hwnd As Long, ByVal wMsg As _
    16. Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    17.     If wMsg = WM_CONTEXT Then Exit Function
    18.     WindProc = CallWindowProc(WndProcOld&, hwnd&, wMsg&, _
    19.     wParam&, lParam&[b][/b])
    20. End Function
    21.  
    22. Sub SubClassWnd(hwnd As Long)
    23.     WndProcOld& = SetWindowLong(hwnd, GWL_WNDPROC, _
    24.     AddressOf WindProc)
    25. End Sub
    26.  
    27. Sub UnSubclassWnd(hwnd As Long)
    28.     SetWindowLong hwnd, GWL_WNDPROC, WndProcOld&
    29.     WndProcOld& = 0
    30. End Sub

    In a form:
    VB Code:
    1. Private Sub Form_Load()
    2.     SubClassWnd TextBox.hwnd
    3. End Sub
    4.  
    5. Private Sub Form_Unload(Cancel As Integer)
    6.     UnSubclassWnd TextBox.hwnd
    7. End Sub

    This doesn't affect the right click message in any way.
    Last edited by da_silvy; Sep 22nd, 2003 at 02:47 AM.

  3. #3

    Thread Starter
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    Disabling copy, cut and paste in a textbox

    In a module:
    VB Code:
    1. Declare Function SetWindowLong& Lib "user32" _
    2. Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As _
    3. Long, ByVal dwNewLong As Long)
    4.  
    5. Declare Function CallWindowProc Lib "user32" _
    6. Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal _
    7. hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal _
    8. lParam As Long) As Long
    9.  
    10. Const GWL_WNDPROC = (-4)
    11.  
    12. Const WM_COPY = &H301
    13. Const WM_PASTE = &H302
    14. Const WM_CUT = &H300
    15.  
    16. Private WndProcOld As Long
    17.  
    18. Public Function WindProc(ByVal hwnd As Long, ByVal wMsg As _
    19. Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    20.     If wMsg = WM_COPY Or wMsg = WM_PASTE Or wMsg = WM_CUT Then Exit Function
    21.     WindProc = CallWindowProc(WndProcOld&, hwnd&, wMsg&, _
    22.     wParam&, lParam&)
    23. End Function
    24.  
    25. Sub SubClassWnd(hwnd As Long)
    26.     WndProcOld& = SetWindowLong(hwnd, GWL_WNDPROC, _
    27.     AddressOf WindProc)
    28. End Sub
    29.  
    30. Sub UnSubclassWnd(hwnd As Long)
    31.     SetWindowLong hwnd, GWL_WNDPROC, WndProcOld&
    32.     WndProcOld& = 0
    33. End Sub

    In a form:
    VB Code:
    1. Private Sub Form_Load()
    2.     SubClassWnd TextBox.hwnd
    3. End Sub
    4.  
    5. Private Sub Form_Unload(Cancel As Integer)
    6.     UnSubclassWnd TextBox.hwnd
    7. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width