?
Solved

programmatically disable paste

Posted on 2014-01-26
5
Medium Priority
?
451 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A small collection of useful tips and tricks for Windows 10 users that I decided to write as a result of recent questions that were asked and answered at Experts Exchange. Two short video tutorials included. Enjoy..
Compliance and data security require steps be taken to prevent unauthorized users from copying data.  Here's one method to prevent data theft via USB drives (and writable optical media).
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

765 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