Solved

Disable Cut command

Posted on 1998-08-08
5
570 Views
Last Modified: 2010-04-04

How can i disable Cut, Copy, and Paste comands with delphi?

Codes... please
0
Comment
Question by:dia050398
  • 3
  • 2
5 Comments
 
LVL 8

Expert Comment

by:ZifNab
ID: 1361737
Hi dia,

 Why do you want to do this?

but here is the way :

you make a new descendant of TEdit, and check the windows messages WM_PASTE, WM_CUT AND WM_COPY. In these methods, you DON'T call the inherited method.

Zif.
0
 
LVL 8

Accepted Solution

by:
ZifNab earned 70 total points
ID: 1361738
this is probably a way through the Form methods :

This is an example for COPY. Do the same for cut and paste. write the following:

   procedure TForm1.FormCreate(Sender: TObject);
   begin
     Application.OnMessage := AppMessage;
   end;

   procedure TForm1.AppMessage(var Msg: TMsg; var              Handled: Boolean);
    begin
     if (Msg.hwnd = edit1.Handle) and //if the focus is in the edit1
      (Msg.Message=WM_KEYDOWN) and //if the user              pressed a key
       ((Msg.wParam = 67) OR (Msg.wParam = 99)) then //if C or c was pressed
       begin
        if GetKeyState(VK_CONTROL) < 0 then //if control        key is down
         showmessage('copy is canceled');
         Handled := True;//cancel the copy action
       end;

Possible the same for WM_PASTE, .... but I didn't tried it.

Regards, Zif.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1361739
Last sentence had to be :

 same for PASTE and CUT.

You probebly also can get the same with WM_PASTE, WM_COPY, WM_CUT but I don't think they are available at form level, so this need to be done in a descendant of TEdit, ...

Zif.
0
 
LVL 4

Expert Comment

by:erajoj
ID: 1361740
Hi,
Here's a simple implementation I wrote for this question. It's probably missing some minor details but it works. It adds a default popupmenu [undo|delete|select all] and removes all clipboard messages. There might be some tricks in here that can be useful to you:

unit NoCBEdit;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Menus;

type
  TNoClipBoardEdit = class( TCustomEdit )
  private
    FDefaultMenu: TPopupMenu;
    FUndo, FDelete, FSelectAll : TMenuItem;
    procedure WndProc( var Message: TMessage ); override;
    procedure SetPopup( Menu: TPopupMenu );
    function  GetPopup: TPopupMenu;
    { Private declarations }
  protected
    procedure ContextDelete   ( Sender: TObject );
    procedure ContextUndo     ( Sender: TObject );
    procedure ContextSelectAll( Sender: TObject );
    procedure OnDefaultPopup  ( Sender: TObject );

    { Protected declarations }
  public
    constructor Create( AOwner: TComponent ); override;
    { Public declarations }
  published
    property PopupMenu: TPopupMenu read GetPopup write SetPopup;
    { Published declarations }
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents( 'Samples', [ TNoClipBoardEdit ] );
end;

constructor TNoClipBoardEdit.Create( AOwner: TComponent );
var
  mi1, mi2, mi3, sp1, sp2: TMenuItem;
begin
  inherited Create( AOwner );
  FDefaultMenu := TPopupMenu.Create( Self );
  with FDefaultMenu do begin
    OnPopup := OnDefaultPopup;
    sp1 := TMenuItem.Create( Self ); sp1.Caption := '-';
    sp2 := TMenuItem.Create( Self ); sp2.Caption := '-';
    FUndo      := TMenuItem.Create( Self ); FUndo.Caption := '&Undo';            FUndo.OnClick := ContextUndo;
    FDelete    := TMenuItem.Create( Self ); FDelete.Caption := '&Delete';        FDelete.OnClick := ContextDelete;
    FSelectAll := TMenuItem.Create( Self ); FSelectAll.Caption := 'Select &All'; FSelectAll.OnClick := ContextSelectAll;
    Items.Add( FUndo ); Items.Add( sp1 ); Items.Add( FDelete ); Items.Add( sp2 ); Items.Add( FSelectAll );
  end;
end;

procedure TNoClipBoardEdit.WndProc( var Message: TMessage );
begin
  case Message.Msg of
    WM_COPY, WM_PASTE, WM_CUT:; // do nothing!
    WM_CONTEXTMENU: if Assigned( PopupMenu )
      then PopupMenu.Popup( LOWORD( Message.LParam ), HIWORD( Message.LParam ) )
      else FDefaultMenu.Popup( LOWORD( Message.LParam ), HIWORD( Message.LParam ) );
    else inherited WndProc( Message );
  end;
end;

procedure TNoClipBoardEdit.ContextDelete   ( Sender: TObject );
begin
  SendMessage( Handle, WM_CLEAR, 0, 0 );
end;

procedure TNoClipBoardEdit.ContextUndo     ( Sender: TObject );
begin
  SendMessage( Handle, WM_UNDO, 0, 0 );
end;

procedure TNoClipBoardEdit.ContextSelectAll( Sender: TObject );
begin
  SendMessage( Handle, EM_SETSEL, 0, -1 );
end;

procedure TNoClipBoardEdit.OnDefaultPopup  ( Sender: TObject );
begin
  FUndo.Enabled      := Boolean( SendMessage( Handle, EM_CANUNDO, 0, 0 ) );
  FDelete.Enabled    := SelLength > 0;
  FSelectAll.Enabled := SelLength < Length( Text );
end;

procedure TNoClipBoardEdit.SetPopup( Menu: TPopupMenu );
begin
  if Assigned( Menu )
  then inherited PopupMenu := Menu
  else inherited PopupMenu := FDefaultMenu;
end;

function  TNoClipBoardEdit.GetPopup: TPopupMenu;
begin
  Result := inherited PopupMenu;
end;

end.

Use it a a "normal" TEdit.

/// John
0
 
LVL 4

Expert Comment

by:erajoj
ID: 1361741
Sorry!
The SetPopup event is erroneous. Remove the else statement.
You could remove the PopupMenu property completely and use the inherihed.

/// John
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

895 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

12 Experts available now in Live!

Get 1:1 Help Now