Sign in to follow this  
Followers 0

_GetControlID()

10 posts in this topic

Posted (edited)

For those who like to create Functions() and have always wondered "How can I place my Function() underneath a new Control and only specify -1 and not the $Variable of the Control" e.g.

 

GUICtrlCreateLabel("ExampleID", 0, 0)
_Function(-1, "New Example")

; How It's Normally Done With Custom Functions.
$Label = GUICtrlCreateLabel("ExampleID", 0, 0)
_Function($Label, "New Example")
Then maybe this will help you understand how to achieve this. I have included 2 Versions of the basic Function, one requires WinAPI.au3 and the other doesn't.

Function:

#include <WinAPI.au3>

Func _GetControlID($iControlID = Default)
    If $iControlID = Default Then $iControlID = -1
    $iControlID = Int($iControlID)
    Return _WinAPI_GetDlgCtrlID(GUICtrlGetHandle($iControlID))
EndFunc   ;==>_GetControlID
How it works is very simple, it takes the ControlHandle of the last ControlID and then using _WinAPI_GetDlgCtrlID() converts this back into the ControlID. I have included an Example of how to take the basic Version and expand it. Now before people say "Well why don't you put GUICtrlSetBkColor() & GUICtrlSetData() underneath the Label," as much as this is true it's only showing how to do it simply without getting to complex.

Example 1:

#include <GUIConstantsEx.au3>
#include <WinAPI.au3>

Example()

Func Example()
    Local $hGUI = GUICreate('_GetControlID() Example 1')
    GUISetState(@SW_SHOW, $hGUI)

    GUICtrlCreateLabel('Example Label', 10, 10, 150, 20)
    Sleep(2000) ; To Show The Label Before Changing.
    SomeFunc(-1, 'New Example Label') ; Uses the last control id created.

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
        EndSwitch
    WEnd

    GUIDelete($hGUI)
EndFunc   ;==>Example

Func SomeFunc($iControlID = -1, $sData = '')
    $iControlID = _WinAPI_GetDlgCtrlID(GUICtrlGetHandle($iControlID))
    If $iControlID Then
        GUICtrlSetData($iControlID, $sData)
    EndIf
    Return $iControlID > 0
EndFunc   ;==>SomeFunc
Example 2: >>>

Example 3: >>>

NOTE: Of course people might know how to do this already but I haven't seen it documented before in the General & Example Sub-Forums.

Edited by guinness

Share this post


Link to post
Share on other sites



Posted

I have written something similar

SmOke_N has two outstanding that you may be interested in too.

_CtrlGetByPos

and

_WinGetCtrlInfo()

Share this post


Link to post
Share on other sites

Posted

Hey cheers for that, that will go into my "Function Folder." But I do want to point out that my post is not so much about getting information about the Control Handle, but more about improving custom Functions that users create. For example if the code was placed at the top of then it would mean that _SetBitmapToCtrl() could be placed underneath the Control (.e.g. Label) that the Image would be drawn onto.

Example:

GUICtrlCreateLabel("", 10, 10)
_SetBitmapToCtrl(-1,"Example.png")

but for now the Label has to be assigned a variable and passed to the Function.

$Label = GUICtrlCreateLabel("", 10, 10)
_SetBitmapToCtrl($Label,"Example.png")

Share this post


Link to post
Share on other sites

Posted

It works if you just pass -1 as the first parameter in the function as well ........

I'm sure your code has other uses, but it's not this one.

_Main()

Func _Main()
    GUICreate("_GetControlID() Example")
    GUISetState(@SW_SHOW)

    GUICtrlCreateLabel("Example Label", 10, 10, 150, 20)
    Sleep(2000) ; To Show The Label Before Changing.
    _Function(-1, "New Example Label")

    While 1
        If GUIGetMsg() = -3 Then ExitLoop
    WEnd
    GUIDelete()
EndFunc   ;==>_Main

