Link to home
Start Free TrialLog in
Avatar of eNarc
eNarcFlag for United Kingdom of Great Britain and Northern Ireland

asked on

how do you tell if your Application is Minimized?

Hi I'd like to know how I can tell if the Application is minimized or not?
Avatar of AnilKumarSharma
AnilKumarSharma
Flag of India image

Avatar of ThievingSix
Application.Active is FALSE when the application is minimized. The code is:

if not Application.Active then
...
else
...
@Application.Active is FALSE when the application is minimized
Not really exactly

From Delphi Help

Specifies whether the application is active and has focus.   Active is true while the application is  active and false if it is not. Active is set to  true in the constructor.  An application is active if the form or application has focus. An application  becomes inactive when a window from a different application is about to become  activated. When the application closes, Active  is set to false in the destructor.

There is the windows boolean function IsIconic than can be used like

if IsIconic(Application.Handle) then
      Application.Restore
    else
      Application.BringToFront;

Hi eNarc

This is what you want :

if WindowState = wsMinimized then Form1.Caption:=('I just got minimized, WindowState');
Or you can tell if your application is minimized the moment that this happens
so you can eliminate any other checks with a timer :

...
...
  public
    { Public declarations }
     procedure WMSysCommand(var Msg: TWMSysCommand);
     message WM_SYSCOMMAND;
  end;
...
...
procedure TForm1.WMSysCommand;
begin
   if (Msg.CmdType = SC_MINIMIZE) or
      (Msg.CmdType = SC_MAXIMIZE) then
   MessageBeep(0) ; //Or any other procedure-Function you want to launch.
   DefaultHandler(Msg) ;
end;
ASKER CERTIFIED SOLUTION
Avatar of CodedK
CodedK
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tormork
tormork

I use Delphi 7, so this might no be correct in newer versions.

What makes this a bit tricky is that delphi creates an invisible background window which does not return the correct result for isiconic. This window is also the reason the image that you get when you hover over a minimized window in windows xp is just a blank window with the applications icon in it.

A guid to "fix" this can be found at: http://www.installationexcellence.com/articles/VistaWithDelphi/Original/Index.html under "Where’s My Induction (or, Why the Secret Window)?"

If this window is removed as the article describes IsIconic will retur the correct value.

Try the following code (create a new form with a Timer, double click the onTimer event and replace the entrire code with the following):

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Timer1: TTimer;
    procedure Timer1Timer(Sender: TObject);
  private
    { Private declarations }
  public
    procedure CreateParams(var Params: TCreateParams); override;
    procedure WMSyscommand(var Message: TWmSysCommand); message WM_SYSCOMMAND;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.CreateParams(var Params: TCreateParams);
begin
  inherited CreateParams(Params);

  // The following will remove the original button from the start menu.
  ShowWindow(Application.Handle, SW_HIDE);
  SetWindowLong(Application.Handle, GWL_EXSTYLE,
    GetWindowLong(Application.Handle, GWL_EXSTYLE) and not WS_EX_APPWINDOW
    or WS_EX_TOOLWINDOW);
  ShowWindow(Application.Handle, SW_SHOW);

  Params.ExStyle := Params.ExStyle and not WS_EX_TOOLWINDOW or WS_EX_APPWINDOW;


end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  if IsIconic(Application.MainForm.handle)
    then memo1.lines.insert(0,'yep')
    else memo1.lines.insert(0,'nope')
end;

procedure TForm1.WMSyscommand(var Message: TWmSysCommand);
begin
  case (Message.CmdType and $FFF0) of
    SC_MINIMIZE:
    begin
      ShowWindow(Handle, SW_MINIMIZE);
      Message.Result := 0;
    end;
    SC_RESTORE:
    begin
      ShowWindow(Handle, SW_RESTORE);
      Message.Result := 0;
    end;
  else
    inherited;  
  end;
end;

end.