Solved

programmatically disable paste

Posted on 2014-01-26
5
439 Views
Last Modified: 2014-01-27
Is it possible to disable pasting text on a form or Edit box ? I don't necessarily want to prevent copying by right-click/copy or ctrl-C but I do want to prevent such copied text being pasted into a specific Edit Box. I wish to force the user to type data in the Edit box.
I am using  Borland or Embarcadero C++ Builder and windows Xp/windows 7
0
Comment
Question by:alcindor
  • 3
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 39810678
There are basically two approaches to achieve that. You could either subclass the edit control to not accept pasted text. That means replacing the edit control's window procedure with your own to filter the events and then calling the original one. See the article and sample code at http://msdn.microsoft.com/en-us/library/windows/desktop/ms633570%28v=vs.85%29.aspx ("Using Window Procedures", scroll down to "Subclassing a Window"). The scoop would be to

WNDPROC wpOrigEditProc; 
 
LRESULT APIENTRY EditBoxProc(
    HWND hwndDlg, 
    UINT uMsg, 
    WPARAM wParam, 
    LPARAM lParam) 
{ 
    HWND hwndEdit; 
 
    switch(uMsg) 
    { 
        case WM_INITDIALOG: 
            // Retrieve the handle to the edit control. 
            hwndEdit = GetDlgItem(hwndDlg, ID_EDIT); 
 
            // Subclass the edit control. 
            wpOrigEditProc = (WNDPROC) SetWindowLong(hwndEdit, 
                GWL_WNDPROC, (LONG) EditSubclassProc); 
            // 
            // Continue the initialization procedure. 
            // 
            return TRUE; 
 
        case WM_DESTROY: 
            // Remove the subclass from the edit control. 
            SetWindowLong(hwndEdit, GWL_WNDPROC, 
                (LONG) wpOrigEditProc); 
            // 
            // Continue the cleanup procedure. 
            // 
            break; 
    } 
    return FALSE; 
        UNREFERENCED_PARAMETER(lParam); 
} 
 
// Subclass procedure 
LRESULT APIENTRY EditSubclassProc(
    HWND hwnd, 
    UINT uMsg, 
    WPARAM wParam, 
    LPARAM lParam) 
{ 
    if (uMsg == WM_PASTE) 
        return 0;  // filter 'paste'

    return CallWindowProc(wpOrigEditProc, hwnd, uMsg, 
        wParam, lParam); // dispatch to original WndProc 
} 

Open in new window


The other option would be a hook that basically does the same, see http://msdn.microsoft.com/en-us/library/windows/desktop/ms632589%28v=vs.85%29.aspx ("Hooks").

Yet I'd say subclassing would be the simpler approach.
0
 
LVL 86

Expert Comment

by:jkr
ID: 39810984
Ooops, just in case you didn't notice, I altered the example to exactly suit your needs. While C++ Builder hadles dialog instantiation differently, the rest of that pure WinAPI example will show how to handle subclassing in the big picture.
0
 
LVL 2

Author Closing Comment

by:alcindor
ID: 39811545
Thanks for your help. I will take some time to study the references.
I have adopted a different approach which involves less code using the VCL Clipboard component to clear the clipboard with an on-click event for the EditBox control in question. the code is as follows:

void __fastcall TForm1::Edit1Click(TObject *Sender)
{
    Clipboard()->Clear();
}
0
 
LVL 86

Expert Comment

by:jkr
ID: 39812315
I'd be careful about emptying the clipboard, since that is something that the user won't expect and this would interfere with their workflow... there might be something important in there that was not intended for yor app...
0
 
LVL 2

Author Comment

by:alcindor
ID: 39812968
The app is the only one that is running with user interaction. The user has been copying and pasting data that he has previously entered which is not what he should be doing. This measure is intended to force the user to type the entry each time. The IT team have temporarily disabled the right-hand mouse button on this PC (un-beknownst to me) using a registry setting as it would appear that the operator is not aware of ctrl-C/ctrl-V to copy & paste. The application is in an automotive production line environment and the operators are only supposed to use the control application so there is no danger of any interference with their work.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
In this article, I will show you HOW TO: Install VMware Tools for Windows on a VMware Windows virtual machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, using the VMware Host Client. The virtual machine has Windows Server 2016 instal…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question