Jump to content

Get ClassNameNN by control ID


chenxu
 Share

Recommended Posts

Strange. When I do a search for ClassNameNN in the help file I only get this:

ControlGetFocus ( "title" [, "text"] )

Returns the ControlRef# of the control that has keyboard focus within a specified window.

Return Value

Success: Returns the ClassNameNN of the control that has keyboard focus within a specified window.

Failure: Returns "" (blank string) and sets @error to 1 if window is not found.

Edited by weaponx
Link to comment
Share on other sites

Strange. When I do a search for ClassNameNN in the help file I only get this:

ControlGetFocus ( "title" [, "text"] )

Returns the ControlRef# of the control that has keyboard focus within a specified window.

Return Value

Success: Returns the ClassNameNN of the control that has keyboard focus within a specified window.

Failure: Returns "" (blank string) and sets @error to 1 if window is not found.

what i want is get ClassNameNN by Control ID, which may be not the focusd control

Link to comment
Share on other sites

Hello chenxu,

Here's one way:

#include <WinApi.au3>

; Open Calculator in Scientific Mode
Run( "Calc.exe" )
WinWait( "Calculator" )
WinMenuSelectItem( "Calculator", "", "&View", "&Scientific" )

; Obtain the Control ID for the "5" button 
$Control_hWnd = ControlGetHandle( "Calculator", "", "[TEXT:5]" )
$Control_ID = GetControlID( $Control_hWnd )

; Using the Control ID, obtain the ClassNameNN pattern
$Control_ClassNameNN = GetClassNameNN_FromControlID( "Calculator", "", $Control_ID )
ConsoleWrite( StringFormat( 'Control ID [%s] = "%s"', $Control_ID, $Control_ClassNameNN )& @CRLF)

Exit


Func GetClassNameNN_FromControlID( $wndTitle, $winText, $id )
    ; Derive the Window Class for the given Control ID  
    Local $hWnd = ControlGetHandle( $wndTitle, $winText, $id )
    Local $ClassName = _WinAPI_GetClassName( $hWnd )

    ; Get an array of classes associated with the same window
    Local $ClassList = StringSplit( WinGetClassList( $wndTitle, $winText), @LF )
    Local $ClassCount = 0

    ; Walk the array. 
    For $i = 1 to $ClassList[0]
        If $ClassList[$i] = $ClassName Then
            ; If we encounter the same classname, increment the class count
            $ClassCount += 1
            
            ; Obtain the Control ID using the ClassNameNN syntax
            $Test_ClassNameNN = $ClassName & $ClassCount
            $Test_hWnd = ControlGetHandle( $wndTitle, $winText, $Test_ClassNameNN )
            $Test_ID = GetControlID( $Test_hWnd )
            
            ; Test if the ID's are the same
            If $id = $Test_ID then Return $Test_ClassNameNN
        EndIf 
    Next
EndFunc 


Func GetControlID( $hwnd)
    Local $ID = DllCall('user32.dll', 'int', 'GetDlgCtrlID', 'hwnd', $hwnd)
    if IsArray ($ID) Then Return $ID[0]
    Return 0    
EndFunc

I see PsaltyDS is on the case as well; expect a better solution than mine! :)

Zach...

Edited by zfisherdrums
Link to comment
Share on other sites

what i want is get ClassNameNN by Control ID, which may be not the focusd control

You can get the handle with ControlGetHandle(), and then get the class with _WinAPI_GetClassName(). But the class is returned without the NN instance number part (ClassNameNN appears to be an artificial AutoIt construct that the actual Windows APIs don't know anything about; Windows uses handles for all). So how do you get the instance number to assemble the full ClassNameNN... good question!

Can you work with just the handle? That would be best. Here's a demo getting the ClassName, you're on your own for the NN part:

#include <WinAPI.au3>

Run("notepad.exe")
WinWait("Untitled - Notepad")
$hEdit1 = ControlGetHandle("Untitled - Notepad", "", 15) ; Control ID 15 = Edit1
$sEdit1 = _WinAPI_GetClassName($hEdit1)
ConsoleWrite("Debug: $sEdit1 = " & $sEdit1 & @LF)oÝ÷ ØGb´¬¶Z(¦Øk¢è!º{b*.~)Ýjf­r§§vW·Ü:!ßx-Ö®¶­sb6æ6ÇVFRfÇCµväæS2fwC° ¥'VâgV÷C¶æ÷FWBæWRgV÷C²¥våvBgV÷CµVçFFÆVBÒæ÷FWBgV÷C²¢b33c¶7G&ÄBÒR²6öçG&öÂBRÒVFC¢b33c¶VFCÒ6öçG&öÄvWDæFÆRgV÷CµVçFFÆVBÒæ÷FWBgV÷C²ÂgV÷C²gV÷C²Âb33c¶7G&ÄB¢b33c·4VFCÒõväôvWD6Æ74æÖRb33c¶VFC¤6öç6öÆUw&FRgV÷C´FV'Vs¢b33c·4VFCÒgV÷C²fײb33c·4VFCfײÄb¢b33c´VFD6Æ74æÖTäâÒgV÷C²gV÷C°¤f÷"b33c¶âÒFò bb33c¶VFCÒ6öçG&öÄvWDæFÆRgV÷CµVçFFÆVBÒæ÷FWBgV÷C²ÂgV÷C²gV÷C²Âb33c·4VFCfײb33c¶âFVà b33c´VFD6Æ74æÖTäâÒb33c·4VFCfײb33c¶à WDÆö÷ VæD`¤æW@ ¤bb33c´VFD6Æ74æÖTäâÒgV÷C²gV÷C²FVà 6öç6öÆUw&FRgV÷C´FV'Vs¢fÆVBFòvWB6Æ74æÖTäâgV÷C²fײÄb¤VÇ6P 6öç6öÆUw&FRgV÷C´FV'Vs¢b33c´VFD6Æ74æÖTäâÒgV÷C²fײb33c´VFD6Æ74æÖTäâfײÄb¤VæD`

Thanks to zfisherdrums for the hint.

:)

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Thanks to zfisherdrums for the hint.

I swear, one day I'm going to write something that I won't have to re-write because you tossed out something better. Until then, nice one! :)

Zach...

Edited by zfisherdrums
Link to comment
Share on other sites

Thanks all.

I downloaded a WinAPI.au3, but I still can not run the example, did I download a wrong WinAPI.au3? Can post a correct one here for me, please? Thanks

Its part of the new production version of AutoIt 3.2.10.0

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...