Solved

How to find out, that window appeared?

Posted on 2006-06-11
11
328 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 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

16 Experts available now in Live!

Get 1:1 Help Now