wait for a windows to be created...

Hi,
the goal is to center a windows after it is created.
in my function (look below) I ask a program to open a window for me.
After that I want to wait to be sure that the window was created
because I want to center that window.
the first time I do the operation the
creation is not too fast, so the window
is not centered because it shows up after
the window has been created. Look at my
code please and tell me where I'm wrong.

Thanks for your help.

  try
    WinExecAndWait32(getCMFullPath + 'bin\' + cmTaskToExecute + filename, 0, MustWait);
    //the process is launched but sometimes the window does not come to top Z-buffer
    //so we do it by hand.
    if CenterWindow then
    begin
      //to center a window we need to get a handle on it, the problem is that
      //when we search for the handle the windows is still not created
      //so we first need to wait for the window to appear
      //but maybe that window will never be created because of an error for eg
      //so we have to setup a timeout
      tickStart := GetTickCount;

      if not WindowExist(WindowName) then
      begin
        //showmessage('not found' + WindowName);
        repeat
          Sleep(20);
          if WindowExist(WindowName) then
            break;
        until ((GetTickCount - tickStart) < TimeOutWaitForWindow);
      end;
      //showmessage('go for center !');
      CenterTopMostCMWindows(WindowName);
    end;
    //show the message view after each operation
    if ShowMessageView then
      ShowCMMessageViewWindows(True);
  except
LVL 4
jeurkAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
TheNeilConnect With a Mentor Commented:
I was being honest about wanting the points. I suppose I was also being honest about OFFERING to lie about believing in God (I can't believe I'm having this conversation). Just to clarify the devine power thing: No, I don't believe in God (Do I get the points now? Sorry everyone else but I had to ask)

The Neil
0
 
TheNeilCommented:
TRy the following as a replacement for your existing REPEAT loop.

iCount := 0;
iLimit := 5;

DecodeTime(Now, iHour, iMin, iOldSec, iMsec);
 
REPEAT
  DecodeTime(Now, iHour, iMin, iSec, iMsec);
  IF iSec <> iOldSec
  THEN
  BEGIN
    iCount := iCount + 1;
    iOldSec := iSec;
  END;
  Application.ProcessMessages;
UNTIL WindowExist(WindowName) OR (iCount > iLimit);

What this will do is keep looking for your window until it finds it OR iLimit seconds have passed.

Also, I wouldn't use WinExecAndWait32. Try launching your EXE with ShellExecute instead

The Neil
0
 
florisbCommented:
If you use WinExec (createProcess for 32 bit will make the same possible), you can use the second parameter and put SW_MAXIMIZE in it.

With the handle to the thing you created you can also call ShowWindow(handle,SW_MAXIMIZE);



0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
jeurkAuthor Commented:
Hello,
I can't see a difference between your code and mine.

And i don't use ShellExecute because
sometimes I want to wait for the program
to return and it ShellExecute des not permit that.
0
 
florisbCommented:
if you accept maximixed as centered..
0
 
jeurkAuthor Commented:
Hello florisb,
the problem is that it's not the program
launched with WinExecAndWait32 that
opens a window. My function asks another
program to open a window. That's why I
have all those problems. Because
I don't have my hands on those windows. I have to get them by hand, by look at
their names. What a pain.
0
 
florisbCommented:
You use some kind of windows enum to do that? You should be able to get handles with it I suppose...


function CheckForOutlookProcesses(Handle : HWND; lparam : LPARAM): boolean; stdcall;
var
pName : array [0..MAX_PATH-1] of char;
sName : string;
begin
try
GetWindowText(handle, pname, MAX_PATH);
sName := strpas(pname);
if Pos('OUTLOOK', UpperCase(sname)) > 0 then
  begin
//  inc(Outlooks);
  end;
result := true;
except
  result := false;
end; //try
end;


function lastOutlookWindow : boolean;
{ Description : Is the closed Outlook the last Windows?
  Pre         : -
  Post        : -
  Input       : -
  Returns     : -
  Creator     : Floris
  Date        : 15 decamber 1999}
begin
Outlooks := 0;
EnumWindows(@CheckForOutlookProcesses, 0);
if Outlooks > 1 then
  result := true
else
  result := false;
end;
0
 
jeurkAuthor Commented:
Hello,
I have all those function and they are working I think. Do you want to look at it
to be sure ? :

unit U_WindowOperation;

interface

uses windows;

function EnumWindowsProc(WinHandle: THandle; FoundWindow: PHandle): BOOL; stdcall;
function isContinuusRunning: boolean;
function isSelectingTask: boolean;
function getWindowHandle(WindowCaption: string; WindowClass: string): THandle;
function getChildWindowHandle(WinParentHandle: THandle): THandle;
function getCMChildWinHandle(WinHandle: THandle; FoundWindow: PHandle): BOOL; stdcall;
function getMVChildWinHandle(WinHandle: THandle; FoundWindow: PHandle): BOOL; stdcall;
procedure CenterTopCMProjectWindows(isVisible: boolean);
procedure CenterTopMostCMWindows(WindowName: string);
procedure ShowCMMessageViewWindows(isVisible: boolean);
function WindowExist(WindowName:string): boolean;

var
  fWindowCaption: string;
  fWindowClass: string;
  fChildWindowCaption: string;
  fChildWindowClass: string;

implementation

uses sysutils, forms, dialogs;

{ Search all available top-level windows on the Desktop for a Delphi 3 (!)
  application handle }

function EnumWindowsProc(WinHandle: THandle; FoundWindow: PHandle): BOOL; stdcall;
var
  Buffer: array[0..50] of Char;
begin
  Result := True;
  GetClassName(WinHandle, Buffer, SizeOf(Buffer) - 1);
  if StrIComp('C/CM TopLevel', Buffer) = 0 then
  begin
    GetWindowText(WinHandle, Buffer, SizeOf(Buffer) - 1);
    if Pos('Continuus Project View', Buffer) <> 0 then
    begin
      FoundWindow^ := WinHandle;
      Result := False;
      // MessageBox(0, 'Found Instance', 'OneD4All', MB_OK);
    end;
  end;
end;

{
 isContinuusRunning
 Created on 10/01/2000 by JKN for NMR_1001
 @Description <check if the main project windows does exist>
 Return code:
         @Return$ <returns true if the window exist>
}
function isContinuusRunning: boolean;
begin
  result := False;
  if getWindowHandle('Continuus Project View', 'C/CM TopLevel') <> 0 then
  begin
    result := True;
  end;
end;

{
 isSelectingTask
 Created on 11/01/2000 by JKN for NMR_1001
 @Description <test if the select task window is open>
 Return code:
         @Return$ <true if the select task window exists>
}
function isSelectingTask: boolean;
begin
  result := False;
  if getWindowHandle('Select Task', 'Ccm') <> 0 then
  begin
    result := True;
  end;
end;

{
 WindowExist
 Created on 11/01/2000 by JKN for NMR_1001
 @Description <test if a given window exists>
 Input:
         @arg_name1$ <the caption of the window to check for>
 Return code:
         @Return$ <True if the windows does exist>
}
function WindowExist(WindowName:string): boolean;
begin
  result := False;
  if getWindowHandle(WindowName, 'Ccm') <> 0 then
  begin
    result := True;
  end;
//  showMessage('not exist : '+ WindowName);
end;

function getCMChildWinHandle(WinHandle: THandle; FoundWindow: PHandle): BOOL; stdcall;
var
  Buffer: array[0..50] of Char;
begin
  Result := True;
  GetClassName(WinHandle, Buffer, SizeOf(Buffer) - 1);
  if pos(PChar(fWindowClass), Buffer) <> 0 then
  begin
    GetWindowText(WinHandle, Buffer, SizeOf(Buffer) - 1);
    if Pos(fWindowCaption, Buffer) <> 0 then
    begin
      FoundWindow^ := WinHandle;
      Result := False;
    end;
  end;
end;

{ Center the given window on the screen }

procedure CenterWindow(Wnd: HWnd);
var
  Rect: TRect;
  {$IFNDEF VER100}
  Monitor: TMonitor;
  {$ENDIF}
begin
  GetWindowRect(Wnd, Rect);
  {$IFNDEF VER100}
  if Application.MainForm <> nil then
    Monitor := Application.MainForm.Monitor
  else
    Monitor := Screen.Monitors[0];
  SetWindowPos(Wnd, HWND_NOTOPMOST,
    Monitor.Left + ((Monitor.Width - Rect.Right + Rect.Left) div 2),
    Monitor.Top + ((Monitor.Height - Rect.Bottom + Rect.Top) div 3),
    0, 0, SWP_NOSIZE or SWP_NOOWNERZORDER);
  {$ELSE}
  SetWindowPos(Wnd, HWND_NOTOPMOST,
    ((Screen.Width - Rect.Right + Rect.Left) div 2),
    ((Screen.Height - Rect.Bottom + Rect.Top) div 3),
    0, 0, SWP_NOSIZE or SWP_NOOWNERZORDER);
  {$ENDIF}
  SetForegroundWindow(Wnd);
end;

function getWindowHandle(WindowCaption: string; WindowClass: string): THandle;
begin
  fWindowCaption := WindowCaption;
  fWindowClass := WindowClass;
  result := 0;
  EnumWindows(@getCMChildWinHandle, Integer(@result));
end;

procedure CenterTopMostCMWindows(WindowName: string);
var
  cmWindow: THandle;
begin
  if WindowName = '' then
    exit;
   
  cmWindow := getWindowHandle(WindowName, 'Ccm');
  if cmWindow <> 0 then
  begin
    CenterWindow(cmWindow);
  end;
end;

//this is not working with multi monitors...
procedure CenterTopCMProjectWindows(isVisible: boolean);
var
  cmWindow: THandle;
begin
  cmWindow := getWindowHandle('Continuus Project View', 'C/CM');
  if cmWindow <> 0 then
  begin
    if not isVisible then
    begin
      SetWindowPos(cmWindow, HWND_TOPMOST, screen.Width div 2, screen.Height div 2, 0, 0, SWP_NOSIZE);
      ShowWindow(cmWindow, SW_MINIMIZE);
      ShowWindow(cmWindow, SW_HIDE);
    end
    else
    begin
      SetWindowPos(cmWindow, HWND_NOTOPMOST, screen.Width div 2, screen.Height div 2, 0, 0, SWP_NOSIZE);
      ShowWindow(cmWindow, SW_RESTORE);
      ShowWindow(cmWindow, SW_SHOW);
    end;
  end;
end;

{
 ShowCMMessageViewWindows
 Created on 10/01/2000 by JKN for NMR_1001
 @Description <shows the CM message view>
 Input:
         @arg_name1$ <true if we shall show it>
}
procedure ShowCMMessageViewWindows(isVisible: boolean);
var
  cmWindow: THandle;
begin
  cmWindow := getWindowHandle('Continuus Message View', 'CcmMessage');
  if cmWindow <> 0 then
  begin
    if not isVisible then
    begin
      SetWindowPos(cmWindow, HWND_TOPMOST, screen.Width div 2, screen.Height div 2, 0, 0, SWP_NOSIZE or SWP_NOMOVE      );
      ShowWindow(cmWindow, SW_MINIMIZE);
      ShowWindow(cmWindow, SW_HIDE);
    end
    else
    begin
      SetWindowPos(cmWindow, HWND_NOTOPMOST, screen.Width div 2, screen.Height div 2, 0, 0, SWP_NOSIZE or SWP_NOMOVE);
      ShowWindow(cmWindow, SW_RESTORE);
      ShowWindow(cmWindow, SW_SHOW);
    end;
  end;
end;

{
 getChildWindowHandle
 Created on 10/01/2000 by JKN for NMR_1001
 @Description <get's a child window handle>
 Input:
         @arg_name1$ <the handle of the parent window>
 Return code:
         @Return$ <the handle of the child window we found>
}
function getChildWindowHandle(WinParentHandle: THandle): THandle;
begin
  result := 0;
  EnumChildWindows(WinParentHandle, @getMVChildWinHandle, Integer(@result));
end;

{
 getMVChildWinHandle
 Created on 10/01/2000 by JKN for NMR_1001
 @Description <that function is a param for getChildWindowHandle
               it tests if the given handle is the one we are looking for>
 Input:
         @arg_name1$ <the handle to test>
         @arg_name2$ <the handle we searched>
 Return code:
         @Return$ <false to stop looking at the next handle, we found the right one>
}
function getMVChildWinHandle(WinHandle: THandle; FoundWindow: PHandle): BOOL; stdcall;
var
  Buffer: array[0..50] of Char;
begin
  Result := True;
  GetClassName(WinHandle, Buffer, SizeOf(Buffer) - 1);
  if pos(PChar('RICHEDIT'), Buffer) <> 0 then
  begin
    FoundWindow^ := WinHandle;
    Result := False;
  end;
end;

end.
0
 
MadshiCommented:
Hi John...

would you please tell me: Which process creates the window that you want to center? You say it is not the process that you started with WinExecAndWait32. Which process is it then?

Regards, Madshi.
0
 
jeurkAuthor Commented:
Hello my friends
the race is over.
I found my problem.
It was easy in fact.
The problem was here :
until ((GetTickCount - tickStart) < TimeOutWaitForWindow);

should have been :
until NOT ((GetTickCount - tickStart) < TimeOutWaitForWindow);

I never used a repeat...shame on me.

thanks for your help anyway.
0
 
TheNeilCommented:
Does that mean that no-one gets the points?

The Neil
0
 
jeurkAuthor Commented:
do you think you earned them ?
do you want them ?

-Jeurk
0
 
TheNeilCommented:
I don't think we earned them but I certainly want them. Do I get them for my honesty and having a general happy go lucky attitude?

The Neil
0
 
jeurkAuthor Commented:
do you believe the god exists ?
0
 
jeurkAuthor Commented:
sorry I meaned that...
0
 
TheNeilCommented:
If I lie and say yes do I get some points?

The Neil
0
 
jeurkAuthor Commented:
Choose and answer. Was'nt that you that where
talking about
honesty and having a general happy go lucky attitude
that seems incompatible with lying to me...

sorry for the buz you other guys.
0
 
MadshiCommented:
:-P
0
 
jeurkAuthor Commented:
Hi,
I'm surprised that when I choose to
accept a comment as an answer that
I cannot put D as a grade.
Anyway about god. It was just a way
to give you a chance to give me
a good answer. I could have asked
another question, but that one sounded
simple...and was dans l'air du temps.

CU guys, thanks for your help that time
again.
0
 
TheNeilCommented:
I would just like to accept these points on behalf of cheek and bare faced audacity.

What was wrong with my answer about God. It was honest and truthful. I hope I wasn't supposed to be writing a huge essay on the subject

The Neil =:)
0
 
