Getting handle from windows with same name

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
rullbandspelare
Pro Scripter
Posts: 149
Joined: Tue Mar 23, 2004 9:11 pm

Getting handle from windows with same name

Post by rullbandspelare » Thu May 18, 2017 7:55 pm

I have two windows that have exactly the same name when the application starts.
But only one of them have the info that I need.

This is how I do now to get the two handles for the different windows.
Is there a better way to get the handles for the twoTopScan windows


running code bellow give me theese two.
TooScan,919344,TfrmShell
TopScan,2492356,TApplication

Let>WIN_USEHANDLE=1
GetWindowList>allWindows
Separate>allWindows,CRLF,wins
If>wins_count>0
Let>k=0
Repeat>k
Let>k=k+1
Let>this_window=wins_%k%
GetWindowNames>this_window,strTitle,strClass
MessageModal>%strTitle%,%this_window%,%strClass%
Until>k=wins_count

conjure
Pro Scripter
Posts: 64
Joined: Thu Jan 12, 2012 3:05 pm

Re: Getting handle from windows with same name

Post by conjure » Sat May 20, 2017 1:12 pm

from another post

Code: Select all


Label>ask_for_instances
Input>instances,Enter the number of Notepad windows you'd like to control (minimum 2 or enter 0 to Exit):,5

//trim leading and trailing spaces
Trim>instances,instances

Length>instances,entered_instances_length
//if no input then ask_for_instances again
IF>entered_instances_length=0,ask_for_instances

//next line not required here but a good habit to get in to if you will run RegEx> more than once 
Let>matches_1=
//let's look for any char that is NOT a digit
Let>pattern=[^0123456789]
RegEx>pattern,instances,0,matches,num,0
//if we found a non-digit then entry was invalid so ask_for_instances again
If>num>0,ask_for_instances

//we want 2 minimum to demo so if less, ask_for_instances again
If>instances<2,ask_for_instances

//an entry of 0 indicates user wants to exit
If>instances=0,end


Let>instance_number=0
Repeat>instance_number
  Let>instance_number=instance_number+1

  Run>notepad.exe
  WaitWindowOpen>Untitled - Notepad

  //grab the window handle for this particular Notepad window
  Let>WIN_USEHANDLE=1
  GetActiveWindow>hwnd,X,Y
  Let>WIN_USEHANDLE=0

  //lets store that window handle in an array for later use
  Let>my_app_instance_handle_array_%instance_number%=hwnd

  //re-title this notepad window to make the next one easier to grab via window title
  LibFunc>User32,SetWindowTextA,r,hwnd,str:Untitled - Notepad - Instance %instance_number%
         
Until>instance_number,instances


//minimize all windows
Press RWinKey
Send>d
Release RWinKey

Wait>0.5

Label>ask_for_number
Input>number,Now we can SetFocus> to any of the Notepad windows we opened via their window handle rather than trying to use the Window Title which may change depending on what file is loaded. What instance would you now like to control? (1 - %instances% or enter 0 to Exit):

//trim leading and trailing spaces
Trim>number,number

Length>number,entered_number_length
//if no input then ask_for_number again
IF>entered_number_length=0,ask_for_number

//next line not required here but a good habit to get into if you will run RegEx> more than once 
Let>matches_1=
//let's look for any char that is NOT a digit
Let>pattern=[^0123456789]
RegEx>pattern,number,0,matches,num,0
//if we found a non-digit then entry was invalid so ask_for_number again
If>num>0,ask_for_number

//negative numbers don't make sense here so...
If>number<0,ask_for_number

//we can't ask for an instance higher than we have
If>number>instances,ask_for_number

//an entry of 0 indicates user wants to exit
If>instances=0,end


//OK, user asked for a valid instance so SetFocus> to it using its handle stored in our array

//set to 1 to use window handles instead of titles 
Let>WIN_USEHANDLE=1

SetFocus>my_app_instance_handle_array_%number%


Label>end


wvanellis
Newbie
Posts: 3
Joined: Fri May 19, 2017 7:41 pm

Re: Getting handle from windows with same name

Post by wvanellis » Sat May 20, 2017 1:27 pm

My apologies... I posted to the incorrect post

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts