Setting Gui x/y coordinates with window hook

Ask gaming related questions (AHK v1.1 and older)
jimboslice
Posts: 5
Joined: 01 Jan 2021, 23:35

Setting Gui x/y coordinates with window hook

01 Jan 2021, 23:52

Hi,

I play poker, and I'm trying to create a very basic gui that attaches to my poker client window and covers up my account balance with a solid box. I'm using the code from this post.

Code: Select all

#NoEnv
#SingleInstance force
SetWorkingDir % A_ScriptDir
SendMode, Input
if not A_IsAdmin
	Run *RunAs "%A_ScriptFullPath%"

SetWinDelay, -1


WinWaitActive, Ignition Casino - Poker Lobby
WinGet, PID, PID, % "ahk_id " . owner:=WinExist() ; if all parameters are omitted, WinExist returns the ID the Last Found Window if it still exist; WinWait set the Last Found Window
GUI, +ToolWindow +Owner%owner% +Hwndowned -Caption
/*
+ToolWindow > no taskbar button
+Owner%owner% > make the GUI owned by the window whose ID is the one stored in the variable 'owner'
+Hwndowned > stores the ID of the GUI in 'owned'
*/
global ownerAhkId := "ahk_id " . owner, ownedAhkId := "ahk_id " . owned
GUI, Show, w75 h50 x100 y100
hWinEventHook := SetWinEventHook("0x800B", "0x800B", 0, RegisterCallback("OnLocationChangeMonitor"), PID, 0, 0) ; register a event hook function for EVENT_OBJECT_LOCATIONCHANGE := "0x800B"
OnLocationChangeMonitor(0, 0, 1)
OnExit, handleExit ; specifies a subroutine or callback function to run automatically when the script exits...
WinWaitClose % ownerAhkId ; ... or until the owner window does not exist
handleExit:
UnhookWinEvent(hWinEventHook) ; removes the event hook function created by SetWinEventHook before exiting the script
ExitApp



OnLocationChangeMonitor(_hWinEventHook, _event, _hwnd) { ; https msdn.microsoft.com /en-us/library/windows/desktop/dd373885(v=vs.85).aspx  Broken Link for safety
	_listLines := A_ListLines
	ListLines, Off
		if not (_hwnd) ; if the system sent the EVENT_OBJECT_LOCATIONCHANGE event for the caret: https msdn.microsoft.com /en-us/library/windows/desktop/dd318066(v=vs.85).aspx  Broken Link for safety
			return
		WinGetPos, _x, _y,,, % ownerAhkId
		WinMove, % ownedAhkId,, % _x, % _y ; set the position of the window owned by owner
	ListLines % _listLines ? "On" : "Off"
}



SetWinEventHook(_eventMin, _eventMax, _hmodWinEventProc, _lpfnWinEventProc, _idProcess, _idThread, _dwFlags) {
   DllCall("CoInitialize", "Uint", 0)
   return DllCall("SetWinEventHook"
			, "Uint", _eventMin
			, "Uint", _eventMax
			, "Ptr", _hmodWinEventProc
			, "Ptr", _lpfnWinEventProc
			, "Uint", _idProcess
			, "Uint", _idThread
			, "Uint", _dwFlags)
} ; cf. https://autohotkey.com/boards/viewtopic.php?t=830
UnhookWinEvent(_hWinEventHook) {
    _v := DllCall("UnhookWinEvent", "Ptr", _hWinEventHook)
    DllCall("CoUninitialize")
return _v
} ;  cf. https://autohotkey.com/boards/viewtopic.php?t=830
It's *almost* there, I just need to adjust the coordinates of the gui to match where the account balance is. However, the x and y coordinates specified after "Gui, Show" don't have any effect on where it's drawn within the window of the poker client. I'm very wet behind the ears at this, so I'm not sure if the rest of the code is forcing the gui to always attach to the top left of the window or something?

Any help would be much appreciated. Thanks so much!
Last edited by BoBo on 02 Jan 2021, 03:02, edited 1 time in total.
Reason: Moved to Gaming section.
paulpma
Posts: 65
Joined: 08 Sep 2018, 22:05

Re: Setting Gui x/y coordinates with window hook

02 Jan 2021, 02:15

