Solved

Detecting window maximization

Posted on 2004-08-22
10
361 Views
Last Modified: 2010-04-05
How do I detect whenever another application has just maximized its window, in my delphi application?
0
Comment
Question by:ziche
  • 4
  • 4
  • 2
10 Comments
 
LVL 1

Expert Comment

by:henrythacker
ID: 11874073
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
 
LVL 33

Expert Comment

by:Slick812
ID: 11877820
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
 

Author Comment

by:ziche
ID: 11878270
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
 
LVL 33

Expert Comment

by:Slick812
ID: 11883181
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
 

Author Comment

by:ziche
ID: 11889445
Thanks.

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

Is this possible?
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 1

Assisted Solution

by:henrythacker
henrythacker earned 60 total points
ID: 11891287
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
 
LVL 33

Expert Comment

by:Slick812
ID: 11896658
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
 

Author Comment

by:ziche
ID: 11900207
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
 
LVL 33

Accepted Solution

by:
Slick812 earned 60 total points
ID: 11909066
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
 

Author Comment

by:ziche
ID: 11910173
Thanks guys ::::::::::::::::::::::::::::::::::::::::::::::
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now