Func _Function($gControlID = -1, $gText = "")
    GUICtrlSetBkColor($gControlID, 0xFF0000)
    GUICtrlSetData($gControlID, $gText)
EndFunc   ;==>_Function

Share this post


Link to post
Share on other sites

Posted (edited)

OK, good to know but, if it isn't an inbuilt Function (as in my simple example) that doesn't recognise -1 as the previous ControlID, then it becomes useful e.g.

Example 2:

#include <Array.au3>
#include <GUIConstantsEx.au3>
#include <WinAPI.au3>

Example()

Func Example()
    Local $hGUI = GUICreate('_GetControlID() Example 2')
    GUISetState(@SW_SHOW, $hGUI)

    Local $aArray[1][2] = [[0, 2]], _
            $iTop = 10
    For $i = 1 To 10
        GUICtrlCreateLabel('Example Label_' & $i, 5, $iTop, 150, 20)
        AddControl($aArray, Default) ; Use the last created controlid.
        $iTop += 25
    Next
    Local $iButton = GUICtrlCreateButton('Show Array', 5, 400 - 30, 65, 22.5)

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop

            Case $iButton
                _ArrayDisplay($aArray)

        EndSwitch
    WEnd
    GUIDelete($hGUI)
EndFunc   ;==>Example

Func AddControl(ByRef $aArray, $iControlID = Default)
    $iControlID = _GetControlID($iControlID)
    If $iControlID Then
        ; ReDim the array if required.
        If UBound($aArray) <= $aArray[0][0] + 1 Then
            ReDim $aArray[Ceiling(UBound($aArray) * 1.5)][$aArray[0][1]]
        EndIf
        $aArray[0][0] += 1
        $aArray[$aArray[0][0]][0] = $iControlID
        $aArray[$aArray[0][0]][1] = GUICtrlRead($iControlID)
    EndIf
    Return $iControlID > 0 ; Returns True if the controlid was retrieved.
EndFunc   ;==>AddControl

Func _GetControlID($iControlID = Default)
    If $iControlID = Default Then $iControlID = -1
    $iControlID = Int($iControlID)
    Return _WinAPI_GetDlgCtrlID(GUICtrlGetHandle($iControlID))
EndFunc   ;==>_GetControlID
I want feedback of course, especially on how I code. I have been learning by reading the forum and trying to solve problems by myself, hence why I have a limited presence on AutoIt. Edited by guinness

Share this post


Link to post
Share on other sites

Posted

3rd time lucky eh?

I did try at one point to see if I could find the handle to the last used GUI ( -1 ). Yashied came up with this:

Func _GUIGetCur ()
   Local $hLabel = GUICtrlCreateLabel ("", -99, -99, 1, 1), $aRet

   $aRet = DllCall ("User32.dll", "hwnd", "GetParent", "hwnd", GUICtrlGetHandle ($hLabel))
    If @Error Then Return SetError(@Error, 0 * GUICtrlDelete ($hLabel), 0)
   GUICtrlDelete ($hLabel)

    Return $aRet[0]
EndFunc ; ==> _GUIGetCur

Share this post


Link to post
Share on other sites

Posted (edited)

Wow, that 3 entires thingy was when my browser froze :x But as we say 3 is the magic number.

Interesting concept too, cheers Mat! It will go in my "Function Folder" :P I love little snippets like this.

Edited by guinness

Share this post


Link to post
Share on other sites

Posted (edited)

You can see in this Function the use of Edited by guinness

Share this post


Link to post
Share on other sites

Posted (edited)

I updated the examples, removing 'magic numbers' and the unnecessary use of Global variable in the second example.

Edited by guinness

Share this post


Link to post
Share on other sites

Posted

Fixed on an old bug where Default would be converted to -1 instead of 0 when casting as a double (using Number()). I have now fixed this in my code above.

1 person likes this

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

  • Recently Browsing   0 members

    No registered users viewing this page.