Solved

How to find out, that window appeared?

Posted on 2006-06-11
11
330 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 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 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