我希望不论应用程序运行到哪一步,都可以响应其它程序发来的消息,应该怎样做到?
例,新建一个Delphi工程:
program Project1;uses
Forms,
Windows,
Messages,
Dialogs,
Unit1 in 'Unit1.pas' {Form1};const UserMessageStr = 'testmsg';
{$R *.res}
var
UserMessage:Uint;Function ChooseFile:Boolean;
var
OpenDialog:TOpenDialog;
Begin
Result:=False;
OpenDialog:=TOpenDialog.Create(nil);
OpenDialog.Title := 'test';
OpenDialog.Filter:='test'+'(*.txt)|*.txt|';
OpenDialog.Options:=[ofHideReadOnly,ofAllowMultiSelect];
OpenDialog.FileName:='';
If OpenDialog.Execute then
Result:=True;
End;begin
UserMessage:=RegisterWindowMessage(UserMessageStr);
Application.Initialize;
ChooseFile;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
这个程序在运行的时候,其它程序会发广播消息SendMessage(HWND_BROADCAST,UserMessage,0,0);我希望这个程序无论何时,只要收到消息UserMessage,就马上弹出消息框('Success'),请问应怎样做到。
例,新建一个Delphi工程:
program Project1;uses
Forms,
Windows,
Messages,
Dialogs,
Unit1 in 'Unit1.pas' {Form1};const UserMessageStr = 'testmsg';
{$R *.res}
var
UserMessage:Uint;Function ChooseFile:Boolean;
var
OpenDialog:TOpenDialog;
Begin
Result:=False;
OpenDialog:=TOpenDialog.Create(nil);
OpenDialog.Title := 'test';
OpenDialog.Filter:='test'+'(*.txt)|*.txt|';
OpenDialog.Options:=[ofHideReadOnly,ofAllowMultiSelect];
OpenDialog.FileName:='';
If OpenDialog.Execute then
Result:=True;
End;begin
UserMessage:=RegisterWindowMessage(UserMessageStr);
Application.Initialize;
ChooseFile;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
这个程序在运行的时候,其它程序会发广播消息SendMessage(HWND_BROADCAST,UserMessage,0,0);我希望这个程序无论何时,只要收到消息UserMessage,就马上弹出消息框('Success'),请问应怎样做到。
CM_RESTORE = WM_USER + $1001; {自定义的消息}
procedure RestoreRequest(var msg: TMessage);message CM_Restore;
procedure TfrmMain.RestoreRequest(var msg: TMessage);
begin
//在此写你的代码
end;
UserMessage : UINT;
procedure TForm1.FormCreate(Sender: TObject);
begin
UserMessage := RegisterWindowMessage(UserMessageStr); //注册自己的消息
if UserMessage=0 then
begin
//
showmessage('注册失败!');
end;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
SendMessage(HWND_BROADCAST,UserMessage,handle,0); //广播消息
end;//-------------------------------------------------------------------------
在接受程序中const UserMessageStr = 'testmsg';var
UserMessage : UINT;procedure TForm1.FormCreate(Sender: TObject);
begin
UserMessage := RegisterWindowMessage(UserMessageStr); //注册自己的消息
end;procedure TForm1.WndProc(var Message: TMessage);
begin
if(Message.Msg = UserMessage) then
begin //接受到
//你的处理
end;
inherited;
end;
http://expert.csdn.net/Expert/topic/2569/2569320.xml?temp=.1078607
现在所发的贴子是对该贴的延伸,我看过有提到用Application.OnMessage方法去处理它,可是我没有试出来,所以想发贴子请教一下。
"无论何时"的意思是我不管这个应用程序有多少窗口,当前正在运行哪个窗口,或在处理什么事情,都要能够即时响应消息。
用线程会不会麻烦?Application对象应该可以处理消息吧?(我是刚用Delphi,没做过线程)
function TApplication.ProcessMessage(var Msg: TMsg): Boolean;
var
Handled: Boolean;
begin
Result := False;
if PeekMessage(Msg, 0, 0, 0, PM_REMOVE) then
begin
Result := True;
if Msg.Message <> WM_QUIT then
begin
Handled := False;
if Assigned(FOnMessage) then FOnMessage(Msg, Handled);
if not IsHintMsg(Msg) and not Handled and not IsMDIMsg(Msg) and
not IsKeyMsg(Msg) and not IsDlgMsg(Msg) then
begin
TranslateMessage(Msg);
DispatchMessage(Msg);
end;
end
else
FTerminate := True;
end;
end;
你可以用多线程调用循环调用他的函数,并对function TApplication.ProcessMessage(var Msg: TMsg): Boolean;函数修改,你就可以随时对接收到的消息进行处理了!!!
不过接收到消息最好用多线程来处理接收到的消息,不作特殊处理的要调用默认的消息处理,这样才能够一直不间断的处理消息,否则不能“在任何情况下接收消息”!!!procedure TApplication.ProcessMessages;
var
Msg: TMsg;
begin
while ProcessMessage(Msg) do {loop};
end;