自定义消息问题 (200分)

H

hpsmall

Unregistered / Unconfirmed
GUEST, unregistred user!
主程序定义如下:
Const
CM_ParaMESSAGE = WM_USER + $1001;
{========================自定义消息处理===========================}
procedure RestoreRequest(var cmessage: TMessage); message CM_ParaMESSAGE;

procedure Tmainform.RestoreRequest(var cmessage: TMessage);
begin
。。。。
cmessage.Result := 1;
。。。。
end;

另外一个程序发送消息到主程序:
var
RvHandle: THandle;
begin
RvHandle := FindWindow(nil,'主程序标题');
if RvHandle = 0 then
ShowMessage('目标窗口没找到!')
else
PostMessage(RvHandle, CM_ParaMESSAGE, 0, 0);
end;

主程序没有任何反应,但如果在主程序的
procedure Tmainform.ApplicationEventsMessage(var Msg: tagMSG;
var Handled: Boolean);
中加入代码:
if Msg.message=CM_ParaMESSAGE then
Showok('Message Received!');
则显示'Message Received!对话框,这说明消息的确发送到了主程序,但最上面那个过程
接收不到!为什么?
 
RvHandle := FindWindow('Tmainform',nil);
试试,就算类名是空,也不要用'',而用nil
 
写错了,代码里是nil
 
按照ysai说的做就可以了,我试过了,没有问题。不过两个程序主窗体的类名不能相同(如果用FindWindow(nil,'主程序标题'),则是
标题不能相同,否则找到的是自己)。
 
需要在系统中对自定义消息进行注册!
 
如何注册?
 
你看看这二个贴子:
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1097780
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1380953
 
你要用
var
CM_ParaMESSAGE:integer;
//定义一个全局变量
//在主Form创建的时候注册消息。
CM_ParaMESSAGE := Registerwindowmessage('CM_ParaMESSAGE');
 
不要定义常量!
不注册只在本程序中有效。


The RegisterWindowMessage function defines a new window message that is guaranteed to be unique throughout the system. The returned message value can be used when calling the SendMessage or PostMessage function.

UINT RegisterWindowMessage(
LPCTSTR lpString // address of message string
);

Parameters
lpString
Pointer to a null-terminated string that specifies the message to be registered.
Return Values
If the message is successfully registered, the return value is a message identifier in the range 0xC000 through 0xFFFF.

If the function fails, the return value is zero. To get extended error information, callGetLastError.

Remarks
The RegisterWindowMessage function is typically used to register messages for communicating between two cooperating applications.

If two different applications register the same message string, the applications return the same message value. The message remains registered until the session ends.

Only use RegisterWindowMessage when more than one application must process the same message. For sending private messages within a window class, an application can use any integer in the range WM_USER through 0x7FFF. (Messages in this range are private to a window class, not to an application. For example, predefined control classes such as BUTTON, EDIT, LISTBOX, and COMBOBOX may use values in this range.)

 
除了常量改为全局变量,我上面的代码还需要其他修改吗?
 
程序之间发送消息调用RegisterWindowMessage()函数。保证应用程序使用一致的消息编号。
 
jinmen说的没错.
 
怎么修改?
 
在你的主Form创建的时候就调用
CM_ParaMESSAGE := Registerwindowmessage('CM_ParaMESSAGE');
再在其他程序里用Postmessage.
 
我这个过程需要修改吗?
procedure RestoreRequest(var cmessage: TMessage); message CM_ParaMESSAGE;
 
你也可以通过重载
procedure WndProc(var Msg:TMessage);override;
来实现。
 
试了,但不行! 给个具体例子吧,谢谢了!!!
 
具体例子:

type
TForm1 = class(TForm)
...
public
{ Public declarations }
procedure WndProc(var Mess: TMessage); override;
...
end;

var ZwKeyMsg: UINT;

procedure TForm1.WndProc(var Mess: TMessage);
begin
if mess.Msg = ZwKeyMsg then
begin
Windows.Beep(2000,10);
end;
inherited;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
ZwKeyMsg := RegisterWindowMessage('WM_ZW_KEYBOARD');
end;
 
气死我了:(
原来是因为那个主窗口不能响应任何事件了,而我放了一个TApplicationEvents可以响应
消息,并能收到其他程序发送的自定义消息。

现在的问题是为什么主窗口不能响应任何事件了?原来可以的啊!
注:主窗口已经Enabled.
 
顶部