Solved

How to find out, that window appeared?

Posted on 2006-06-11
11
339 Views
Last Modified: 2010-04-04
Is there any way to find out, that a window has appeard? For simple example, when Notepad is launched and its window appears, my Delphi application reacts and executes some action ...  Thank you.
0
Comment
Question by:JackKuti
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 6

Accepted Solution

by:
pritaeas earned 84 total points
ID: 16881641
You can use FindWindow to find a certain window. There are a lot of examples on this subject on the web.

More info:
http://www.swissdelphicenter.ch/torry/showcode.php?id=327
0
 
LVL 20

Expert Comment

by:Mark Brady
ID: 16882662
If you want your delphi app to find an open window (like notepad for eg) this is easy as stated above.  however, if Notepad has the focus then your app will not know that notepad has opened.  you will have to create a hook of some sort that runs in the background and checks every window opened and when it see Notepad open then it can do some action.

Elvin
0
 
LVL 6

Assisted Solution

by:saravananvg
saravananvg earned 83 total points
ID: 16883977
Hello Sir,

  You could check whether a process is running to find out whether a winow appeared or not supplying the exe name.


uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,Tlhelp32, StdCtrls;
const
  MAX_PATH           = 260;
  TH32CS_SNAPPROCESS = 2;
type
  TProcessEntry32 = packed record
    iSize,
    iUsage,
    iProcessID,
    iDefaultHeapId,
    iModuleId,
    iThreads,
    iParentProcessId,
    iPriClassBase,
    iFlags  : Integer;
    aExeFile: array[0..MAX_PATH] of Char;
  end;
  function CreateToolHelpSnapShot(lFlags, lProcessId: Longint): Longint; stdcall;
  function ProcessFirst(hSnapshot: longint; var uProcess: TProcessEntry32): Longint; stdcall;
  function ProcessNext(hSnapshot: Longint; var uProcess: TProcessEntry32): Longint; stdcall;
  procedure CloseHandle(hPass: Longint); StdCall;
  function IsProcess(sExe: string): Boolean;

type
  TForm1 = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form1: TForm1;
  ExitCode: longword;
implementation

  function CreateToolHelpSnapShot; external 'kernel32.dll' name 'CreateToolhelp32Snapshot';
  function ProcessFirst; external 'kernel32.dll' name 'Process32First';
  function ProcessNext; external 'kernel32.dll' name 'Process32Next';
  procedure CloseHandle; external 'kernel32.dll';
  procedure SwitchToThisWindow(h1: hWnd; x: bool); stdcall;
  external user32 Name 'SwitchToThisWindow';
         {x = false: Size unchanged, x = true: normal size}
{$R *.dfm}

function IsProcess(sExe: string): Boolean;
(*
** This routine examines Windows processes currently running to see if a
** certain program is running.
**
** sExe  : The executable name of the program to check.
** Result: True if the program is running, False otherwise.
*)
var
  liI, lSnapShot: Longint;
  rProcess      : TProcessEntry32;
  EXEName : String;
begin
  Result := False;
  EXEName := UpperCase(sExe);
  lSnapShot := CreateToolHelpSnapShot(TH32CS_SNAPPROCESS, 0);
  if lSnapShot <> 0 then begin
    rProcess.iSize := SizeOf(rProcess);
    liI := ProcessFirst(lSnapShot, rProcess);
    while liI <> 0 do begin
      if Pos(EXEName, UpperCase(rProcess.aExeFile)) <> 0 then begin
        Result := True;
        Break;
      end;
      liI := ProcessNext(lSnapShot, rProcess);
    end;
    CloseHandle(lSnapShot);
  end;
end;

On the click event of the button call the function
 function IsProcess(sExe: string): Boolean;
like

 if IsProcess('Notepad.exe') then
   ShowMessage('Notepad is running');


with regards,
padmaja.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:JackKuti
ID: 16884306
Thanks all for your comments. I really need some application in the background, that is activated by some specific window appearing on the screen. That means no user activity, no button pressing to find a window etc.
0
 
LVL 17

Assisted Solution

by:TheRealLoki
TheRealLoki earned 83 total points
ID: 16897867
0
 
LVL 4

Expert Comment

by:David_Ward
ID: 16900710
Yep, WindowsHooks are the elegent way to do it, but, you could just call your "snapshot" code from a timer
0
 
LVL 4

Expert Comment

by:David_Ward
ID: 17977856
cwwkie:

This seems a strange decision to me.

Priteas, elvinn66 and TheRealLoki have all provided valid hints and suggestions as to how to create a solution.

But, saravananavq has provided a working code solution which would just need to be called, periodically, from a timer, it could (should) also be placed in it's own thread and that would be that, job done!

I am curious as to why you have not considered saravananvq for ALL of the points, indeed, at least, why you have not thought that "he?" should at least SHARE the available points???
0
 
LVL 14

Expert Comment

by:cwwkie
ID: 17990898
>> I am curious as to why you have not considered saravananvq for ALL of the points, indeed,
>> at least, why you have not thought that "he?" should at least SHARE the available points???

I think I ticked the wrong box. I did not give all points, because it does not mention a timer, but an onclick instead.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

738 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