Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2020-11

FindWindow return sequence (Windows 10)?

3 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Bo Berglund

I am writing utilities to enumerate windows (in Windows10) based on the partial title. Will be used in order to find a particular set of windows.

The function should return a list of window titles and handles to the window. I found examples I have adapted and it is working partially..

But there is one thing I cannot understand and this is why the call to ListWindowExtd returns the windows out of sequence. The first handle is always larger than all following...

I thought that GetWindow(hWndTemp, GW_HWNDNEXT); would return the next larger handle following hWndTemp, but it seems like this is not the case. Is the result random or is there some known sequence when using the flag GW_HWNDNEXT??

Of course I could sort the result afterwards but why? Should not GW_HWNDNEXT do the sequencing?

I am currently using these 3 functions for the enumeration:

function GetWindowTitle(Hdl: HWND) : string;
var
  i: integer;
  cTitletemp: array [0..254] of Char;
  sTitleTemp: string;
begin
  Result := '';
  i := GetWindowText(Hdl, cTitletemp, 255);
  if i > 0 then
  begin
    sTitleTemp := cTitletemp;
    Result := Copy(sTitleTemp, 1, i);
  end;
end;
function FindWindowExtd(partialTitle : string) : HWND;
{Find handle to a window containing partialTitle}
var
  hWndTemp: hWnd;
  sTitleTemp: string;
begin
  partialTitle := LowerCase(partialTitle);
  hWndTemp := FindWindow(nil, nil); //First window???
  while hWndTemp  0 do
  begin
    sTitleTemp := lowercase(GetWindowTitle(hWndTemp));
    if pos(partialTitle, sTitleTemp)  0 then
      Break;
    hWndTemp := GetWindow(hWndTemp, GW_HWNDNEXT);
  end;
  result := hWndTemp;
end;
function ListWindowExtd(partialTitle : string) : string;
var
  hWndTemp: hWnd;
  sTitle: string;
begin
  Result := '';
  partialTitle := lowercase(partialTitle);
  hWndTemp := FindWindowExtd(partialTitle);
  if hWndTemp = 0 then exit;
  Result := IntToStr(hWndTemp) + '   '#9 + GetWindowTitle(hWndTemp);
  hWndTemp := GetWindow(hWndTemp, GW_HWNDNEXT);
  while hWndTemp  0 do
  begin
    sTitle := GetWindowTitle(hWndTemp);
    if pos(partialTitle, lowercase(sTitle))  0 then
      Result := Result + #13 + IntToStr(hWndTemp) + '   '#9 + sTitle;
    hWndTemp := GetWindow(hWndTemp, GW_HWNDNEXT);
  end;
end;
Bo Berglund Developer in Sweden

Reply by email

Sven Barth
to Bo Berglund

Bo Berglund via lazarus schrieb am Mo., 2.
Nov. 2020, 08:07:

Show quoted text 5 lines
I thought that GetWindow(hWndTemp, GW_HWNDNEXT); would return the next larger handle following hWndTemp, but it seems like this is not the case. Is the result random or is there some known sequence when using the flag GW_HWNDNEXT??

The documentation about GW_HWNDNEXT on MSDN says this:

=== MSDN begin ===

The retrieved handle identifies the window below the specified window in the Z order.
If the specified window is a topmost window, the handle identifies a topmost window. If the specified window is a top-level window, the handle identifies a top-level window. If the specified window is a child window, the handle identifies a sibling window.

=== MSDN end ===

So nowhere does it guarantee that the handles values themselves are ordered only that the resulting windows are ordered in some way.

In general you should not rely on the order of Handle values, because from the perspective of the application they are to be considered as random.

Regards,
Sven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20201102/69832569/attachment.html>

Reply by email

Bo Berglund
to Sven Barth
Sven Barth wrote 29 lines
Bo Berglund via lazarus schrieb am Mo., 2. Nov. 2020, 08:07: > I thought that GetWindow(hWndTemp, GW_HWNDNEXT); would return the next > larger handle following hWndTemp, but it seems like this is not the > case. Is the result random or is there some known sequence when using > the flag GW_HWNDNEXT?? > The documentation about GW_HWNDNEXT on MSDN says this: === MSDN begin === The retrieved handle identifies the window below the specified window in the Z order. If the specified window is a topmost window, the handle identifies a topmost window. If the specified window is a top-level window, the handle identifies a top-level window. If the specified window is a child window, the handle identifies a sibling window. === MSDN end === So nowhere does it guarantee that the handles values themselves are ordered only that the resulting windows are ordered in some way. In general you should not rely on the order of Handle values, because from the perspective of the application they are to be considered as random.

OK, thanks!
I just want to find the handles of windows related to each other... So I will disregard the numerical order.

Bo Berglund Developer in Sweden

Reply by email