how do you tell if your Application is Minimized?

eNarc
eNarc used Ask the Experts™
on
Hi I'd like to know how I can tell if the Application is minimized or not?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Application.Active is FALSE when the application is minimized. The code is:

if not Application.Active then
...
else
...
Ferruccio AccalaiSenior developer, analyst and customer assistance

Commented:
@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;

OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

CodedKSenior Software Engineer

Commented:
Hi eNarc

This is what you want :

if WindowState = wsMinimized then Form1.Caption:=('I just got minimized, WindowState');
CodedKSenior Software Engineer

Commented:
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;
Senior Software Engineer
Commented:
Or ...

interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, AppEvnts, StdCtrls;
 
type
  TForm1 = class(TForm)
    ApplicationEvents1: TApplicationEvents;
    procedure ApplicationEvents1Minimize(Sender: TObject);
    procedure ApplicationEvents1Restore(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
procedure TForm1.ApplicationEvents1Minimize(Sender: TObject);
begin
   Application.Title := 'Minimized'
end;
 
procedure TForm1.ApplicationEvents1Restore(Sender: TObject);
begin
  Application.Title := 'Maximized'
end;
 
end.

Commented:
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial