Search code examples
delphiwinapidelphi-10.3-riominimized

Best alternative for detecting Minimized state of a window?


I need to detect whether a specific window is minimized or not. For this purpose I have found two functions:

1.

function PAIsWindowMinimized(h: HWND): Boolean;
// Detects whether a window is minimized or not
var
  wp: Winapi.Windows.WINDOWPLACEMENT;
begin
  wp.length := SizeOf(Winapi.Windows.WINDOWPLACEMENT);
  Winapi.Windows.GetWindowPlacement(h, @wp);
  Result := wp.showCmd = Winapi.Windows.SW_SHOWMINIMIZED;  
end;

2.

// Alternative (https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-isiconic):
Winapi.Windows.IsIconic(h);

Which of the two alternatives is preferable? Or are they equally good in all situations?


Solution

  • IsIconic() is the proper and documented way to check if a window is minimized:

    IsIconic function

    Determines whether the specified window is minimized (iconic).

    Window Features

    The IsZoomed and IsIconic functions determine whether a given window is maximized or minimized, respectively. The GetWindowPlacement function retrieves the minimized, maximized, and restored positions for the window, and also determines the window's show state.

    Using anything else is a hack at best. The fact that IsIconic() and GetWindowPlacement() internally check the HWND for the WS_MINIMIZE window style is just an implementation detail. The overhead of using these functions rather than checking the window style manually is negligible.

    Stick with IsIconic(), it is the API that Microsoft specifically provides for this exact purpose.