Results 1 to 5 of 5

Thread: VB - Intercepting Paste Command on RichTextBoxes

Threaded View

  1. #1

    Thread Starter
    Fanatic Member RealisticGraphics's Avatar
    Join Date
    Jul 1999
    Location
    Arkansas
    Posts
    655

    VB - Intercepting Paste Command on RichTextBoxes

    This code will show you how to intercept the Paste command that is sent to the RichTextBox control (RTB).

    Now you may ask why you would need this... If you create a menu for Pasting and give it a shortcut key of Ctrl+V. Then when you paste to a RTB you get double whatever you had on the clipboard if you used Ctrl+V. The data on the clipboard will also paste as whatever Format it is in instead of plain text (in my case, I needed it to always be in plain text).

    As I discovered, the RTB does NOT except the WM_PASTE message (as well as WM_COPY & WM_CUT). After hours of trying to figure out what message it was receiving, I came up with the code below.

    Module:
    VB Code:
    1. Public Declare Function CallWindowProc Lib "user32" _
    2. Alias "CallWindowProcA" _
    3. (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, _
    4. ByVal msg As Long, ByVal wParam As Long, _
    5. ByVal lParam As Long) As Long
    6.  
    7. Public Declare Function SetWindowLong Lib "user32" Alias _
    8. "SetWindowLongA" (ByVal hwnd As Long, _
    9. ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    10.  
    11. Public lOldWinProc As Long
    12.  
    13. Public Const GWL_WNDPROC& = (-4)
    14. Private Const WM_KEYDOWN = &H100
    15. Private Const WM_PASTE = &H302
    16.  
    17. Public Function WndProc(ByVal hwnd As Long, _
    18. ByVal uMsg As Long, ByVal wParam As Long, _
    19. ByVal lParam As Long) As Long
    20.  
    21. Dim bCompleted As Boolean
    22. bCompleted = False
    23.  
    24. Select Case uMsg
    25.     Case WM_KEYDOWN
    26.         Select Case wParam
    27.             Case vbKeyV
    28.                 bCompleted = True
    29.             Case vbKeyC
    30.                 bCompleted = True
    31.             Case vbKeyX
    32.                 bCompleted = True
    33.         End Select
    34.     Case WM_PASTE
    35.         MyPaste
    36.         bCompleted = True
    37. End Select
    38.  
    39. If bCompleted = False Then
    40.     WndProc = CallWindowProc(lOldWinProc, hwnd, uMsg, wParam, lParam)
    41. End If
    42. End Function
    43.  
    44. Public Sub MyCopy()
    45. Clipboard.SetText Form1.RTB.SelText, vbCFText
    46. End Sub
    47.  
    48. Public Sub MyCut()
    49. Clipboard.SetText Form1.RTB.SelText, vbCFText
    50. Form1.RTB.SelText = ""
    51. End Sub
    52.  
    53. Public Sub MyPaste()
    54. Form1.RTB.SelText = Clipboard.GetText(vbCFText)
    55. End Sub

    As you can see I've created my own Cut, Copy, & Paste procedures to take over for the normal ones. In the procedure I'm using to intercept the commands you'll see WM_KEYDOWN. What this message tells us is that the Control key is pressed. If it is pressed, we then check wParam. wParam is the KeyCode for the key that is being pressed along with the Control key.

    If we determine that Ctrl+C, Ctrl+X, or Ctrl+V is being pressed them we set bCompleted = True so that the commands will not be given to the RTB and our new commands will.

    In a Form named Form1 with an RTB named RTB:
    VB Code:
    1. Private Sub Form_Load()
    2. lOldWinProc = SetWindowLong(RTB.hwnd, GWL_WNDPROC, AddressOf WndProc)
    3. End Sub
    4.  
    5. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    6. SetWindowLong RTB.hwnd, GWL_WNDPROC, lOldWinProc
    7. End Sub
    8.  
    9. Private Sub mnuCopy_Click()
    10. MyCopy
    11. End Sub
    12.  
    13. Private Sub mnuCut_Click()
    14. MyCut
    15. End Sub
    16.  
    17. Private Sub mnuPaste_Click()
    18. MyPaste
    19. End Sub

    In the Form_Load and Form_QueryUnload event we Hook/Unhook the RTB. This just tells the RTB to use our new procedure instead of the default one first. The menu code should be pretty straight forward.

    I've attached a copy of a project that shows this code in action.
    Attached Files Attached Files
    Last edited by RealisticGraphics; Oct 5th, 2003 at 09:39 AM.
    www.RealisticGraphics.net

    Running VS.Net Enterprise & VB 6

    Other Languages: JavaScript, VBScript, VBA, HTML, CSS, ASP, SQL, XML

    MSN Messenger: kmsheff

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