Thanks
Helgef for this amazing and relevant function
I'm experimenting with the register callback examples from the documentation trying to understand how they works. Then I came up with the idea to implement the second one directly in the 'EventObj' example you provided @
GuiSize Function inside Class Not Executing to see if I can this way create a
GUISize Event obj. The fact that
registercallback's functionName parameter does not support function objects brought me here: I remembered seeing this function a while ago.
The script below works but, for some reason, I cannot interact with the
MsgBox which fires from the
__Delete meta-method and adding a sleep simply crashes the script. Also, I don't know if, passing a func object, I need to take account of the 'this' hidden param for the
ParamCount parameter (
ParamCount + 1?). Sorry for these foolish questions, simply experimenting
- and enjoying trying your script
Code: Select all
#NoEnv
#SingleInstance force
SetWorkingDir % A_ScriptDir
SendMode, Input
CoordMode, ToolTip, Screen
#Warn
; Windows 8.1 64 bit - Autohotkey v1.1.30.01 32-bit Unicode
#Include %A_ScriptDir%\callbackcreate.ahk ; https://github.com/HelgeffegleH/AHK-misc./blob/master/functions/callbackcreate/callbackcreate.ahk
G := new GUI()
return
!i::CallbackFree(G._winProcNew)
Class GUI {
__New() {
static _winUserFunc := (A_PtrSize = 8) ? "SetWindowLongPtr" : "SetWindowLong"
static GWL_WNDPROC := -4
local
_hLastFoundWindow := WinExist()
_GUI := A_DefaultGUI
GUI, New, +hwnd_GUIID +Resize
GUI, Show, w300 h300
GUI, %_GUI%:Default
WinExist("ahk_id " . _hLastFoundWindow)
_winProcNew := this._winProcNew := CallbackCreate(this._windowProc.bind(this), "", 4)
; _winProcNew := CallbackCreate(this._windowProc.bind(this), "", 5) ; does the hidden 'this' also to be counted?
this._winProcOld := DllCall(_winUserFunc, "Ptr", _GUIID, "Int", GWL_WNDPROC, "Ptr", _winProcNew, "Ptr")
; based on the second exemple from the documentation @registercallback: https://www.autohotkey.com/docs/commands/RegisterCallback.htm#Examples
}
_windowProc(_hwnd, _uMsg, _wParam, _lParam) {
static WM_SIZE := 0x05
local
if (_uMsg = WM_SIZE) {
_w := _lParam & 0xFFFF
_h := _lParam >> 16
ToolTip % _w "," _h "|" this.__Class
}
return DllCall("CallWindowProc", "Ptr", this._winProcOld, "Ptr", _hwnd, "UInt", _uMsg, "Ptr", _wParam, "Ptr", _lParam)
}
__Delete() {
MsgBox % A_ThisFunc ; one cannot interact with the MsgBox
ToolTip % A_ThisFunc
; sleep, 1000 ; crashes the script
}
}
[EDIT] I finally understood why the
MsgBox freezed and why the script crashed: I'm pretty sure that was related to the fact that I was trying to free the callback while its adress was still used for the window procedure...
Code: Select all
G := new GUI()
return
!i::
DllCall((A_PtrSize = 8) ? "SetWindowLongPtr" : "SetWindowLong", "Ptr", G.ID, "Int", -4, "Ptr", G._winProcOld, "Ptr") ; reset the window procedure (I think...)
CallbackFree(G._winProcNew)
return
; ...