unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs,Clipbrd, StdCtrls;type TForm1 = class(TForm) Button1: TButton; Edit1: TEdit; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } procedure NewWndProc(var Message: TMessage); end;var Form1: TForm1; OldWndProc:TWndMethod; implementation{$R *.dfm}{ TForm1 }procedure TForm1.NewWndProc(var Message: TMessage); var str:string; i:Integer; begin if Message.Msg=WM_PASTE then begin str:=clipboard.AsText; for i:=1 to Length(str)-1 do begin if Ord(str[i])>128 then begin Message.Msg:=0; Break; end; end; end; OldWndProc(Message); end;procedure TForm1.Button1Click(Sender: TObject); begin OldWndProc:=Edit1.WindowProc; Edit1.WindowProc:=NewWndProc; end;end.写的不好,你可以参考下
楼上不要写个新函数出来,直接覆盖WndProc就行
Edit中的粘贴时不会触发WM_PASTE消息的
假如你需要重装WndProc来拦截消息的话,参考下面unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs,WinSock, StdCtrls;type TForm1 = class(TForm) Edit1: TEdit; private { Private declarations } procedure WndProc(var msg:TMessage);override; public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm} { TForm1 }procedure TForm1.WndProc(var msg: TMessage); begin if msg.Msg = WM_PASTE then ShowMessage('ok'); inherited; end;end.
下面的可以unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs,WinSock, StdCtrls;type TForm1 = class(TForm) Edit1: TEdit; Button1: TButton; procedure FormCreate(Sender: TObject); private { Private declarations } procedure EditWndProc(var Message: TMessage); public { Public declarations } end;var Form1: TForm1; EditHandle: THandle; EditPointer:Pointer;implementation{$R *.dfm} { TForm1 } { TForm1 }procedure TForm1.EditWndProc(var Message: TMessage); begin case Message.Msg of WM_PASTE : ShowMessage('ok'); //这里可以做你要做的事情 end; Message.Result:=CallWindowProc(EditPointer, EditHandle, Message.Msg, Message.WParam, Message.LParam); end;procedure TForm1.FormCreate(Sender: TObject); Var P:Pointer; begin EditHandle:=Edit1.Handle; if EditHandle<>0 then begin EditPointer := Pointer(GetWindowLong(EditHandle, GWL_WNDPROC)); P := Classes.MakeObjectInstance(EditWndProc); SetWindowLong(EditHandle, GWL_WNDPROC, Longint(P)); end; end;end.
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,Clipbrd, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure NewWndProc(var Message: TMessage);
end;var
Form1: TForm1;
OldWndProc:TWndMethod;
implementation{$R *.dfm}{ TForm1 }procedure TForm1.NewWndProc(var Message: TMessage);
var
str:string;
i:Integer;
begin
if Message.Msg=WM_PASTE then
begin
str:=clipboard.AsText;
for i:=1 to Length(str)-1 do
begin
if Ord(str[i])>128 then
begin
Message.Msg:=0;
Break;
end;
end;
end;
OldWndProc(Message);
end;procedure TForm1.Button1Click(Sender: TObject);
begin
OldWndProc:=Edit1.WindowProc;
Edit1.WindowProc:=NewWndProc;
end;end.写的不好,你可以参考下
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,WinSock, StdCtrls;type
TForm1 = class(TForm)
Edit1: TEdit;
private
{ Private declarations }
procedure WndProc(var msg:TMessage);override;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}
{ TForm1 }procedure TForm1.WndProc(var msg: TMessage);
begin
if msg.Msg = WM_PASTE then
ShowMessage('ok');
inherited;
end;end.
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,WinSock, StdCtrls;type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
procedure EditWndProc(var Message: TMessage);
public
{ Public declarations }
end;var
Form1: TForm1;
EditHandle: THandle;
EditPointer:Pointer;implementation{$R *.dfm}
{ TForm1 }
{ TForm1 }procedure TForm1.EditWndProc(var Message: TMessage);
begin
case Message.Msg of
WM_PASTE : ShowMessage('ok'); //这里可以做你要做的事情
end;
Message.Result:=CallWindowProc(EditPointer, EditHandle, Message.Msg, Message.WParam, Message.LParam);
end;procedure TForm1.FormCreate(Sender: TObject);
Var P:Pointer;
begin
EditHandle:=Edit1.Handle;
if EditHandle<>0 then
begin
EditPointer := Pointer(GetWindowLong(EditHandle, GWL_WNDPROC));
P := Classes.MakeObjectInstance(EditWndProc);
SetWindowLong(EditHandle, GWL_WNDPROC, Longint(P));
end;
end;end.
有什么办法能不做粘贴工程么?