Solved

programmatically disable paste

Posted on 2014-01-26
5
441 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Identify two keyboard codes and how to enter them 9 56
Google Chrome new tab content. 5 19
Windows icons complete gone 8 29
SCSM reports export 1 12
If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
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…

839 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