Results 1 to 15 of 15

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

Threaded View

  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:)

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