Hi @kurtykurtyboy
You are welcome Feedback always helps me know where to focus my efforts on a project
So the most direct approach to your goal is not possible.
AutoIt3 only accepts user functions with it's event functions. I tried setting a getter on the main object and giving the object itself as the second argument to GUICtrlSetOnEvent, to no avail.
I did find a workaround though, let me know what you think
#include "AutoItObject_Internal.au3"
#include <GUIConstantsEx.au3>
#include <WinAPISysWin.au3>
Opt("GUIOnEventMode", 1)
Global $hGUI = GUICreate("MyGUI", 400, 350, 763, 317)
GUISetOnEvent($GUI_EVENT_CLOSE, "_onExitMain")
Global $Button_1 = _objButton("Button 1", 140, 100, 91, 41)
;~ GUICtrlSetOnEvent($Button_1.hwnd, _button1ClickEvent)
_main()
Func _main()
GUISetState(@SW_SHOWNORMAL)
While 1
Sleep(100)
WEnd
EndFunc ;==>_main
;'traditional' event function
Func _button1ClickEvent()
$Button_1.click()
EndFunc
Func _onExitMain()
$Button_1 = 0
GUIDelete()
Exit
EndFunc ;==>_onExitMain
Func _objButton($sCaption, $iLeft, $iTop, $iWidth, $iHeight)
Local $oIDispatch = IDispatch()
$oIDispatch.caption = $sCaption
$oIDispatch.hwnd = GUICtrlCreateButton($sCaption, $iLeft, $iTop, $iWidth, $iHeight)
ConsoleWrite($oIDispatch.hwnd&@CRLF)
;$oIDispatch.__defineGetter("click", _objButton_click)
GUICtrlSetOnEvent($oIDispatch.hwnd, _objButton_click2)
_WinAPI_SetProp(GUICtrlGetHandle($oIDispatch.hwnd), "IDispatch", Ptr($oIDispatch))
Return $oIDispatch
EndFunc
;event function built into the button object
Func _objButton_click($oSelf)
ConsoleWrite("Click Caption: " & $oSelf.parent.caption & @CRLF)
EndFunc
;event function for the button object
Func _objButton_click2()
$pIDispatch = _WinAPI_GetProp(@GUI_CtrlHandle, "IDispatch"); Retrive IDispatch pointer
If $pIDispatch = 0 Then Return; Pointer not available, return from function
Local $oIDispatch = ObjCreateInterface($pIDispatch, $__AOI_IID_IDispatch, Default, True); Convert pointer back to object
__AOI_AddRef($pIDispatch);When using ObjCreateInterface multiple times on the same ptr, AutoIt3 seems to not AddRef +1 (or it may be a bug in AutoItObject_Internal)
ConsoleWrite("Click Caption: " & $oIDispatch.caption & @CRLF)
EndFunc
;From https://www.autoitscript.com/forum/topic/207492-help-needed-image-viewer-with-zoom-and-translate/
Func _WinAPI_SetProp($hWnd, $sProp, $pData)
Return DllCall("user32.dll", "bool", "SetPropW", "hwnd", $hWnd, "wstr", $sProp, "ptr", $pData)[0]
EndFunc
;From https://www.autoitscript.com/forum/topic/207492-help-needed-image-viewer-with-zoom-and-translate/
Func _WinAPI_GetProp($hWnd, $sProp)
Return DllCall("user32.dll", "ptr", "GetPropW", "hwnd", $hWnd, "wstr", $sProp)[0]
EndFunc
I did originally try to use _WinAPI_SetWindowLong with $GWL_USERDATA, but it seems AutoIt3 uses that itself 😡
edit:
you COULD call $oIDispatch.click() within _objButton_click2, if you want custom functions associated with the click getter.