jeurkAuthor Commented:
The best answers are short and efficient.
Here we do not have time to loose.
If you would have answered, just YES
or just NO you would have gotten an
A or B.

I know... I am fixing the rules here ;)

CU
0
 
florisbCommented:
maybe?
0
 
TheNeilCommented:
Err I did answer 'NO' - It just took a little time. And now, for all those who also have nothing better to do today but read and write general gibberish:

Do I Believe In God - An Essay by The Neil

Do I believe in God? That's a big question. First we must consider...

The Neil
0
 
jeurkAuthor Commented:
I hope you are not ungry. That was not
the goal. And I'm not making fun of you.
CU
0
 
TheNeilCommented:
Me angry? Not over this. I was just having a laugh at it all. As for making fun of me then go right ahead, everybody else I've ever met has so why should you be any different.

Calm down jeurk or you'll end up having a heart attack or something equally nasty

The Neil
0
 
jeurkAuthor Commented:
I'm calm. it's just that I find the
place cool here . And I don't want to offand
people here.
I prefer to make to much then not enought.
So, let's meet in another thread.

CYA
0
 
florisbCommented:
Bit too calm here.

Wainting for Neils essay,
Jeurk, don't worry.

0
 
TheNeilCommented:
Oh. Should I ask everybody to STOP being calm? to get all excited?

Essay, essay, essay. Bearing in mind that I'll probably get death threats from every religion in the world (including those that are against violence) it's probably best if it doesn't get posted onto the net.

Imagine the shock and public outcry if the newspapers picked it up - "Stop This Depraved Anti-Religion Blasphemy NOW!", "Net Nut Knocks God", "Anti-God Freak Gets Struck By Lightning" and etc.

I'm beginning to wonder if putting my life on the line was really worth 76 expert points. Hmmmmm

The Neil
0
 
MadshiCommented:
>> Anti-God Freak Gets Struck By Lightning

I like this one especially...  :-))

(Just a joke)
0
 
TheNeilCommented:
Anyone wishing to lavish points on me for being a total halfwit idiot clown is more than welcome (I'm certainly not going to get them for writing answers to questions)

The Neil
0
All Courses

From novice to tech pro — start learning today.