AutoHotkey Community

It is currently May 25th, 2012, 8:19 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: June 14th, 2007, 9:54 am 
Offline

Joined: December 1st, 2006, 9:27 am
Posts: 460
This will return the class name and number of a control if provided the control's id or the text/var. I updated it to make it easier to use. Now there are only two arguments: ID/Text/Var and gui number (which defaults to 1.)

Image

Call with:
Code:
ret := getClassNN(butOk)   ;use the ID
ret1 := getClassNN("Radio1")   ;use the Caption
ret2 := getClassNN(Check1)   ;use the text/var


Here's the function:
Code:
getClassNN(c, g=1)   ;c = text/var,Hwnd   ;g = gui number (default=1)
{
   Gui,%g%:+LastFound
   guiID := WinExist()   ;get the id for the gui
   WinGet, List_controls, ControlList
   StringReplace, List_controls, List_controls, `n, `,, All
   ;if id supplied do nothing special
   IfNotInString, c, %List_controls%   ;must be the text/var - get the ID
   {   mm := A_TitleMatchMode
      SetTitleMatchMode, 3   ;exact match only
      ControlGet, o, hWnd,, %c%   ;get the id
      SetTitleMatchMode, %mm%   ;restore previous setting
      If(o)   ;found match
      {   c := o   ;set sought-after id
      }
   }
   Loop, Parse, List_controls, CSV
   {   ControlGet, o, hWnd,, %A_LoopField%   ;get the id of current classnn
      If(o = c)   ;if it is the one we want
      {   Return A_Loopfield   ;return the classnn
      }
   }
}


Example:
Code:
gui,add,Checkbox, vCheck1, Checkbox
gui,add,Radio, xp y+5,Radio1
gui,add,Radio, x+ yp,Radio2
gui,add,button,HwndbutOk xm+10 y+5 section,Ok   ;add first button
gui,add,button,HwndbutCancel x+ yp,cancel         ;add second button
gui,show

ret := getClassNN(butOk)   ;use the ID
ret1 := getClassNN("Radio1")   ;use the Caption
ret2 := getClassNN(Check1)   ;use the text/var

MsgBox, Ok Button:`t%ret%`nRadio Button1:`t%ret1%`nCheckbox:`t%ret2%

Return

guiClose:
   ExitApp
Return




getClassNN(c, g=1)   ;c = text/var,Hwnd   ;g = gui number (default=1)
{
   Gui,%g%:+LastFound
   guiID := WinExist()   ;get the id for the gui
   WinGet, List_controls, ControlList
   StringReplace, List_controls, List_controls, `n, `,, All
   ;if id supplied do nothing special
   IfNotInString, c, %List_controls%   ;must be the text/var - get the ID
   {   mm := A_TitleMatchMode
      SetTitleMatchMode, 3   ;exact match only
      ControlGet, o, hWnd,, %c%   ;get the id
      SetTitleMatchMode, %mm%   ;restore previous setting
      If(o)   ;found match
      {   c := o   ;set sought-after id
      }
   }
   Loop, Parse, List_controls, CSV
   {   ControlGet, o, hWnd,, %A_LoopField%   ;get the id of current classnn
      If(o = c)   ;if it is the one we want
      {   Return A_Loopfield   ;return the classnn
      }
   }
}

_________________
Image


Last edited by Micahs on April 11th, 2009, 1:07 pm, edited 4 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 11th, 2007, 2:34 am 
Offline

Joined: December 1st, 2006, 9:27 am
Posts: 460
I updated the script. Now it only loops through the existing controls on the gui. This should be faster and more efficient.
One issue still exists: If more than one control has the same caption or variable contents, it will return the first one it sees.
Using the Hwnd option when creating the control and passing that removes this limitation, though.

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 11th, 2009, 1:01 pm 
Offline

Joined: December 1st, 2006, 9:27 am
Posts: 460
Updated

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 11th, 2009, 3:43 pm 
Offline

Joined: February 7th, 2009, 11:28 pm
Posts: 384
thanks. i'm sure this will become handy in some situations.

_________________
Hardware: 1.8 GHz laptop with 4 GB ram, Windows XP/SP3
Software: Prevx, Privatefirewall, KeyScrambler.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 7th, 2009, 5:27 pm 
Offline

Joined: July 4th, 2009, 6:53 pm
Posts: 36
Thank you for your incredibly useful code. I am incurring issues with Hwnd & ComboBox controls. They are showing up correctly in your function, but Window Spy lists them as Edit controls.

This prevents me from using your function on GUIs with ComboBox controls.

Example:
Code:
gui,add,Checkbox, vCheck1, Checkbox
gui,add,Radio, xp y+5,Radio1
gui,add,Radio, x+ yp,Radio2
gui,add,button,HwndbutOk xm+10 y+5 section,Ok   ;add first button
gui,add,button,HwndbutCancel x+ yp,cancel         ;add second button
gui,add,ComboBox,Hwndcombo1, Yes||No|
gui,show

ret := getClassNN(butOk)   ;use the ID
ret1 := getClassNN("Radio1")   ;use the Caption
ret2 := getClassNN(Check1)   ;use the text/var
ret3 := getClassNN(combo1)   ;use the text/var

MsgBox, Ok Button:`t%ret%`nRadio Button1:`t%ret1%`nCheckbox:`t%ret2%`nCombobox:`t%ret3%

Return

guiClose:
   ExitApp
Return

In AutoIt Window Spy, the ControlNN for the ComboBox is "Edit1".


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 7th, 2009, 7:09 pm 
Offline

Joined: December 1st, 2006, 9:27 am
Posts: 460
The combobox is a little different. If you move the mouse over the "down arrow box" part to the right, it shows the correct classnn instead of the "edit1" classnn. The way to test that it actually works is to use this:
Code:
GuiControlGet, CBtxt, , %ret3%
MsgBox, %CBtxt%

to get and use the classnn of the combobox. This will accurately display the selected text in the combobox. It really does work!

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Wicked and 4 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group