unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Button1: TButton; Edit1: TEdit; procedure Edit1KeyPress(Sender: TObject; var Key: Char); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char); var Found: boolean; i,SelSt,j: Integer; TmpStr: string; begin with (Sender as TEdit) do begin j :=0; SelSt :=SelStart; if (Key = Chr(vk_Back)) and (SelLength <> 0) then TmpStr := Copy(Text,1,SelStart)+Copy(Text,SelLength+SelStart+1,255) else if Key = Chr(vk_Back) then {SelLength = 0} TmpStr := Copy(Text,1,SelStart-1)+Copy(Text,SelStart+1,255) else if Key in [‘0‘..‘9‘] then begin TmpStr := Copy(Text,1,SelStart)+Key+Copy(Text,SelLength+SelStart+1,255); j :=1; end else {Key in [‘A‘..‘Z‘, etc]} TmpStr := Copy(Text,1,SelStart)+Copy(Text,SelStart+1,255); if (Key = Chr(vk_Back)) and (SelSt > 0) then Dec(SelSt) else if Key <> Chr(vk_Back) then Inc(SelSt); Key := #0; { indicate that key was handled } if SelSt = 0 then begin Text:= ‘‘; Exit; end else begin Text :=TmpStr; SelStart :=SelSt+j; end; end;end;end.
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char); begin if (Key=#3) or (Key=#22) then key:=#0; end;将一个空的popumenu赋给edit,防止右键菜单复制粘贴
type TForm1 = class(TForm) Edit1: TEdit; procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); private { Private declarations } FOldWndProc: TWndMethod; public { Public declarations } procedure NewWndProc(var Message: TMessage); end;
var Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.NewWndProc(var Message: TMessage); begin if (Message.Msg <> WM_CUT) and (Message.Msg <> WM_COPY) and (Message.Msg <> WM_PASTE) then FOldWndProc(Message); end;
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Edit1KeyPress(Sender: TObject; var Key: Char);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
var
Found: boolean;
i,SelSt,j: Integer;
TmpStr: string;
begin
with (Sender as TEdit) do
begin
j :=0;
SelSt :=SelStart;
if (Key = Chr(vk_Back)) and (SelLength <> 0) then
TmpStr := Copy(Text,1,SelStart)+Copy(Text,SelLength+SelStart+1,255)
else if Key = Chr(vk_Back) then {SelLength = 0}
TmpStr := Copy(Text,1,SelStart-1)+Copy(Text,SelStart+1,255)
else if Key in [‘0‘..‘9‘] then
begin
TmpStr := Copy(Text,1,SelStart)+Key+Copy(Text,SelLength+SelStart+1,255);
j :=1;
end
else {Key in [‘A‘..‘Z‘, etc]}
TmpStr := Copy(Text,1,SelStart)+Copy(Text,SelStart+1,255); if (Key = Chr(vk_Back)) and (SelSt > 0) then
Dec(SelSt)
else if Key <> Chr(vk_Back) then
Inc(SelSt);
Key := #0; { indicate that key was handled }
if SelSt = 0 then
begin
Text:= ‘‘;
Exit;
end
else
begin
Text :=TmpStr;
SelStart :=SelSt+j;
end;
end;end;end.
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if (Key=#3) or (Key=#22) then
key:=#0;
end;将一个空的popumenu赋给edit,防止右键菜单复制粘贴
就是将Edit1.WindowProc重定义。
uses
Messages, StdCtrls, Classes;
type
TMyEdit= Class(TEdit)
private
procedure myCopy(var Msg: TMessage); Message WM_COPY;
procedure myPaste(var Msg: TMessage); Message WM_PASTE;
procedure myCut(var Msg: TMessage); Message WM_CUT;
end;
procedure Register;
implementationprocedure TMyEdit.myCopy(var Msg: TMessage);
begin
msg.Msg:= 0;
end;procedure TMyEdit.myCut(var Msg: TMessage);
begin
msg.Msg:= 0;
end;procedure TMyEdit.myPaste(var Msg: TMessage);
begin
msg.Msg:= 0;
end;procedure Register;
begin
RegisterComponents('Hxy', [TMyEdit]);
end;
end.
保存后安装控件即可实现不能剪切、复制、粘贴功能
谢谢各位提供建议
代码如下:unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
FOldWndProc: TWndMethod;
public
{ Public declarations }
procedure NewWndProc(var Message: TMessage);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.NewWndProc(var Message: TMessage);
begin
if (Message.Msg <> WM_CUT) and (Message.Msg <> WM_COPY) and
(Message.Msg <> WM_PASTE) then
FOldWndProc(Message);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
FOldWndProc := Edit1.WindowProc;
Edit1.WindowProc := NewWndProc;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
Edit1.WindowProc := FOldWndProc;
end;
end.