Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to find out, that window appeared?

Posted on 2006-06-11
11
Medium Priority
?
344 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
11 Comments
 
LVL 6

Accepted Solution

by:
pritaeas earned 336 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 332 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
Industry Leaders: 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 332 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

885 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