Sign in to follow this  
Followers 0
mattschinkel

Get ControlID from focused control

8 posts in this topic

#1 ·  Posted (edited)

When creating a button, $ctrlButton1 seems to hold an integer value (controlID??), but ControlGetFocus($hGUI) holds a string CLASSNN value.

See the following code, I want to get back the integer value(controlID) from the currently selected control. I do not want to use CLASSNN.

The tooltip at the bottom of the code does not work. How can I fix it?

I already looked at ControlCommand & ControlGetFocus

#include <GuiConstantsEx.au3>


$hGUI = GUICreate("MyGUI", 300, 300)
$ctrlButton1 = GUICtrlCreateButton("Button1", 100, 150, 100, 30)
$ctrlButton2 = GUICtrlCreateButton("Button2", 100, 250, 100, 30)
GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $ctrlButton1
            ToolTip ("button 1 pressed")
            Sleep (500)
        Case $ctrlButton2
            ToolTip ("button 2 pressed")
            Sleep (500)
        EndSwitch
        
    $a = ControlGetFocus($hGUI)
    If $a = $ctrlButton1 Then
        ToolTip ("button1 is selected")
    ElseIf $a = $ctrlButton2 Then
        ToolTip ("button2 is selected")
    Else 
        ToolTip ("")
    EndIf
    
WEnd

Thanks,

Matt.

Edited by mattschinkel

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

AutoIt accepts the integer Control ID, the ClassNameNN, the control handle, or an advanced specification (i.e. "[CLASS:Button; INSTANCE:1]"). So you can take the ClassNameNN and use it directly for the control ID in most AutoIt functions. If you really must find the integer ID, get the handle first and pass it to _WinAPI_GetDlgCtrlID().

:D

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

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

When creating a button, $ctrlButton1 seems to hold an integer value (controlID??), but ControlGetFocus($hGUI) holds a string CLASSNN value.

See the following code, I want to get back the integer value(controlID) from the currently selected control. I do not want to use CLASSNN.

The tooltip at the bottom of the code does not work. How can I fix it?

I already looked at ControlCommand & ControlGetFocus

#include <GuiConstantsEx.au3>


$hGUI = GUICreate("MyGUI", 300, 300)
$ctrlButton1 = GUICtrlCreateButton("Button1", 100, 150, 100, 30)
$ctrlButton2 = GUICtrlCreateButton("Button2", 100, 250, 100, 30)
GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $ctrlButton1
            ToolTip ("button 1 pressed")
            Sleep (500)
        Case $ctrlButton2
            ToolTip ("button 2 pressed")
            Sleep (500)
        EndSwitch
        
    $a = ControlGetFocus($hGUI)
    If $a = $ctrlButton1 Then
        ToolTip ("button1 is selected")
    ElseIf $a = $ctrlButton1 Then
        ToolTip ("button2 is selected")
    Else 
        ToolTip ("")
    EndIf
    
WEnd

Thanks,

Matt.

#include <GuiConstantsEx.au3>


$hGUI = GUICreate("MyGUI", 300, 300)
$ctrlButton1 = GUICtrlCreateButton("Button1", 100, 150, 100, 30)
$ctrlButton2 = GUICtrlCreateButton("Button2", 100, 250, 100, 30)
GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $ctrlButton1
            ToolTip ("button 1 pressed")
            Sleep (500)
        Case $ctrlButton2
            ToolTip ("button 2 pressed")
            Sleep (500)
        EndSwitch

    $a = GUIGetCursorInfo()
    if IsArray($a) Then
    If $a[4] = $ctrlButton1 Then
        ToolTip ("button1 is selected")
    ElseIf $a[4] = $ctrlButton2 Then
        ToolTip ("button2 is selected")
    Else
        ToolTip ("")
    EndIf
    EndIf

WEnd

Or

#include <GuiConstantsEx.au3>
#Include <WinAPI.au3>

