Page 1 of 1
Find specific window by title?
Posted: Wed Jul 04, 2018 8:27 am
by Fangbeast
I can find a specific windows by its title but returning the title text into the buffer is one character short for some reason. Can anyone tell me what I have missed?
I would also like to find a window by string subset rather than the entire string. Is there a way to do that witht he API like FindString but in a window title?
Code: Select all
WindowText.s = "Untitled - Notepad"
TargetWindowHandle.i = FindWindow_(0, WindowText.s)
If TargetWindowHandle.i
Debug "Found notepad window handle: " + Str(TargetWindowHandle.i)
TextLength.i = GetWindowTextLength_(TargetWindowHandle.i)
If TextLength.i
Debug "Found title text of: " + Str(TextLength.i) + " characters in length "
*CharacterBuffer = AllocateMemory(TextLength.i)
If *CharacterBuffer
GetWindowText_(TargetWindowHandle.i, *CharacterBuffer, TextLength.i)
Debug PeekS(*CharacterBuffer)
Else
Debug "There was n0 character buffer allocated for some reason or other"
EndIf
Else
Debug "No text found in the notepad window titlebar to display"
EndIf
Else
Debug "Target window handle not found"
EndIf
Re: Find specific window by title?
Posted: Wed Jul 04, 2018 8:30 am
by RSBasic
Code: Select all
WindowText.s = "Untitled - Notepad"
TargetWindowHandle.i = FindWindow_(0, WindowText.s)
If TargetWindowHandle.i
Debug "Found notepad window handle: " + Str(TargetWindowHandle.i)
TextLength.i = GetWindowTextLength_(TargetWindowHandle.i)+1
If TextLength.i
Debug "Found title text of: " + Str(TextLength.i) + " characters in length "
*CharacterBuffer = AllocateMemory(TextLength.i)
If *CharacterBuffer
GetWindowText_(TargetWindowHandle.i, *CharacterBuffer, TextLength.i)
Debug PeekS(*CharacterBuffer)
Else
Debug "There was n0 character buffer allocated for some reason or other"
EndIf
Else
Debug "No text found in the notepad window titlebar to display"
EndIf
Else
Debug "Target window handle not found"
EndIf
Re: Find specific window by title?
Posted: Wed Jul 04, 2018 12:27 pm
by Fangbeast
Thanks RSBASIC, I was playing with that earlier and figured it out but I don't understand why GetWindowTextLength_(TargetWindowHandle.i) returns the text length -1?
I noticed that MSDN had some things to say and they pretty well hinted that ALL the text would be returned and sometimes a little more depending on some circumstances but never less.
They also said "To obtain the exact length of the text, use the WM_GETTEXT, LB_GETTEXT, or CB_GETLBTEXT messages".
Maybe I have to do that?
Re: Find specific window by title?
Posted: Wed Jul 04, 2018 12:46 pm
by RSBasic
Fangbeast wrote:They also said "To obtain the exact length of the text, use the WM_GETTEXT, LB_GETTEXT, or CB_GETLBTEXT messages".
You can also use WM_GETTEXT.
But you can't use LB_GETTEXT and CB_GETLBTEXT because LB_GETTEXT is only for List Box Control (ListViewGadget) and CB_GETLBTEXT for Combo Box Control (ComboBoxGadget).
Re: Find specific window by title?
Posted: Wed Jul 04, 2018 2:20 pm
by RASHAD
Hi Fang
According and after your discussion with RSBasic
Code: Select all
WindowText.s = "Untitled - Notepad"
TargetWindowHandle.i = FindWindow_(0, WindowText.s)
If TargetWindowHandle.i
Title$ = Space(#MAX_PATH)
SendMessage_(TargetWindowHandle,#WM_GETTEXT,#MAX_PATH,@title$)
Debug title$
Debug Len(Title$)
Else
Debug "Target window handle not found"
EndIf
Re: Find specific window by title?
Posted: Wed Jul 04, 2018 4:32 pm
by MrMat
This is fairly minor (depending on use case!), but SendMessage with WM_GETTEXT can hang (I've seen it happen). There is some explanation here:
https://blogs.msdn.microsoft.com/oldnew ... 0/?p=42833
I assume SendMessageTimeout would work around this (not tested).
Re: Find specific window by title?
Posted: Wed Jul 04, 2018 6:02 pm
by mk-soft
I like this for Notepad
Code: Select all
hWnd = 0
cnt = 0
Repeat
hWnd = FindWindowEx_(0, hWnd, @"NotePad", 0)
If hwnd
cnt + 1
title.s = Space(512)
GetWindowText_(hWnd, @title, 512)
Debug "" + cnt + ": " + title
hEdit = FindWindowEx_(hWnd, 0, @"Edit", 0)
If hEdit
len = SendMessage_(hEdit, #WM_GETTEXTLENGTH, 0, 0)
If len
text.s = Space(len)
SendMessage_(hEdit, #WM_GETTEXT, len + SizeOf(character), @text)
Debug text
Else
Debug "No text"
EndIf
Else
Debug "Error: No hEdit"
EndIf
EndIf
Until hWnd = 0
Find all Windows
Code: Select all
hWnd = 0
cnt = 0
Repeat
;hWnd = FindWindowEx_(0, hWnd, @"NotePad", 0)
hWnd = FindWindowEx_(0, hWnd, 0, 0)
If hwnd
cnt + 1
title.s = Space(512)
GetWindowText_(hWnd, @title, 512)
Debug "" + cnt + ": " + title
EndIf
Until hWnd = 0
Re: Find specific window by title?
Posted: Wed Jul 04, 2018 7:20 pm
by RSBasic
mk-soft wrote:Find all Windows
Or with EnumWindows_():
Code: Select all
Procedure EnumWindows(hwnd, lParam)
Protected Title${#MAX_PATH}
SendMessage_(hwnd, #WM_GETTEXT, #MAX_PATH, @Title$)
Debug Title$
ProcedureReturn #True
EndProcedure
EnumWindows_(@EnumWindows(), 0)
Re: Find specific window by title?
Posted: Wed Jul 04, 2018 9:11 pm
by Fangbeast
Lots of interesting approaches to be sure. Thanks all. Will be playing more as the weather gets warmer and the neurons start to flow better:)
Re: Find specific window by title?
Posted: Thu Jul 05, 2018 10:34 am
by Kwai chang caine
@MkSoft
Thanks master for the read of notepad content, very interesting

Re: Find specific window by title?
Posted: Mon Jul 09, 2018 8:19 am
by the.weavster
Could somebody please explain this syntax to me?:
I haven't seen curly brackets after a variable declaration before.
Re: Find specific window by title?
Posted: Mon Jul 09, 2018 8:25 am
by RSBasic
{} specifies the maximum length of the string,
Re: Find specific window by title?
Posted: Mon Jul 09, 2018 12:36 pm
by Shardik
the.weavster wrote:Could somebody please explain this syntax to me?:
I haven't seen curly brackets after a variable declaration before.