GabeinOZ
asked on
Preventing Cut and Paste in edit box
Hi all, I have a program that tests a users input after a lesson and I need to be able to prevent the users from cutting and pasting from the lesson portion to the edit box in the testing.How do I do this without eliminating the ability to use the shift and ctrl keys?(the ctrl isnt important but they may have to type in uppercase characters at times) Any ideas? Any help is greatly appreciated....
I wrote small component TCutPasteMonitor.
It can handle any TWinControl, not only TEdit, but any its descendant
TMemo, TMaskEdit ets.
Also you can modify it for your purpose.
Enjoy :)
unit CutPasteMnt;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
type
TCutPasteMonitor = class(TComponent)
private
FCanCut, FCanCopy, FCanPaste: Boolean;
FControl: TWinControl;
FWndProcAdd : Pointer;
ObjectInstance : Pointer;
procedure SetControl(Value : TWinControl);
protected
procedure WndProc(var Message: TMessage);
function Execute(Message: TMessage) : Boolean; virtual;
public
constructor Create(AOwner : TComponent); override;
destructor Destroy; override;
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
published
property CanCut: Boolean read FCanCut write FCanCut;
property CanCopy: Boolean read FCanCopy write FCanCopy;
property CanPaste: Boolean read FCanPaste write FCanPaste;
property Control: TWinControl read FControl write SetControl;
end;
procedure Register;
implementation
{ TCutPasteMonitor }
constructor TCutPasteMonitor.Create(AO wner : TComponent);
begin
inherited Create(AOwner);
ObjectInstance := MakeObjectInstance(WndProc );
end;
destructor TCutPasteMonitor.Destroy;
begin
if (ObjectInstance <> nil) then
FreeObjectInstance(ObjectI nstance);
inherited Destroy;
end;
procedure TCutPasteMonitor.WndProc(v ar Message: TMessage);
begin
if not Execute(Message) then
with Message do
Result := CallWindowProc(FWndProcAdd , FControl.Handle, Msg, WParam, LParam);
end;
procedure TCutPasteMonitor.Notificat ion(ACompo nent: TComponent; Operation: TOperation);
begin
inherited;
if(AComponent = FControl) And (Operation = opRemove) then
FControl := nil;
end;
procedure TCutPasteMonitor.SetContro l(Value : TWinControl);
begin
if(FControl = Value) then Exit;
if(FControl <> nil) and (FWndProcAdd <> nil) then
begin
SetWindowLong(FControl.Han dle, GWL_WNDPROC, LongInt(FWndProcAdd));
FWndProcAdd := nil;
end;
FControl := Value;
if(FControl <> nil) and not (csDesigning in FControl.ComponentState) then
begin
FWndProcAdd := Pointer(GetWindowLong(FCon trol.Handl e, GWL_WNDPROC));
SetWindowLong(FControl.Han dle, GWL_WNDPROC, LongInt(ObjectInstance));
end;
end;
function TCutPasteMonitor.Execute(M essage: TMessage) : Boolean;
begin
Result := True;
if not FCanCut and (Message.Msg = WM_CUT) then Exit;
if not FCanCopy and (Message.Msg = WM_COPY) then Exit;
if not FCanPaste and (Message.Msg = WM_PASTE) then Exit;
Result := False;
end;
procedure Register;
begin
RegisterComponents('Expert sExchange' , [TCutPasteMonitor]);
end;
end.
It can handle any TWinControl, not only TEdit, but any its descendant
TMemo, TMaskEdit ets.
Also you can modify it for your purpose.
Enjoy :)
unit CutPasteMnt;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
type
TCutPasteMonitor = class(TComponent)
private
FCanCut, FCanCopy, FCanPaste: Boolean;
FControl: TWinControl;
FWndProcAdd : Pointer;
ObjectInstance : Pointer;
procedure SetControl(Value : TWinControl);
protected
procedure WndProc(var Message: TMessage);
function Execute(Message: TMessage) : Boolean; virtual;
public
constructor Create(AOwner : TComponent); override;
destructor Destroy; override;
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
published
property CanCut: Boolean read FCanCut write FCanCut;
property CanCopy: Boolean read FCanCopy write FCanCopy;
property CanPaste: Boolean read FCanPaste write FCanPaste;
property Control: TWinControl read FControl write SetControl;
end;
procedure Register;
implementation
{ TCutPasteMonitor }
constructor TCutPasteMonitor.Create(AO
begin
inherited Create(AOwner);
ObjectInstance := MakeObjectInstance(WndProc
end;
destructor TCutPasteMonitor.Destroy;
begin
if (ObjectInstance <> nil) then
FreeObjectInstance(ObjectI
inherited Destroy;
end;
procedure TCutPasteMonitor.WndProc(v
begin
if not Execute(Message) then
with Message do
Result := CallWindowProc(FWndProcAdd
end;
procedure TCutPasteMonitor.Notificat
begin
inherited;
if(AComponent = FControl) And (Operation = opRemove) then
FControl := nil;
end;
procedure TCutPasteMonitor.SetContro
begin
if(FControl = Value) then Exit;
if(FControl <> nil) and (FWndProcAdd <> nil) then
begin
SetWindowLong(FControl.Han
FWndProcAdd := nil;
end;
FControl := Value;
if(FControl <> nil) and not (csDesigning in FControl.ComponentState) then
begin
FWndProcAdd := Pointer(GetWindowLong(FCon
SetWindowLong(FControl.Han
end;
end;
function TCutPasteMonitor.Execute(M
begin
Result := True;
if not FCanCut and (Message.Msg = WM_CUT) then Exit;
if not FCanCopy and (Message.Msg = WM_COPY) then Exit;
if not FCanPaste and (Message.Msg = WM_PASTE) then Exit;
Result := False;
end;
procedure Register;
begin
RegisterComponents('Expert
end;
end.
It's another approach to a problem then ZifNaf proposes.
Instead making descendant of each control
we are subclassing existing controls (and their existing and future descendant).
Instead making descendant of each control
we are subclassing existing controls (and their existing and future descendant).
SORRY!!! ZifNab instead of ZifNaf
vladika, no problem with ZifNaf... BanZif :-)
I forgot explain how to use my component.
Set TEdit, TMemo or other TWinControl on the form.
Set TCutPasteMonitor on the form.
Set CutPasteMonitor.Control property.
Set CanCut, CanCopy and CanPaste properties.
Set TEdit, TMemo or other TWinControl on the form.
Set TCutPasteMonitor on the form.
Set CutPasteMonitor.Control property.
Set CanCut, CanCopy and CanPaste properties.
ASKER
thanks for the help zif but I tried Banzif's ( :P) component and it works perfectly....thanks for your answer though...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks again man...your component is sweet :)
catch ya next time...
Gabe
catch ya next time...
Gabe
make a descendant of TEdit and trap the messages for WM_PASTE, WM_CUT, WM_COPY. When you've trapped them, just do nothing! Don't call inherited.
Zif.