Detecting window maximization

How do I detect whenever another application has just maximized its window, in my delphi application?
zicheAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Slick812Connect With a Mentor Commented:
here is some code for two button clicks, the   sbut_GetMaximizeClick  button click will do a  EnumWindows( ) API function, which will start the EnumFunc( ) to get all top level windows into an Array of  TWinMax, and then it will start  Timer3. In the Timer3 event it will loop through all window in array and test the IsZoomed. . If the IsMax if false then you can do code for a maximize event. . . . The other button click will just stop the Timer3
This has NO effect for sub MDI forms, only top level (Parent) windows



type
  TWinMax = record
    Handle: Cardinal;
    IsMax: Boolean;
    end;


  private
    { Private declarations }
    aryWinRec: Array of TWinMax;



implementation


function EnumFunc(hWnd: Cardinal; lParam: Integer): Bool; stdcall;
begin
Result := True;

if IsWindowVisible(hWnd) then
  begin
  SetLength(Form1.aryWinRec, Length(Form1.aryWinRec)+1);
  Form1.aryWinRec[High(Form1.aryWinRec)].Handle := hWnd;
  if IsZoomed(hWnd) then
    Form1.aryWinRec[High(Form1.aryWinRec)].IsMax := True
    else
    Form1.aryWinRec[High(Form1.aryWinRec)].IsMax := False;
  end;
end;

procedure TForm1.sbut_GetMaximizeClick(Sender: TObject);
begin
SetLength(Form1.aryWinRec, 0);
EnumWindows(@EnumFunc, 0);
Timer3.Interval := 200;
Timer3.Enabled := True;
end;

procedure TForm1.Timer3Timer(Sender: TObject);
var
i: Integer;
begin
If Length(aryWinRec) < 1 then
  Timer3.Enabled := False;

for i := 0 to High(aryWinRec) do
  if IsZoomed(aryWinRec[i].Handle) then
    begin
    if not aryWinRec[i].IsMax then // this is the Maximize Event
      Label10.caption := 'EVENT , '+IntToStr(aryWinRec[i].Handle)+' has JUST Maximized';
      // Do Whatever code you need to execute here for a maximize

    aryWinRec[i].IsMax := True;
    end else
    aryWinRec[i].IsMax := False;
end;

procedure TForm1.sbut_StopWinMaxClick(Sender: TObject);
begin
Timer3.Enabled := False
end;

 - - - - - - - - - - - -  - - -  - - - - - - - - - - - -  -

ask questions if you need more
0
 
henrythackerCommented:
There is an onresize event for forms, you could possibly use that and check whether the size has increased from your original values. You could compare the new form height / width values to the Screen.Height and Screen.Width which would give you the fullscreen values for your current resolution.
0
 
Slick812Commented:
hello  ziche,  I do not know of a function or message in the API that is called or sent when another application is maximized, although you can detect thin in your own application. What I would do is use the  API  function  IsZoomed( ), which will tell you is a top level wimdow is maximized, since there are no events for other apps maximized, I would set up a TTimer with an interval of about 200 milliseconds (5 times a second) and then test the  IsZoomed(OtherHandle) and if it is true then you can stop the timer and do something
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
zicheAuthor Commented:
Thanks henry but slick seams to know what I'm talking about. I specifically want to detect the window states of other running programs.

Mr slick, any chance of a bit of sample code to show how to go about your approach, because I m not too familiar with using the API thing you are talking about.

0
 
Slick812Commented:
Maybe some code, but I would ask -
Do you already know the "Handle" for the specific  top level window that you want to monitor?

I do not think this is so hard to do, it's a SINGLE  boolean test


procedure TForm1.Timer1Timer(Sender: TObject);
begin
if  IsZoomed(windowHandle) then
  begin
  Timer1.Enabled := False;
  ShowMessage('Window is maximized');
  end;
end;
0
 
zicheAuthor Commented:
Thanks.

Now, any code for getting the Handle to the window in another application that you dont kow anything about?

Is this possible?
0
 
henrythackerConnect With a Mentor Commented:
There are several different methods..

FindWindow(classname,windowname) - finds the handles using the specified windowname/classname. It is possible to search just using one of those parameters ie...

 form1.caption:=inttostr(FindWindow(0,'Untitled - Notepad'));

or alternatively, you can get the one underneath the cursor

myhandle:=WindowFromPoint(Mouse.CursorPos);

Would return the handle to the instance of the untitled document in Notepad. I understand for your uses this isn't particularly useful, because i imagine you would like to enumerate the processes executed on the system.  This can also be accomplished.

Firstly download Hyperstring from http://www.mindspring.com/~efd/hyperstr.htm, which contains many useful utilities. There is a function which returns a list of all the currently open windows in there. The following example i've written requires HyperStr and Hyperfrm to be added to the uses clause. Then just stick in a memo. When you run the application, the windows are parsed into a TStringList and query the API as to whether they are maximized or not. If they are, then the result is added to the memo which you will have put onto the form.

procedure TForm1.FormCreate(Sender: TObject);
var
window,windows:string;
i:integer;
MyList:TStringList;
begin
memo1.lines.clear;
MyList:=TStringList.Create();
try
windows:=Hyperfrm.GetWindows;
i:=1;
repeat
window:=Parse(windows,',',i);
mylist.Add(window);
until (i<1) or (i>Length(windows));
for i:=0 to mylist.Count-1 do
begin
if iszoomed(FindWindow(0,pchar(mylist.strings[i]))) then memo1.lines.add(mylist.strings[i]+' is Maximized');
end;
finally
MyList.Free();
end;
end;

Hope this helps, sorry for getting the wrong end of the stick the first time!

Henry Thacker.
0
 
Slick812Commented:
OK, what do you know about the program (or it's MAIN Window), that you want to monitor? ?
Is it just ONE program, or do you want to be able to choose a diferent program when YOUR application runs?
Does it always have the same TitleBar title in it's window, or does it change from time to time?
Can this program run more than one at a time? And do you want to monitor all of it's running windows, or just a certain one?
Would you know the file path to this program?
0
 
zicheAuthor Commented:
Thanks Henry, that's great.

It seems Ill have to split these points between the 2 of you.

Slick. To your questions.

Ill be completely ignorant of the other applications my application will be monitoring and I will also like to monitor all the applications that will be currently running on the system.

Here's more about what I want to do...

I want to create a windowless application that runs in the background. As the user runs other applications, I want my application to do something every time any window of any application covers the screen. This could be a window in Internet Explorer, Word or anything.

So I suppose this means when any form is maximized in an SDI application but only when the Parent form is maximized from an MDI application. But this consideration isn't really that important to begin with.
0
 
zicheAuthor Commented:
Thanks guys ::::::::::::::::::::::::::::::::::::::::::::::
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.