Jump to content



Photo

_GetControlID()


  • Please log in to reply
7 replies to this topic

#1 guinness

guinness

    guinness

  • MVPs
  • 10,427 posts

Posted 16 December 2010 - 10:30 PM

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 = -1)     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:
AutoIt         
#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 #include <GUIConstantsEx.au3> #include <WinAPI.au3> _Main() Func _Main()     Local $hGUI     $hGUI = GUICreate("_GetControlID() Example 1")     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() = $GUI_EVENT_CLOSE Then             ExitLoop         EndIf     WEnd     GUIDelete($hGUI) EndFunc   ;==>_Main Func _Function($iControlID = -1, $sData = "")     $iControlID = _WinAPI_GetDlgCtrlID(GUICtrlGetHandle($iControlID))     If @error Then         Return SetError(1, 0, 0)     EndIf     Return GUICtrlSetData($iControlID, $sData) EndFunc   ;==>_Function


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, 11 October 2011 - 01:00 PM.

Example List: _AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_DesktopDimensions()_DisplayPassword()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUISetIcon()_Icon_Clear()/_Icon_Set()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringIsValid()_StringReplaceWholeWord()_StringStripChar()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()AutoIt SearchAutoIt3 PortableAutoItWinGetTitle()/AutoItWinSetTitle()CodingFileInstallrGeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIGetBkColor()LockFile()PasteBinSciTE JumpSignature CreatorWM_COPYDATAMore Examples...Updated: 11/04/2013








#2 bo8ster

bo8ster

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 1,189 posts

Posted 17 December 2010 - 06:10 AM

I have written something similar here
SmOke_N has two outstanding that you may be interested in too.

_CtrlGetByPos
and
_WinGetCtrlInfo()
Post your code because code says more then your words can. SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y. Use Opt("MustDeclareVars", 1)Brett F's Learning To Script with AutoIt V3Valuater's AutoIt 1-2-3, Class... is now in SessionContribution: Get SVN Rev Number, Control Handle under mouse, A Presentation using AutoIt, Log ConsoleWrite output in Scite

#3 guinness

guinness

    guinness

  • MVPs
  • 10,427 posts

Posted 17 December 2010 - 01:02 PM

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 _SetBitmapToCtrl() 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")

Example List: _AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_DesktopDimensions()_DisplayPassword()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUISetIcon()_Icon_Clear()/_Icon_Set()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringIsValid()_StringReplaceWholeWord()_StringStripChar()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()AutoIt SearchAutoIt3 PortableAutoItWinGetTitle()/AutoItWinSetTitle()CodingFileInstallrGeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIGetBkColor()LockFile()PasteBinSciTE JumpSignature CreatorWM_COPYDATAMore Examples...Updated: 11/04/2013


#4 Manadar

Manadar

    Taking a REST.

  • MVPs
  • 10,714 posts

Posted 17 December 2010 - 02:08 PM

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


#5 guinness

guinness

    guinness

  • MVPs
  • 10,427 posts

Posted 17 December 2010 - 04:20 PM

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:
AutoIt         
#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 #include <Array.au3> Global $Global_Array[1][2] = [[0, 2]] _Main() Func _Main()     Local $B = 10, $hGUI, $iButton     $hGUI = GUICreate("_GetControlID() Example 2")     GUISetState(@SW_SHOW)     For $A = 1 To 10         GUICtrlCreateLabel("Example Label_" & $A, 5, $B, 150, 20)         _ArrayAddControl(-1)         $B += 25     Next     $iButton = GUICtrlCreateButton("Show Array", 5, 400 - 30, 65, 22.5)     While 1         Switch GUIGetMsg()             Case -3                 ExitLoop             Case $iButton                 _ArrayDisplay($Global_Array)         EndSwitch     WEnd     GUIDelete($hGUI) EndFunc   ;==>_Main Func _ArrayAddControl($iControlID = -1)     Local $aControlID, $hControlID     $hControlID = GUICtrlGetHandle($iControlID)     $aControlID = DllCall("user32.dll", "int", "GetDlgCtrlID", "hwnd", $hControlID)     If @error Then         Return SetError(1, 0, 0)     EndIf     $iControlID = $aControlID[0]     If UBound($Global_Array, 1) <= $Global_Array[0][0] + 1 Then         ReDim $Global_Array[UBound($Global_Array, 1) * 2][$Global_Array[0][1]]     EndIf     $Global_Array[0][0] += 1     $Global_Array[$Global_Array[0][0]][0] = $iControlID     $Global_Array[$Global_Array[0][0]][1] = GUICtrlRead($iControlID)     ReDim $Global_Array[$Global_Array[0][0] + 1][$Global_Array[0][1]]     Return $iControlID ; Returns The ControlID, But It Could Return Another Value. EndFunc   ;==>_ArrayAddControl

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, 16 May 2011 - 09:23 PM.

Example List: _AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_DesktopDimensions()_DisplayPassword()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUISetIcon()_Icon_Clear()/_Icon_Set()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringIsValid()_StringReplaceWholeWord()_StringStripChar()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()AutoIt SearchAutoIt3 PortableAutoItWinGetTitle()/AutoItWinSetTitle()CodingFileInstallrGeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIGetBkColor()LockFile()PasteBinSciTE JumpSignature CreatorWM_COPYDATAMore Examples...Updated: 11/04/2013


#6 Mat

Mat

    43 38 48 31 30 4E 34 4F 32

  • MVPs
  • 4,042 posts

Posted 17 December 2010 - 07:04 PM

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

I don't know where I'm going, but I'm on my way.


#7 guinness

guinness

    guinness

  • MVPs
  • 10,427 posts

Posted 17 December 2010 - 11:43 PM

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, 17 December 2010 - 11:48 PM.

Example List: _AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_DesktopDimensions()_DisplayPassword()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUISetIcon()_Icon_Clear()/_Icon_Set()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringIsValid()_StringReplaceWholeWord()_StringStripChar()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()AutoIt SearchAutoIt3 PortableAutoItWinGetTitle()/AutoItWinSetTitle()CodingFileInstallrGeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIGetBkColor()LockFile()PasteBinSciTE JumpSignature CreatorWM_COPYDATAMore Examples...Updated: 11/04/2013


#8 guinness

guinness

    guinness

  • MVPs
  • 10,427 posts

Posted 08 March 2011 - 04:10 PM

Example 3: You can see in this Function the use of _GetControlID().

Edited by guinness, 08 March 2011 - 04:10 PM.

Example List: _AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_DesktopDimensions()_DisplayPassword()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUISetIcon()_Icon_Clear()/_Icon_Set()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringIsValid()_StringReplaceWholeWord()_StringStripChar()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()AutoIt SearchAutoIt3 PortableAutoItWinGetTitle()/AutoItWinSetTitle()CodingFileInstallrGeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIGetBkColor()LockFile()PasteBinSciTE JumpSignature CreatorWM_COPYDATAMore Examples...Updated: 11/04/2013





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users