Jump to content

What ID corresponds to -1 in my custom function?


TimRude
 Share

Recommended Posts

With built-in GUI control functions, you can reference the last created control using -1 as the ID number, such as this snippet from the GUICtrlSetStyle help topic:

GUICtrlCreateLabel("my label which will split on several lines", 10, 20, 100, 100)
GUICtrlSetStyle(-1, $SS_RIGHT)

Likewise the same thing with the built-in Tray item functions, such as this snippet from the TrayItemSetOnEvent help topic:

TrayCreateItem("About")
TrayItemSetOnEvent(-1, "About")

If I want to write a custom function to work with a GUI control or a Tray item and accept -1 as a parameter for the ID number, how would I figure out which GUI control ID or Tray item ID I'm actually working with in my function?

For example:

; while creating GUI

GUICtrlCreateLabel("my label which will split on several lines", 10, 20, 100, 100)
GUICtrlSetStyle(-1, $SS_RIGHT)
_MyGUICtrlFunction(-1)

; while creating Tray items

TrayCreateItem("About")
TrayItemSetOnEvent(-1, "About")
_MyTrayItemFunction(-1)

Exit

Func _MyGUICtrlFunction($iID)
    If $iID = -1 Then
        ; somehow convert -1 to actual control ID of last created control
    EndIf
    ConsoleWrite("Control ID = " & $iID & @CRLF)
EndFunc

Func _MyTrayItemFunction($iID)
    If $iID = -1 Then
        ; somehow convert -1 to actual tray item ID of last created tray item
    EndIf
    ConsoleWrite("Tray Item ID = " & $iID & @CRLF)
EndFunc

 

Link to comment
Share on other sites

Yes, I am fully aware that I can assign a variable to the GUI Control ID or Tray Item ID and then pass that variable to my function directly.

What I'm trying to determine is if there's a way to emulate the built-in functions' ability to accept -1 as a the ID value and convert that to the actual ID of the last created control or tray item.

For instance, in the examples I cited suppose there was no need to remember the ID of the label control since it was never going to be referenced again in code beyond the GUI creation block. Likewise the 'About' tray item wasn't going to be referenced again in code beyond the GUI creation block so no real need to remember its item ID. Thus the handy shortcut of simply creating a control or tray item without an accompanying variable to remember its ID, and then addressing it in the next couple of calls as -1, before moving on to the next control or item to be created.

So I'm simply looking to see if there's a way to write a custom function that accepts a '-1' ID parameter in a manner consistent with the built-in functions.

Link to comment
Share on other sites

@TimRudethis could work concerning your _MyGUICtrlFunction() , based on help file example, topic GUICtrlCreateLabel :

#include <GUIConstantsEx.au3>
#include <WinAPIDlg.au3>

Example()

Func Example()
    GUICreate("My GUI")
    Local $iOldOpt = Opt("GUICoordMode", 2) ; 2 = cell positioning relative to current cell

    Local $iWidthCell = 70
    GUICtrlCreateLabel("Line 1 Cell 1", 10, 30, $iWidthCell) ; first cell 70 width
    GUICtrlCreateLabel("Line 2 Cell 1", -1, 0) ; next line
    GUICtrlCreateLabel("Line 3 Cell 2", 0, 0) ; next line and next cell
    GUICtrlCreateLabel("Line 3 Cell 3", 0, -1) ; next cell same line
    GUICtrlCreateLabel("Line 4 Cell 1", -3 * $iWidthCell, 0) ; next line Cell1

    _MyGUICtrlFunction(-1) ; <==============================

    GUISetState(@SW_SHOW)

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

    $iOldOpt = Opt("GUICoordMode", $iOldOpt)
EndFunc   ;==>Example

Func _MyGUICtrlFunction($iID)
    If $iID = -1 Then
        ; somehow convert -1 to actual control ID of last created control
        $iID = _WinAPI_GetDlgCtrlID(GUICtrlGetHandle(-1)) ; 7
        GUICtrlSetBkColor($iID, 0xFF0000) ; red
    EndIf
    ConsoleWrite("Control ID = " & $iID & @CRLF)
EndFunc   ;==>_MyGUICtrlFunction

 

Link to comment
Share on other sites

with data is like

; while creating GUI
#include <StaticConstants.au3>
 GUICreate("My GUICtrlRead") ;
Local $idLabel = GUICtrlCreateLabel("my label which will split on several lines", 10, 20, 100, 100)
GUICtrlSetStyle(-1, $SS_RIGHT)

