Thanks to all for your new posts.
Probably: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getforegroundwindow
I have good news: on Windows this works now! Thanks a lot Martin.
There was some disadvantage, that when the Focus is moved back to the program, who had it before, that then this other program is also moved to the foreground, so it can cover "my Form" partly or completely.
The only way I could find to prevent this, is to use 'FormStyle:=fs
SystemStayOnTop' before 'SetForegroundWindow'.
But I had massive problems, because this only worked, when I set 'FormStyle:=fsSystemStayOnTop' *permanently* (but that's not what I want).
- all what I tried to set again 'FormStyle:=fsNormal' "later" moved the other program in the foreground and could cover my Form
- all what I tried to set 'FormStyle:=fsStayOnTop' "later" did nothing, that means, that my Form stayed permanently with 'fsSystemStayOnTop' over all other programs.
After trying "millions" of variations for some hours the problem suddenly disapeared. Now it works to set 'FormStyle:=fsNormal' after 'SetForegroundWindow' (and it works also after a restart of Windows). Have absolute no explanation for that.
If this issue does not occur again I have a good solution for Windows :-))
To prevent your application from stealing focus when it (re)activates itself? Because if it is, then I've got a partial solution for you
(Windows-only unfortunately):
{$ifdef WINDOWS}
ShowWindow(Form.Handle, SW_SHOWNOACTIVATE); // Makes the form visible without stealing focus
{$endif}
Form.Visible := True; // Synchronizes LCL state with WM state
Form.Update(); // Required in my use case (splash screen), but it's possible that your form doesn't need this
I tried your suggestion (it reads as it should work), but unfortunately my Form always has the Focus when it is made visible this way.
Without 'Update' command there is no difference.
Without 'Visible:=True' the Form stays invisible.
I tested in Win7 32bit with Lazarus 3.6.0 and 2.0.10.
Linux and Windows:
...
And at the beginning of the form's OnCreate event, you call SaveFocus, and in the destination event call RestoreFocus
Tested on Linux Mint (Cinnamon X11) and Windows 10.
The Windows part is the same as from Martin, I got it to work.
The Linux part I will test as soon I find some time and will report after.
BTW: on Linux I use WindowManager 'X11' (forgot to mention that).