unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, sockets;
const
 wm_mymessage = wm_user + 1;
type
  TForm1 = class(TForm)
    btn1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure btn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure mymessage(var Msg: TMsg; var Handled: Boolean);
  end;var
  Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
  application.onmessage := mymessage;
end;procedure TForm1.mymessage(var Msg: TMsg; var Handled: Boolean);
begin
  if msg.message = wm_mymessage then // 为什么收不到?
    showmessage('ok');
end;procedure TForm1.btn1Click(Sender: TObject);
begin
//  sendmessage(handle, wm_mymessage,0, 0);
  sendmessage(application.handle, wm_mymessage,0, 0); //
end;end.

解决方案 »

  1.   

    把这句
    sendmessage(application.handle, wm_mymessage,0, 0); //
    换成
    Postmessage(application.handle, wm_mymessage,0, 0); //
      

  2.   

    把这句写在Form1.OnShow中,就可以了
    application.onmessage := mymessage;
      

  3.   

    application是在form建立之前就已经建立的, 为什么要在formshow的时候才能...?
      

  4.   

    sendMessage的消息不进入消息对列。application.onmessage这儿都是当前线程消息队列的消息。。
      

  5.   

    还有一点,APPLICATION。HANDLE同FORM1。HANDLE是两回事。。
      

  6.   

    我要实现的是,在其它unit里面发消息到主窗体,sendmessage(),但使用application.handle无反应,使用application.mainform.handle出现地址错,可能是未create的原因.如何解决?还有两个小问题.
    getcurrentprocess 和 getcurrentprocessID得到的是什么值,和窗体句柄有什么关系?谢谢
      

  7.   

    10975037(猩猩)的答案正确:)是不是由于sendmessage需要等待发送完后,再返回,而postmessage不需要.
    在application里面发消息到自己,sendmessage这样等为什么不会loop而当掉?