message handling (early)

Q.   How can I get messages before my application's window procedure is called?

A.    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
    NewWndProc := 0; { Default WndProc return value }

    { * * * Handle messages here; The message number is in Msg * * * }

    NewWndProc := 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.