Search code examples
delphiwindows-messages

Message passing with parent handle not reaching


In a new project, I created a MainForm with 2 panels, and a Form with a button.

I added this code on the MainForm:

interface

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    procedure FormCreate(Sender: TObject);
  private
     procedure OnMyMessage(var Msg: TMessage); message WM_FILEREADY;
  public
    { Public declarations }
  end;

implementation

uses
  PannelForm;

{$R *.dfm}


procedure TForm1.FormCreate(Sender: TObject);
begin
  with TForm2.Create(self) do
  try
    parent := panel2;
    borderstyle := bsNone;
    InnerHandle := self.Handle;
    Show;

  finally

  end;
end;

procedure TForm1.OnMyMessage(var Msg: TMessage);
begin
  showmessage('got event');
end;

And this code on the Form with a button:

type
  TForm2 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    InnerHandle:HWND;
  end;

procedure TForm2.Button1Click(Sender: TObject);
begin
//  PostMessage(Application.Mainform.Handle, WM_FILEREADY, 0, 0); // works
//  PostMessage(Application.Handle, WM_FILEREADY, 0, 0); // not working
//  PostMessage(parent.Handle, WM_FILEREADY, 0, 0); // not working
  PostMessage(InnerHandle, WM_FILEREADY, 0, 0); // works

end;

My question is: when calling the first and forth version, everything is fine.

What is missing in the third version that is not working?

Why doesn't Parent contain the right handle? Isn't it the (part of) point of passing a Parent?


Solution

  • You have implemented message handling in TForm1, but Form2.Parent.Handle is not Form1.Handle instead you have assigned Panel2.Handle to it.

    Each windowed control has its own handle. So your panels have different handles than your form and they cannot process messages that are implemented in Form class.

    Everything works as it should, even though it is not what you expect.