delphi 使用自定义HANDLE处理消息

时间:2022-06-19 04:06:10

unit UCallHwnd; interface uses Classes, Messages, Windows; const WM_USER_MSG = WM_USER + 1; type TCallHwnd = class(TObject) private FHwnd: THandle; protected procedure DoUserMessage(var AMsg: TMessage); public constructor Create; destructor Destroy; override; //调用测试 function CallHwndTest: Boolean; end; implementation { TCallHwnd } function TCallHwnd.CallHwndTest: Boolean; begin Result := PostMessage(FHwnd, WM_USER_MSG, 1, 2) end; constructor TCallHwnd.Create; begin FHwnd := AllocateHWnd(DoUserMessage); end; destructor TCallHwnd.Destroy; begin DeallocateHWnd(FHwnd); inherited; end; procedure TCallHwnd.DoUserMessage(var AMsg: TMessage); begin if AMsg.Msg = WM_USER_MSG then begin //AMsg.LParam 2 //AMsg.WParam 1 //do something end else AMsg.Result := DefWindowProc(FHwnd, AMsg.Msg, AMsg.WPARAM, AMsg.LPARAM); end; end.