Examples Delphi

Title: How to capture messages and process them before my application.run
Question: 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.
Answer:
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.