_MyGUICtrlFunction()
_MyGUICtrlFunction($idLabel)

Exit

Func _MyGUICtrlFunction($iID = -1)
    If $iID = -1 Then
        Local $data = GUICtrlRead(-1)
    Else
        Local $data = GUICtrlRead($iID)
    EndIf
    ConsoleWrite("Control data = " & $data & @CRLF)
EndFunc

 

I know that I know nothing

Link to comment
Share on other sites

and now that I saw it

#include <StaticConstants.au3>
 GUICreate("My GUICtrlRead") ;
Local $idLabel = GUICtrlCreateLabel("my label which will split on several lines", 10, 20, 100, 100)
GUICtrlSetStyle(-1, $SS_RIGHT)

_MyGUICtrlFunction()
_MyGUICtrlFunction($idLabel)

Exit

Func _MyGUICtrlFunction($iID = -1)
    If $iID = -1 Then
        Local $data = GUICtrlGetHandle(-1)
    Else
        Local $data = GUICtrlGetHandle($iID)
    EndIf
    ConsoleWrite("Control id = " & $data & @CRLF)
EndFunc

 

I know that I know nothing

Link to comment
Share on other sites

21 hours ago, pixelsearch said:

@TimRudethis could work concerning your _MyGUICtrlFunction() , based on help file example, topic GUICtrlCreateLabel :

#include <WinAPIDlg.au3>

Func _MyGUICtrlFunction($iID)
    If $iID = -1 Then $iID = _WinAPI_GetDlgCtrlID(GUICtrlGetHandle(-1))
    ConsoleWrite("Control ID = " & $iID & @CRLF)
EndFunc   ;==>_MyGUICtrlFunction

 

@pixelsearch Yep, this appears to work, as long as the control is one that has a handle, and not one of these mentioned in the GUICtrlGetHandle help topic:

Quote

The following controls will not return a handle: GUICtrlCreateDummy(), GUICtrlCreateGraphic(), GUICtrlCreateObj(), GUICtrlCreateListViewItem() and GUICtrlCreateTabItem().

And for most purposes, that's probably sufficient. For those controls that don't have a handle, the function sets $iID to 0 (the failure return value for GUICtrlGetHandle).

Edited by TimRude
Link to comment
Share on other sites

20 hours ago, ioa747 said:

with second look if start-mend no needed

Func _MyGUICtrlFunction($iID = -1)
  Local $data = GUICtrlGetHandle($iID)
  ConsoleWrite("Control id = " & $data & @CRLF)
EndFunc

 

@ioa747 Close, but in that case you're returning the handle (hWnd) of the control, not its ID. Two very different things.

That's part of the road to the solution, but only gets you half-way there. See pixelsearch's function that pairs GUICtrlGetHandle with _WinAPI_GetDlgCtrlID for the other half.

Link to comment
Share on other sites

After some pondering (while in the shower), a method hit me for determining the actual Tray ID when -1 is passed.

This is based on the example for the TrayCreateItem function:

#NoTrayIcon
#include <AutoItConstants.au3>
#include <MsgBoxConstants.au3>
#include <StringConstants.au3>
#include <TrayConstants.au3> ; Required for the $TRAY_CHECKED and $TRAY_ICONSTATE_SHOW constants.
#include <GuiMenu.au3> ; Required for the _GUICtrlMenu functions

Opt("TrayMenuMode", 3) ; The default tray menu items will not be shown and items are not checked when selected. These are options 1 and 2 for TrayMenuMode.

Example()

