Solved

How to find out, that window appeared?

Posted on 2006-06-11
11
326 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
 

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
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

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

13 Experts available now in Live!

Get 1:1 Help Now