Find specific window by title?

Just starting out? Need help? Post your questions and find answers here.
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Find specific window by title?

Post 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
  
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
User avatar
RSBasic
Moderator
Moderator
Posts: 1228
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: Find specific window by title?

Post 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
Image
Image
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Find specific window by title?

Post 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?
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
User avatar
RSBasic
Moderator
Moderator
Posts: 1228
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: Find specific window by title?

Post 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).
Image
Image
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4953
Joined: Sun Apr 12, 2009 6:27 am

Re: Find specific window by title?

Post 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
Egypt my love
MrMat
Enthusiast
Enthusiast
Posts: 762
Joined: Sun Sep 05, 2004 6:27 am
Location: England

Re: Find specific window by title?

Post 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).
Mat
User avatar
mk-soft
Always Here
Always Here
Posts: 6240
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Find specific window by title?

Post 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
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
RSBasic
Moderator
Moderator
Posts: 1228
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: Find specific window by title?

Post 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)
Image
Image
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Find specific window by title?

Post 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:)
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Find specific window by title?

Post by Kwai chang caine »

@MkSoft
Thanks master for the read of notepad content, very interesting 8)
ImageThe happiness is a road...
Not a destination
User avatar
the.weavster
Addict
Addict
Posts: 1577
Joined: Thu Jul 03, 2003 6:53 pm
Location: England

Re: Find specific window by title?

Post by the.weavster »

Could somebody please explain this syntax to me?:

Code: Select all

Protected Title${#MAX_PATH}
I haven't seen curly brackets after a variable declaration before.
User avatar
RSBasic
Moderator
Moderator
Posts: 1228
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: Find specific window by title?

Post by RSBasic »

{} specifies the maximum length of the string,
Image
Image
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Find specific window by title?

Post by Shardik »

the.weavster wrote:Could somebody please explain this syntax to me?:

Code: Select all

Protected Title${#MAX_PATH}
I haven't seen curly brackets after a variable declaration before.
[color=#0040FF]PureBasic documentation about variables and types[/color] wrote:

Code: Select all

    Name        Extension    Memory consumption     Range
.
.
Fixed String    .s{Length}   string length        unlimited
Post Reply