[Lazarus] FindWindow return sequence (Windows 10)?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[Lazarus] FindWindow return sequence (Windows 10)?

Free Pascal - Lazarus mailing list
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

--
_______________________________________________
lazarus mailing list
[hidden email]
https://lists.lazarus-ide.org/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] FindWindow return sequence (Windows 10)?

Free Pascal - Lazarus mailing list
Bo Berglund via lazarus <[hidden email]> 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.

Regards, 
Sven 

--
_______________________________________________
lazarus mailing list
[hidden email]
https://lists.lazarus-ide.org/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] FindWindow return sequence (Windows 10)?

Free Pascal - Lazarus mailing list
On Mon, 2 Nov 2020 11:08:32 +0100, Sven Barth via lazarus
<[hidden email]> wrote:

>Bo Berglund via lazarus <[hidden email]> 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

--
_______________________________________________
lazarus mailing list
[hidden email]
https://lists.lazarus-ide.org/listinfo/lazarus