$hGUI = GUICreate("MyGUI", 300, 300)
$ctrlButton1 = GUICtrlCreateButton("Button1", 100, 150, 100, 30)
$ctrlButton2 = GUICtrlCreateButton("Button2", 100, 250, 100, 30)
GUISetState()
$handle1 = ControlGetHandle ($hGUI,"",$ctrlButton1)
$handle2 = ControlGetHandle ($hGUI,"",$ctrlButton2)
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $ctrlButton1
            ToolTip ("button 1 pressed")
            Sleep (500)
        Case $ctrlButton2
            ToolTip ("button 2 pressed")
            Sleep (500)
        EndSwitch

    $a = ControlGetFocus("")
    $handle = ControlGetHandle ($hGUI,"",$a)
    If $handle = $handle1 Then
        ToolTip ("button1 is selected")
    ElseIf $handle = $handle2 Then
        ToolTip ("button2 is selected")
    Else
    ToolTip ("")
    EndIf

WEnd
Edited by wolf9228

صرح السماء كان هنا

 

Share this post


Link to post
Share on other sites

How about just this, which works on non-AutoIt GUIs for which you know the ClassNameNN, but not the control ID. It also doesn't require the window to be visible, in focus, or have the mouse over it:

#include <GuiConstantsEx.au3>
#include <WinAPI.au3>

$sMsg = ""
$iPID = Run("Notepad.exe")
WinWait("Untitled - Notepad")
$hNotepad = WinGetHandle("Untitled - Notepad")
$sMsg &= "$hNotepad = " & $hNotepad & @CRLF
$hEdit1 = ControlGetHandle($hNotepad, "", "Edit1") ; Get handle using known ClassNameNN
$sMsg &= "$hEdit1 = " & $hEdit1 & @CRLF
$idEdit1 = _WinAPI_GetDlgCtrlID($hEdit1) ; Get control ID from handle
$sMsg &= "$idEdit1 = " & $idEdit1 & @CRLF
ControlSetText($hNotepad, "", $idEdit1, $sMsg)

:D


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

Share this post


Link to post
Share on other sites

Well, this way works good for buttons:

#include <GuiConstantsEx.au3>
#include <WinAPI.au3>

$hGUI = GUICreate("MyGUI", 300, 300)
$ctrlButton1 = GUICtrlCreateButton("Button1", 100, 150, 100, 30)
$ctrlButton2 = GUICtrlCreateButton("Button2", 100, 250, 100, 30)
GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $ctrlButton1
            ToolTip ("button 1 pressed")
            Sleep (500)
        Case $ctrlButton2
            ToolTip ("button 2 pressed")
            Sleep (500)
        EndSwitch
        
    $a = ControlGetHandle ($hGUI, "", ControlGetFocus($hGUI))
    $a = _WinAPI_GetDlgCtrlID ( $a)
    If $a = $ctrlButton1 Then
        ToolTip ("button1 is selected")
    ElseIf $a = $ctrlButton2 Then
        ToolTip ("button2 is selected")
    Else 
        ToolTip ("")
    EndIf
    
WEnd

But, It does not work for combo boxes:

#include <GuiConstantsEx.au3>
#include <WinAPI.au3>

$hGUI = GUICreate("MyGUI", 300, 300)
$ctrlCombo1 = guictrlcreatecombo("Combo1", 100, 150, 100, 30)
$ctrlCombo2 = guictrlcreatecombo("Combo2", 100, 250, 100, 30)
GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $ctrlCombo1
            ToolTip ("Combo 1 pressed")
            Sleep (500)
        Case $ctrlCombo2
            ToolTip ("Combo 2 pressed")
            Sleep (500)
        EndSwitch
        
    $a = ControlGetHandle ($hGUI, "", ControlGetFocus($hGUI))
    $a = _WinAPI_GetDlgCtrlID ( $a)
    If $a = $ctrlCombo1 Then
        ToolTip ("Combo1 is selected")
    ElseIf $a = $ctrlCombo2 Then
        ToolTip ("Combo2 is selected")
    Else 
        ToolTip ("")
    EndIf
    
WEnd

So how do I do this with combo boxes?

Matt.

Share this post


Link to post
Share on other sites

It works fine on combo boxes:

$a = ControlGetHandle($hGUI, "", "[CLASS:ComboBox; INSTANCE:1]")
ConsoleWrite("$a = " & $a & @LF)
$a = _WinAPI_GetDlgCtrlID($a)
ConsoleWrite("$a = " & $a & @LF)

The focus in your code is on the attached Edit control, not the combo box. Check out your GUI with AU3Info.exe to see it.

:D


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

Share this post


Link to post
Share on other sites

It works fine on combo boxes:

