Solved

Disable Cut command

Posted on 1998-08-08
5
569 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

746 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