Solved

Disable Cut command

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

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

Codes... please
0
Comment
Question by:dia050398
[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 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

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

730 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