FMX Get Windows State

In my Delphi 13 FMX app I need to perform an action, but only if the app is minimised.

For Android this was easily done by using the TApplicationEvent, but that doesn’t appear to work for Windows.

While I can get part way there by tracking OnActivate and OnDeactivate, that only tells me if the app doesn’t have focus. I want to know when it’s minimised on Windows, but WindowState doesn’t update when the app is minimised (seems to be a known issue).

Some Googling led to TWindowPlacement and this code, but it never returns 2 (minimised).



function TfmMain.AppShowing: Boolean;
var
State: TWindowPlacement;
Wnd: HWND;
begin
Wnd := FmxHandleToHWND(Handle);
State.Length := SizeOf(TWindowPlacement);
GetWindowPlacement(Wnd,State);
Result := State.showCmd in [1,3];
end;


You could maybe use isIconic(hwnd) in winapi.windows

If true is minimised.

Thanks for the reply. Unfortunately that seems broken in FMX too.

You could try formToHWnd(self) to get the handle. (I have used this in the past)

I just made a bare bones FMX project (logging the state result to a memo every second) with the same code and it shows all three states correctly. There must be something else going on in my project.

I added logging in my real app and found something interesting. If I minimise the app using the _ button in the title bar, the result stays at wsNormal, but if I minimise it by clicking the app icon in the taskbar the results become wsMinimized.

In my test project the state result is reported correctly no matter how the app is minimised. I can’t think of anything in my real app that could cause this.