Solved

programmatically disable paste

Posted on 2014-01-26
5
429 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Too many email signature changes to deal with?

Are you constantly being asked to update your organization's email signatures? Do they take up too much of your time? Wouldn't you love to be able to manage all signatures from one central location, easily design them and deploy them quickly to users. Well, you can!

Join & Write a Comment

NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup". After a while, you have entered a loop for Auto repair which does not fix anything and you will be in a  panic as all your work w…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
This Micro Tutorial will demonstrate how nuggets on the Web are formatted by using Chrome Developer Tools. These tools would not only view the site's CSS but it can also modify it and save the CSS to use on your own site.

744 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now