$a = ControlGetHandle($hGUI, "", "[CLASS:ComboBox; INSTANCE:1]")
ConsoleWrite("$a = " & $a & @LF)
$a = _WinAPI_GetDlgCtrlID($a)
ConsoleWrite("$a = " & $a & @LF)

The focus in your code is on the attached Edit control, not the combo box. Check out your GUI with AU3Info.exe to see it.

:D

I should not have to use AU3Info.exe to find a control ID of a control I created myself. I would like my code to find the control ID, I have a reason for this. I really need this to work on a combo box's edit control. I've been trying for days now.

Thanks,

Matt.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

I should not have to use AU3Info.exe to find a control ID of a control I created myself. I would like my code to find the control ID, I have a reason for this. I really need this to work on a combo box's edit control. I've been trying for days now.

Thanks,

Matt.

You don't have to use AU3Info. You simply catch the returned control ID when you create it. You created a combo box and got the ID for the combo box. But you didn't create the Edit attached to the combo box, the Windows API for a combo box did.

I was suggesting you use AU3Info to look at the two edit boxes attached to the two combo boxes you created and notice they have the same ID (1001) even though they have different ClassNameNN. That's because their parent is the combo box, not your GUI. They do, however, have unique handles, and that's the way to interface with them.

You can use _GuiCtrlCombobox_GetComboInfo() to get the handle to the edit control of the combo. Then use that handle to work with them:

#include <GuiConstantsEx.au3>
#include <WinAPI.au3>
#include <GuiComboBox.au3>

Global $hGUI, $tCombo1, $tCombo2

$hGUI = GUICreate("MyGUI", 360, 300)

$ctrlLabel1 = GUICtrlCreateLabel("", 20, 20, 320, 60)

$ctrlCombo1 = GUICtrlCreateCombo("Combo1", 130, 100, 100, 30)
$hCombo1 = ControlGetHandle($hGUI, "", $ctrlCombo1)
_GUICtrlComboBox_GetComboBoxInfo($hCombo1, $tCombo1)
$hEdit1 = DllStructGetData($tCombo1, "hEdit")
For $n = 1 To 5
    GUICtrlSetData($ctrlCombo1, "Combo1-" & $n)
Next

$ctrlCombo2 = GUICtrlCreateCombo("Combo2", 130, 150, 100, 30)
$hCombo2 = ControlGetHandle($hGUI, "", $ctrlCombo2)
_GUICtrlComboBox_GetComboBoxInfo($hCombo2, $tCombo2)
$hEdit2 = DllStructGetData($tCombo2, "hEdit")
For $n = 1 To 5
    GUICtrlSetData($ctrlCombo2, "Combo2-" & $n)
Next

$sMsg = "ComboBox1:" & @CRLF & _
        @TAB & "ID = " & $ctrlCombo1 & "; Hwnd = " & $hCombo1 & "; Edit1 = " & $hEdit1 & @CRLF & _
        "ComboBox2:" & @CRLF & _
        @TAB & "ID = " & $ctrlCombo2 & "; Hwnd = " & $hCombo2 & "; Edit1 = " & $hEdit2
ControlSetText($hGUI, "", $ctrlLabel1, $sMsg)

$ctrlLabel2 = GUICtrlCreateLabel("", 20, 200, 320, 80)

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case Else
            $sFocus = ControlGetFocus($hGUI)
            $hFocus = ControlGetHandle($hGUI, "", $sFocus)
            $ctrlFocus = _WinAPI_GetDlgCtrlID($hFocus)
            $sMsg = "Focus:" & @CRLF & _
                    @TAB & "ClassNameNN = " & $sFocus & @CRLF & _
                    @TAB & "Hwnd = " & $hFocus & @CRLF & _
                    @TAB & "ID = " & $ctrlFocus
            If $sMsg <> ControlGetText($hGUI, "", $ctrlLabel2) Then ControlSetText($hGUI, "", $ctrlLabel2, $sMsg)
    EndSwitch
WEnd

:D

P.S. If you need to know which parent combo box owns the Edit in focus, pass the handle of the Edit to _WinAPI_GetParent(). Compare the parent's handle to the combo box's handle:

ConsoleWrite("Edit1 parent = " & _WinAPI_GetParent($hEdit1) & @LF)
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

Share this post


Link to post
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
Sign in to follow this  
Followers 0