Results 1 to 15 of 15

Thread: Classic VB6 - Disabling Ctrl+C and Ctrl+V and Ctrl+X in a textbox

  1. #1

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Classic VB6 - Disabling Ctrl+C and Ctrl+V and Ctrl+X in a textbox

    Hai guys,
    Here is a solution to the common problem of disabling the copy, cut, paste procedures. Many people wants to block this thing for certain purposes. I had came up with some solution after some experiments. I don't whether this is the best solution for that, but I had tested it on a textbox and is working nice

    Hope you all will like this one..

    Code inside the form:
    Code:
    'Everything in here(this form)is made by me, Akhilesh B Chandran
    '---------------------------------------------------------------
    '***************************************************************
    Dim Hooked As Boolean
    
    Private Sub Form_Load()
    Hooked = False 'hooked is used to prevent it from calling the function again and again on each mousemove
    End Sub
    
    Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Hooked = True Then
        UnhookMouse
        Hooked = False
    End If
    End Sub
    
    Private Sub Text1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Hooked = False Then
        HookMouse
        Hooked = True
    End If
    End Sub
    
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii = 22 Then 'paste(ctrl+ v)
        KeyAscii = 0
    End If
    
    If KeyAscii = 3 Then 'copy(ctrl + c)
        KeyAscii = 0
    End If
    
    If KeyAscii = 24 Then 'cut(ctrl + x)
        KeyAscii = 0
    End If
    End Sub
    
    Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = 45 And Shift = 1 Then KeyCode = 0 'paste(shift + insert)
    If KeyCode = 45 And Shift = 2 Then KeyCode = 0 'copy(ctrl + insert)
    End Sub
    Code inside the module:
    Code:
    'This code is from Hack, I had copied this code from another thread, from where he Hack had posted it
    '****************************************************************************************************
    '
    Public Const WH_MOUSE As Long = 7
    
    'API Declarations
    Public Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" _
    (ByVal idHook As Long, _
    ByVal lpfn As Long, _
    ByVal hmod As Long, _
    ByVal dwThreadId As Long) As Long
    
    Public Declare Function UnhookWindowsHookEx Lib "user32" _
    (ByVal hHook As Long) As Long
    
    Public Declare Function CallNextHookEx Lib "user32" _
    (ByVal hHook As Long, _
    ByVal ncode As Long, _
    ByVal wParam As Long, _
    ByVal lParam As Long) As Long
    
    'Global mouse/keyboard function callback handles
    Public g_hMouseHook As Long
    
    Public Sub HookMouse()
    g_hMouseHook = SetWindowsHookEx(WH_MOUSE, AddressOf MouseProc, App.hInstance, App.ThreadID)
    End Sub
    
    Public Sub UnhookMouse()
        If g_hMouseHook Then
            Call UnhookWindowsHookEx(g_hMouseHook)
            g_hMouseHook = 0
        End If
    End Sub
    
    Public Function MouseProc(ByVal idHook As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    
        'Mouse Function Hook...
        If idHook < 0 Then
            MouseProc = CallNextHookEx(g_hMouseHook, idHook, wParam, ByVal lParam)
        Else
           ' Debug.Print wParam
            If wParam = 516 Or wParam = 518 Or wParam = 164 Then 'single or double-click of mouse
                'Debug.Print "Left mouse click disabled..."
                'Setting the return value to -1 cancels Mouse input...
                MouseProc = -1
            Else
                MouseProc = CallNextHookEx(g_hMouseHook, idHook, wParam, ByVal lParam)
            End If
        End If
    
    End Function
    I had made some changes...
    Please give your comments and suggestions..

    -Best wishes to all
    Akhilesh
    Attached Files Attached Files
    Last edited by akhileshbc; Sep 12th, 2008 at 05:44 AM. Reason: made more changes:)

  2. #2
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Classic VB6 - Disabling Ctrl+C and Ctrl+V and Ctrl+X in a textbox

    The best way to disable these features is to subclass the textbox.

    Otherwise, someone can still right click and do the same things from the context menu. Or someone can use Ctrl+Insert to copy and Shift+Insert to paste.


    Has someone helped you? Then you can Rate their helpful post.

  3. #3

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Classic VB6 - Disabling Ctrl+C and Ctrl+V and Ctrl+X in a textbox

    Manavo11, i had made some changes. Thanks 4 ur suggestion

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  4. #4
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Classic VB6 - Disabling Ctrl+C and Ctrl+V and Ctrl+X in a textbox

    Now, if you disable the single and double click, does it even get focus? (I haven't tried, I'm just randomly commenting)

    Buuuuuut, since you are subclassing, why not just disable the cut, copy and paste messages instead of disabling the clicking and risking my previous assumption to be true?


    Has someone helped you? Then you can Rate their helpful post.

  5. #5

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Classic VB6 - Disabling Ctrl+C and Ctrl+V and Ctrl+X in a textbox

    Then can u post the code

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  6. #6
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Classic VB6 - Disabling Ctrl+C and Ctrl+V and Ctrl+X in a textbox

    I don't have VB installed at the moment on this pc, but it's the same code as yours (assuming it works, I haven't tried it), only instead of catching the messages you have put, you catch these ones:

    vb Code:
    1. 'Declare up top in the module
    2. Const WM_CUT As Long = &H300
    3. Const WM_COPY As Long = &H301
    4. Const WM_PASTE As Long = &H302

    and the checking line will be:

    vb Code:
    1. If wParam = WM_CUT Or wParam = WM_COPY Or wParam = WM_PASTE Then

    I might have slight mistakes, but you get the idea


    Has someone helped you? Then you can Rate their helpful post.

  7. #7
    Lively Member
    Join Date
    Aug 2008
    Location
    Between the keyboard and the chair.
    Posts
    122

    Re: Classic VB6 - Disabling Ctrl+C and Ctrl+V and Ctrl+X in a textbox

    ORRRRRR....
    You could Lock the textbox on MouseDown event, afterchecking that the button was vbRightButton, and unlock it on MouseUp event.
    For Shift or Ctrl combinations, in KeyDown, check if Shift is vbShiftMask or vbCtrlMask, and if so, set shift = 0. No need for messy subclassing then.
    If I have helped you out, be a pal and rate the helpful post!

    My CodeBank Submissions:
    Convert Color Values Between Long, RGB and Hex. Updated Sept 25th!
    Update Checker FrameWork

    My WIP Projects:
    Winsock Chat Server/Client Updated Oct 5th!

    Useful Tools:
    Aivosto.com MZ-Tools SysInternals Suite

  8. #8
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Classic VB6 - Disabling Ctrl+C and Ctrl+V and Ctrl+X in a textbox

    It's not messy! APIs are fun

    And in theory it's the most "correct" way to do it, not some work around


    Has someone helped you? Then you can Rate their helpful post.

  9. #9

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Classic VB6 - Disabling Ctrl+C and Ctrl+V and Ctrl+X in a textbox

    Manavo, can u post an attachment of that subclassing method.

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  10. #10
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Classic VB6 - Disabling Ctrl+C and Ctrl+V and Ctrl+X in a textbox

    I first have to get a hold of a copy of VB6 since I recently moved and forgot to pack it

    Or unless someone can find me a link for that free version that you can't compile? Can't remember what they call it...


    Has someone helped you? Then you can Rate their helpful post.

  11. #11
    Lively Member
    Join Date
    Aug 2008
    Location
    Between the keyboard and the chair.
    Posts
    122

    Re: Classic VB6 - Disabling Ctrl+C and Ctrl+V and Ctrl+X in a textbox

    Quote Originally Posted by manavo11
    It's not messy! APIs are fun

    And in theory it's the most "correct" way to do it, not some work around
    I agree with you, on all points, yet to a beginner wanting a quick fix, (read, workaround as you put it) mine should do the trick very well. Besides, doesn't subclassing crash the IDE when stopping the project?
    (Mind you, there is code out there to avoid that.)
    If I have helped you out, be a pal and rate the helpful post!

    My CodeBank Submissions:
    Convert Color Values Between Long, RGB and Hex. Updated Sept 25th!
    Update Checker FrameWork

    My WIP Projects:
    Winsock Chat Server/Client Updated Oct 5th!

    Useful Tools:
    Aivosto.com MZ-Tools SysInternals Suite

  12. #12
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Classic VB6 - Disabling Ctrl+C and Ctrl+V and Ctrl+X in a textbox

    OK, here goes:

    vb Code:
    1. 'Module code
    2. Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" _
    3.                                        (ByVal lpPrevWndFunc As Long, _
    4.                                                  ByVal hWnd As Long, _
    5.                                                   ByVal Msg As Long, _
    6.                                                ByVal wParam As Long, _
    7.                                          ByVal lParam As Long) As Long
    8.  
    9. Declare Function SetWindowLong Lib "user32" _
    10.                      Alias "SetWindowLongA" _
    11.                        (ByVal hWnd As Long, _
    12.                       ByVal nIndex As Long, _
    13.              ByVal dwNewLong As Long) As Long
    14.  
    15. Public Const GWL_WNDPROC = -4
    16.  
    17. Public Const WM_CUT As Long = &H300
    18. Public Const WM_COPY As Long = &H301
    19. Public Const WM_PASTE As Long = &H302
    20.  
    21. Public lpPrevWndProc As Long
    22. Private lnghWnd As Long
    23.  
    24. Public Sub Hook(hWnd As Long)
    25.     lnghWnd = hWnd
    26.     lpPrevWndProc = SetWindowLong(lnghWnd, GWL_WNDPROC, AddressOf WindowProc)
    27. End Sub
    28.  
    29. Public Sub UnHook()
    30.     Dim lngReturnValue As Long
    31.     lngReturnValue = SetWindowLong(lnghWnd, GWL_WNDPROC, lpPrevWndProc)
    32. End Sub
    33.  
    34. Function WindowProc(ByVal hw As Long, _
    35.                     ByVal uMsg As Long, _
    36.                     ByVal wParam As Long, _
    37.                     ByVal lParam As Long) As Long
    38.  
    39.     Select Case uMsg
    40.         Case WM_CUT, WM_COPY, WM_PASTE
    41.             Exit Function
    42.         Case WM_DESTROY
    43.             UnHook
    44.     End Select
    45.    
    46.     WindowProc = CallWindowProc(lpPrevWndProc, lnghWnd, uMsg, wParam, lParam)
    47. End Function

    vb Code:
    1. 'form code
    2.  
    3. Option Explicit
    4.  
    5. Private Sub Form_Load()
    6.     Hook Text1.hWnd
    7. End Sub
    8.  
    9. Private Sub Form_Unload(Cancel As Integer)
    10.     UnHook
    11. End Sub

    And in this example, VB doesn't hang when you hit the stop button instead of closing the form normally


    Has someone helped you? Then you can Rate their helpful post.

  13. #13
    Lively Member
    Join Date
    Aug 2008
    Location
    Between the keyboard and the chair.
    Posts
    122

    Re: Classic VB6 - Disabling Ctrl+C and Ctrl+V and Ctrl+X in a textbox

    Quote Originally Posted by manavo11
    And in this example, VB doesn't hang when you hit the stop button instead of closing the form normally
    Is this due to these lines of code?...
    Code:
    ...
            Case WM_DESTROY
                UnHook
    ...
    If I have helped you out, be a pal and rate the helpful post!

    My CodeBank Submissions:
    Convert Color Values Between Long, RGB and Hex. Updated Sept 25th!
    Update Checker FrameWork

    My WIP Projects:
    Winsock Chat Server/Client Updated Oct 5th!

    Useful Tools:
    Aivosto.com MZ-Tools SysInternals Suite

  14. #14
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Classic VB6 - Disabling Ctrl+C and Ctrl+V and Ctrl+X in a textbox

    I tried it without those lines as well and it still didn't hang. Can't remember now in which cases it does hang and in which it doesn't (I also seem to remember it hanging occasionally)

    But it doesn't hurt to add that line as well. If you forget to add the unhook calling in the form unload, that line will save a crash (I think)


    Has someone helped you? Then you can Rate their helpful post.

  15. #15
    Lively Member
    Join Date
    Aug 2008
    Location
    Between the keyboard and the chair.
    Posts
    122

    Re: Classic VB6 - Disabling Ctrl+C and Ctrl+V and Ctrl+X in a textbox

    You just earned credits in my program source code :P I'll be using it in the Color converter in my sig.
    I actually have one ammendment for that, for better module usage... instead of exiting function, have it call an event, returning an enum of the event called. This way, you can deal with the data if needed. ( I think)
    Edit: check that, this would need to be in a class, and have a callback to do what I mentioned.
    Edit: Edit: Ok I got it to crash, while trying to Hook multiple objects. I would think using a collection or array of a usertype would probably get around this...maybe.
    Last edited by EntityReborn; Sep 24th, 2008 at 07:26 PM.
    If I have helped you out, be a pal and rate the helpful post!

    My CodeBank Submissions:
    Convert Color Values Between Long, RGB and Hex. Updated Sept 25th!
    Update Checker FrameWork

    My WIP Projects:
    Winsock Chat Server/Client Updated Oct 5th!

    Useful Tools:
    Aivosto.com MZ-Tools SysInternals Suite

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