Func Example()
    ; Create a tray item with the radio item parameter selected.
    Local $idTemp = TrayCreateItem("Radio 1", -1, -1, $TRAY_ITEM_RADIO)
        ConsoleWrite("Actual Tray Item ID = " & $idTemp & @CRLF)
        _MyTrayItemFunction(-1)
        TrayItemSetState(-1, $TRAY_CHECKED)
    Local $idTemp = TrayCreateItem("Radio 2", -1, -1, $TRAY_ITEM_RADIO)
        ConsoleWrite("Actual Tray Item ID = " & $idTemp & @CRLF)
        _MyTrayItemFunction(-1)
    Local $idTemp = TrayCreateItem("Radio 3", -1, -1, $TRAY_ITEM_RADIO)
        ConsoleWrite("Actual Tray Item ID = " & $idTemp & @CRLF)
        _MyTrayItemFunction(-1)

    Local $idTemp = TrayCreateItem("") ; Create a separator line.
        ConsoleWrite("Actual Tray Item ID = " & $idTemp & @CRLF)
        _MyTrayItemFunction(-1)

    Local $idAbout = TrayCreateItem("About")
        ConsoleWrite("Actual Tray Item ID = " & $idAbout & @CRLF)
        _MyTrayItemFunction(-1)
    
    Local $idTemp = TrayCreateItem("") ; Create a separator line.
        ConsoleWrite("Actual Tray Item ID = " & $idTemp & @CRLF)
        _MyTrayItemFunction(-1)

    Local $idExit = TrayCreateItem("Exit")
        ConsoleWrite("Actual Tray Item ID = " & $idExit & @CRLF)
        _MyTrayItemFunction(-1)

    TraySetState($TRAY_ICONSTATE_SHOW) ; Show the tray menu.

    While 1
        Switch TrayGetMsg()
            Case $idAbout ; Display a message box about the AutoIt version and installation path of the AutoIt executable.
                MsgBox($MB_SYSTEMMODAL, "", "AutoIt tray menu example." & @CRLF & @CRLF & _
                        "Version: " & @AutoItVersion & @CRLF & _
                        "Install Path: " & StringLeft(@AutoItExe, StringInStr(@AutoItExe, "\", $STR_NOCASESENSEBASIC, -1) - 1)) ; Find the folder of a full path.

            Case $idExit ; Exit the loop.
                ExitLoop
        EndSwitch
    WEnd
EndFunc   ;==>Example

Func _MyTrayItemFunction($iID)
    If $iID = -1 Then
        Local $hMenu = TrayItemGetHandle(0)
        Local $iTrayCount = _GUICtrlMenu_GetItemCount($hMenu)
        $iID = _GUICtrlMenu_GetItemID($hMenu, $iTrayCount -1, True)
    EndIf
    ConsoleWrite("Calculated Tray Item ID = " & $iID & @CRLF)
EndFunc

I thought at first I'd have to take into consideration the automatic Pause and Exit tray items that are added if the '1' bit isn't included in the Opt("TrayMenuMode"), but it turns out those two tray items don't get tacked on until the very end so they aren't part of the item count while you're building the tray menu.

Edited by TimRude
Link to comment
Share on other sites

4 hours ago, TimRude said:

@pixelsearch Yep, this appears to work, as long as the control is one that has a handle, and not one of these mentioned in the GUICtrlGetHandle help topic:

Quote

The following controls will not return a handle: GUICtrlCreateDummy(), GUICtrlCreateGraphic(), GUICtrlCreateObj(), GUICtrlCreateListViewItem() and GUICtrlCreateTabItem().

GUICtrlCreateGraphic() shouldn't be in the help file list as -1 returns a correct handle & ID
It can be tested with GUICtrlCreateGraphic help file example, amended with a couple of line for the test :

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <StaticConstants.au3>

#include <WinAPIDlg.au3> ; added line for _WinAPI_GetDlgCtrlID <===================

Global Const $g_MAXGr = 6
Global $g_a_idArray[$g_MAXGr + 1] ; 0 and $g_MAXGr entries not used to allow GUICtrlDelete result
Global $g_idDel

Example()

Func Example()
        Local $idMsg, $iInc, $i

        CreateChild()

        $i = 1
        $iInc = 1
        ; Loop until the user exits.
        Do
                $idMsg = GUIGetMsg()

                If $idMsg = $g_idDel Then
                        GUICtrlDelete($g_a_idArray[$i])
                        $i = $i + $iInc
                        If $i < 0 Or $i > $g_MAXGr Then Exit
                EndIf
                If $idMsg > 0 Then MsgBox($MB_SYSTEMMODAL, "clicked", $idMsg & @CRLF & $g_a_idArray[5], 2)
        Until $idMsg = $GUI_EVENT_CLOSE
EndFunc   ;==>Example

Func CreateChild()
        GUICreate("My Draw")
        $g_idDel = GUICtrlCreateButton("Delete", 50, 165, 50)

        $g_a_idArray[1] = GUICtrlCreateGraphic(20, 50, 100, 100)
        GUICtrlSetBkColor(-1, 0xffffff)
        GUICtrlSetColor(-1, 0)

        ; start added lines <=========================
        ConsoleWrite("GUICtrlGetHandle(-1) = " & GUICtrlGetHandle(-1) & @crlf) ; works
        $iID = _WinAPI_GetDlgCtrlID(GUICtrlGetHandle(-1)) ; 4 (correct)
        ConsoleWrite("$iID = " & $iID & @crlf) ; works
        ; end added lines <===========================

        GUICtrlSetGraphic(-1, $GUI_GR_COLOR, 0xff0000, 0xff0000)
        GUICtrlSetGraphic(-1, $GUI_GR_PIE, 50, 50, 40, 30, 270)
        GUICtrlSetGraphic(-1, $GUI_GR_COLOR, 0x00ff00, 0xffffff)
        GUICtrlSetGraphic(-1, $GUI_GR_PIE, 58, 50, 40, -60, 90)

        GUICtrlSetGraphic(-1, $GUI_GR_ELLIPSE, 100, 100, 50, 80)
        GUICtrlSetGraphic(-1, $GUI_GR_COLOR, 0x00ff00, 0xc0c0ff)
        GUICtrlSetGraphic(-1, $GUI_GR_RECT, 350, 200, 50, 80)
        GUICtrlCreateLabel("label", 65, 100, 30)
        GUICtrlSetColor(-1, 0xff)

        $g_a_idArray[2] = GUICtrlCreateGraphic(220, 50, 100, 100)
        GUICtrlSetStyle(-1, $SS_NOTIFY)
        GUICtrlSetGraphic(-1, $GUI_GR_COLOR, 0, 0xff)
        GUICtrlSetGraphic(-1, $GUI_GR_PIE, 50, 50, 40, 30, 270)
        GUICtrlSetGraphic(-1, $GUI_GR_COLOR, 0x00ff00, 0xffffff)
        GUICtrlSetGraphic(-1, $GUI_GR_PIE, 58, 50, 40, -60, 90)

        $g_a_idArray[3] = GUICtrlCreateGraphic(220, 150, 100, 100, 0)
        GUICtrlSetBkColor(-1, 0xf08080)
        GUICtrlSetColor(-1, 0xff)
        GUICtrlSetGraphic(-1, $GUI_GR_COLOR, 0xff00)
        GUICtrlSetGraphic(-1, $GUI_GR_RECT, 50, 50, 80, 80)

        $g_a_idArray[4] = GUICtrlCreateGraphic(20, 200, 80, 80)
        GUICtrlSetState(-1, $GUI_DISABLE)
        GUICtrlSetBkColor(-1, 0xffffff)
        GUICtrlSetGraphic(-1, $GUI_GR_MOVE, 10, 10)
        GUICtrlSetGraphic(-1, $GUI_GR_COLOR, 0xff)
        GUICtrlSetGraphic(-1, $GUI_GR_LINE, 30, 40)
        GUICtrlSetGraphic(-1, $GUI_GR_COLOR, 0xff00)
        GUICtrlSetGraphic(-1, $GUI_GR_LINE, 70, 70)
        GUICtrlSetGraphic(-1, $GUI_GR_COLOR, 0xff0000)
        GUICtrlSetGraphic(-1, $GUI_GR_LINE, 10, 50)
        GUICtrlSetGraphic(-1, $GUI_GR_COLOR, 0xffff00)
        GUICtrlSetGraphic(-1, $GUI_GR_LINE, 10, 10)

        $g_a_idArray[5] = GUICtrlCreateGraphic(150, 10, 50, 50, 0)
        GUICtrlSetBkColor(-1, 0xa0ffa0)
        GUICtrlSetGraphic(-1, $GUI_GR_MOVE, 20, 20) ; start point
        ; it is better to draw line and after point
        ; to avoid to switch color at each drawing
        GUICtrlSetGraphic(-1, $GUI_GR_COLOR, 0x0000ff)
        GUICtrlSetGraphic(-1, $GUI_GR_DOT, 30, 30)
        GUICtrlSetGraphic(-1, $GUI_GR_COLOR, 0)
        GUICtrlSetGraphic(-1, $GUI_GR_LINE, 20, 40)
        GUICtrlSetGraphic(-1, $GUI_GR_COLOR, 0xff0000)
        GUICtrlSetGraphic(-1, $GUI_GR_DOT, 25, 25)
        GUICtrlSetGraphic(-1, $GUI_GR_COLOR, 0)
        GUICtrlSetGraphic(-1, $GUI_GR_LINE, 40, 40)
        GUICtrlSetGraphic(-1, $GUI_GR_DOT, 40, 40)

        GUISetState(@SW_SHOW)
EndFunc   ;==>CreateChild

Scite Console :

GUICtrlGetHandle(-1) = 0x0050079C
$iID = 4

 

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