• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 581
  • Last Modified:

Disable Cut command


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

Codes... please
0
dia050398
Asked:
dia050398
  • 3
  • 2
1 Solution
 
ZifNabCommented:
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
 
ZifNabCommented:
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
 
ZifNabCommented:
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
 
erajojCommented:
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
 
erajojCommented:
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now