要说灵活性,自然是比不上Delphi自带的覆盖WndProc,或者替换WndProc方法。
unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Edit1: TEdit; procedure FormCreate(Sender: TObject); private FOldWndProc, FNewWndProc: TFarProc; procedure WindowProc(var Message: TMessage); end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.WindowProc(var Message: TMessage); begin Message.Result := CallWindowProc(FOldWndProc, Edit1.Handle, Message.Msg, Message.WParam, Message.LParam); end; procedure TForm1.FormCreate(Sender: TObject); begin FNewWndProc := MakeObjectInstance(WindowProc); FOldWndProc := Pointer(GetWindowLong(Edit1.Handle, GWL_WNDPROC)); SetWindowLong(Edit1.Handle, GWL_WNDPROC, LongInt(FNewWndProc)); end; end. // 其它消息做默认处理 Result := DefWindowProc(hWnd, uMsg, wParam, lParam);
参考:http://www.cnblogs.com/key-ok/p/3380556.html