Hi, have you looked at CoordMode
https://www.autohotkey.com/docs/commands/CoordMode.htm
Hopefully, it will help you.
jimboslice
Posts: 5
Joined: 01 Jan 2021, 23:35

Re: Setting Gui x/y coordinates with window hook

02 Jan 2021, 03:28

paulpma wrote:
02 Jan 2021, 02:15
Hi, have you looked at CoordMode
https://www.autohotkey.com/docs/commands/CoordMode.htm
Hopefully, it will help you.
Hey,

Thanks for the response. I tried adding both CoordMode, ToolTip, Window and then CoordMode, Pixel, Window after SetWinDelay = -1, but neither had any effect. Also tried placing the window itself at the very top left corner of the screen, but also no effect.
User avatar
mikeyww
Posts: 27191
Joined: 09 Sep 2014, 18:38

Re: Setting Gui x/y coordinates with window hook

02 Jan 2021, 07:57

The GUI coordinates are absolute, but it's easy to add the offset as follows.

Code: Select all

WinGetPos, x, y,,, Ignition Casino - Poker Lobby
guiX := x + 100, guiY := y + 100
GUI, Show, w75 h50 x%guiX% y%guiY%
jimboslice
Posts: 5
Joined: 01 Jan 2021, 23:35

Re: Setting Gui x/y coordinates with window hook

02 Jan 2021, 17:04

mikeyww wrote:
02 Jan 2021, 07:57
The GUI coordinates are absolute, but it's easy to add the offset as follows.

Code: Select all

WinGetPos, x, y,,, Ignition Casino - Poker Lobby
guiX := x + 100, guiY := y + 100
GUI, Show, w75 h50 x%guiX% y%guiY%
Hi,

Thanks for responding. I added (slash replaced) this to my script at the "Gui, Show" line, but I'm still not getting the intended result. As a test, I used the variables %guiX% and %guiY% with the width and height parameters, and the width and height of the gui are changing based on the location of the window when I first run the script, so those lines are working correctly.

I'm probably not understanding everything here, but are you sure that the coordinates are absolute? Does absolute mean relative to the screen? The gui always opens at the top left of the application window, and the script sets the application window as owner of the gui, so I would have thought that meant the gui is operating relative to the application window.
User avatar
mikeyww
Posts: 27191
Joined: 09 Sep 2014, 18:38

Re: Setting Gui x/y coordinates with window hook

02 Jan 2021, 18:26

Yep.
Xn: Specify for n the window's X-position on the screen, in pixels. Position 0 is the leftmost column of pixels visible on the screen.
The demo is easy:

Code: Select all

proc := "notepad.exe", wTitle := "ahk_exe " proc
Run, %proc%
WinWaitActive, %wTitle%
WinMove,,, 400, 200, 200, 250
WinGetPos, x, y
guiX := x + 50, guiY := y + 50
MsgBox, 64, Results, x = %x%`ny = %y%`nguiX = %guiX%`nguiY = %guiY%
Gui, Add, Button, Default, OK
GUI, Show, w75 h50 x%guiX% y%guiY%
You might have the wrong window title. What are the actual values of x and y before the GUI, Show? You can check that to find out. Do they match your main window's position? If WinGetPos fails, you will not see an error message, so you need to do some debugging here.

I thought that your goal is to show the GUI relative to the program window? Is that incorrect?

Feel free to post your revised script for more feedback. If you post it, include a detailed statement of the desired behavior.
jimboslice
Posts: 5
Joined: 01 Jan 2021, 23:35

Re: Setting Gui x/y coordinates with window hook

03 Jan 2021, 00:07

Hi,

I figured it out! In the function that moves the gui with the window - OnLocationChangeMonitor() - it always forces it to the same coordinates as the application window:

Code: Select all

WinGetPos, _x, _y,,, % ownerAhkId
WinMove, % ownedAhkId,, % _x, % _y ; set the position of the window owned by owner
That was probably quite basic, but I didn't really understand what was going on until I compared your demo with my script.

Thanks for your help! Cheers.
User avatar
mikeyww
Posts: 27191
Joined: 09 Sep 2014, 18:38

Re: Setting Gui x/y coordinates with window hook

03 Jan 2021, 06:35

Thanks for the follow-up! Best wishes.

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: No registered users and 17 guests