type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
UserMessage: UINT;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var
Recipients: DWORD; // holds the recipient flags
begin
{set the recipients to all applications}
Recipients := BSM_APPLICATIONS;
{send the user defined message to all applications on the system by
posting it to their message queues}
BroadcastSystemMessage(BSF_IGNORECURRENTTASK or BSF_POSTMESSAGE, @Recipients,
UserMessage, 0, 0);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
{register a user defined message}
UserMessage := RegisterWindowMessage('CallWindowProc Test Message');
end;
type
TForm1 = class(TForm)
Label1: TLabel;
ProgressBar1: TProgressBar;
Label2: TLabel;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
{the prototype for the new window procedure}
function NewWindowProc(TheWindow: HWND; Msg: Integer; wParam: WPARAM;
lParam: LPARAM): Longint; stdcall;
var
Form1: TForm1;
UserMessage: UINT; // holds a user defined message identifier
OldWindowProc: TFNWndProc; // holds a pointer to the previous window procedure
implementation
{$R *.DFM}
function NewWindowProc(TheWindow: HWND; Msg: Integer; wParam: WPARAM; lParam: LPARAM): Longint;
var
iLoop: Integer; // a general loop counter
begin
{if the user defined message has been received...}
if Msg=UserMessage then
begin
{...turn on some user interface elements}
Form1.ProgressBar1.Visible := TRUE;
Form1.Label2.Visible := TRUE;
Form1.Repaint;
{animate the progress bar for a short period of time}
for iLoop := 0 to 100 do
begin
Form1.ProgressBar1.Position := iLoop;
Sleep(10);
end;
{turn off the user interface elements}
Form1.ProgressBar1.Visible := FALSE;
Form1.Label2.Visible := FALSE;
{the message was handled, so return a one}
Result := 1;
end
else
{any other message must be passed to the previous window procedure}
Result := CallWindowProc(OldWindowProc, TheWindow, Msg, wParam, lParam);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
{register a user defined message}
UserMessage := RegisterWindowMessage('CallWindowProc Test Message');
{subclass this window. replace the window procedure with one of
ours. this window procedure will receive messages before the
previous one, allowing us to intercept and process any message
before the rest of the application ever sees it.}
OldWindowProc := TFNWndProc(SetWindowLong(Form1.Handle, GWL_WNDPROC,
Longint(@NewWindowProc)));
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
{reset the window procedure to the previous one}
SetWindowLong(Form1.Handle, GWL_WNDPROC, Longint(OldWindowProc));
end;