AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

retrieve ClassNN from control id or text (Version 2)

 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
Micahs



Joined: 01 Dec 2006
Posts: 460

PostPosted: Thu Jun 14, 2007 8:54 am    Post subject: retrieve ClassNN from control id or text (Version 2) Reply with quote

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.)



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
      }
   }
}

_________________


Last edited by Micahs on Sat Apr 11, 2009 12:07 pm; edited 4 times in total
Back to top
View user's profile Send private message
Micahs



Joined: 01 Dec 2006
Posts: 460

PostPosted: Thu Oct 11, 2007 1:34 am    Post subject: Reply with quote

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.
_________________
Back to top
View user's profile Send private message
Micahs



Joined: 01 Dec 2006
Posts: 460

PostPosted: Sat Apr 11, 2009 12:01 pm    Post subject: Reply with quote

Updated
_________________
Back to top
View user's profile Send private message
pajenn



Joined: 07 Feb 2009
Posts: 384

PostPosted: Sat Apr 11, 2009 2:43 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
ghee22



Joined: 04 Jul 2009
Posts: 36

PostPosted: Sat Nov 07, 2009 4:27 pm    Post subject: Reply with quote

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".
Back to top
View user's profile Send private message
Micahs



Joined: 01 Dec 2006
Posts: 460

PostPosted: Sat Nov 07, 2009 6:09 pm    Post subject: Reply with quote

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!
_________________
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group