I tried to move the code from OnCreate to OnShow and now it works fine.
So in the OnCreate event of the form the window handle is not yet available? The few examples found use the OnCreate event itself.
I can't really answer that, but my gut says the problem is that the
Application.MainForm hasn't been assigned yet when the MainForm's
OnCreate event is fired, so there is nothing for
Application.MainFormHandle to return yet.
If
TForm1 is your MainForm, then there is no need to use
Application.MainFormHandle in the Form's own events. Use the Form's own
Handle property instead:
procedure TForm1.FormCreate(Sender: TObject);
const
WM_COPYGLOBALDATA = $0049;
begin
ChangeWindowMessageFilterEx(Handle, WM_COPYDATA, MSGFLT_ALLOW, nil);
ChangeWindowMessageFilterEx(Handle, WM_DROPFILES, MSGFLT_ALLOW, nil);
ChangeWindowMessageFilterEx(Handle, WM_COPYGLOBALDATA, MSGFLT_ALLOW, nil);
end;
However, when it comes to per-window registrations, I would suggest overriding the Form's virtual
CreateWnd() method instead. That way, the registrations can also survive window recreations, too.
protected
procedure CreateWnd; override;
procedure TForm1.CreateWnd;
const
WM_COPYGLOBALDATA = $0049;
begin
inherited;
ChangeWindowMessageFilterEx(Handle, WM_COPYDATA, MSGFLT_ALLOW, nil);
ChangeWindowMessageFilterEx(Handle, WM_DROPFILES, MSGFLT_ALLOW, nil);
ChangeWindowMessageFilterEx(Handle, WM_COPYGLOBALDATA, MSGFLT_ALLOW, nil);
end;