2004. november 3., szerda

How to trap and process Windows messages before the applications' window procedure is executed


Problem/Question/Abstract:

How to trap and process Windows messages before the applications' window procedure is executed

Answer:

The following project source demonstrates how to get Window messages before the application's window procedure is called. It is rare, if ever, that this needs to be done. In most cases assigning a procedure to the Application.OnMessage will accomplish the same thing.


program Project1;

uses
  Forms, messages, wintypes, winprocs, Unit1 in 'UNIT1.PAS' {Form1};

{$R *.RES}

var
  OldWndProc: TFarProc;

function NewWndProc(hWndAppl: HWnd; Msg, wParam: Word; lParam: Longint): Longint; export;
begin
  result := 0; { Default WndProc return value }
  {Handle messages here; The message number is in Msg}
  result := CallWindowProc(OldWndProc, hWndAppl, Msg, wParam, lParam);
end;

begin
  Application.CreateForm(TForm1, Form1);
  OldWndProc := TFarProc(GetWindowLong(Application.Handle, GWL_WNDPROC));
  SetWindowLong(Application.Handle, GWL_WNDPROC, longint(@NewWndProc));
  Application.Run;
end.

Nincsenek megjegyzések:

Megjegyzés küldése