RegisterWindowsMessage的使用?

wuwoczj 2004-06-25 01:37:42
使用RegisterWindowsMessage注册一个系统消息,该如何使用呢
...全文
115 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
fei19790920 2004-06-25
  • 打赏
  • 举报
回复
发送端:
var
HOOK_EVENT: Integer;

HOOK_EVENT:=RegisterWindowMessage(PChar('HOOK_EVENT'));
sendmessage(findwindow(nil,'form2'),HOOK_EVENT,0,0);

接收端:
procedure WndProc(var Mess: TMessage); override;

var
HOOK_EVENT: Integer;
procedure TForm2.FormCreate(Sender: TObject);
begin
HOOK_EVENT:=RegisterWindowMessage(PChar('HOOK_EVENT'));
end;

procedure OnHookEvent(wParam,LParam: Longint);
begin
{
处理代码
}
end;

procedure TForm2.WndProc(var Mess: TMessage);
begin
if mess.Msg = HOOK_EVENT then
OnHookEvent(Mess.WParam, Mess.LParam)
else
inherited;
end;
juliens 2004-06-25
  • 打赏
  • 举报
回复
这是《Delphi Win32核心API参考》第三章的一个例子,分为两个程序,一个Broadcast消息,一个接收消息;
unit BroadcastSystemMessageU;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

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;

end.
接收:
unit CallWindowProcU;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls;

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;

end.

5,392

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