How to detect the position an image loaded in a GUI has been clicked? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
pv007
Posts: 93
Joined: 20 Jul 2020, 23:50

How to detect the position an image loaded in a GUI has been clicked?

Post by pv007 » 28 Nov 2021, 14:18

How i could detect the exactly position where the image in the GUI has been clicked?

The image is 100x100px, so im trying to figure which area in this range has been clicked.
tile.png
tile.png (9.83 KiB) Viewed 616 times
Using random to illustrate that the image could be in a different position:

Code: Select all

Random, X, 0, 500
Random, Y, 0, 200

Gui, Add, Picture, x%x% y%y%, % "HBITMAP:" . LoadPicture("tile.png")
Gui, Show, w600 h300

This is what i already did but im not getting it, confused :crazy:

Code: Select all

Random, X, 0, 500
Random, Y, 0, 200

Gui, Add, Picture, x%x% y%y% gClick, % "HBITMAP:" . LoadPicture("tile.png")
Gui, Show, w600 h300


Gui, P: +hwndGuiP -Caption
Gui, P: Add, Picture, x0 y0, % "HBITMAP:" . LoadPicture("tile.png")
Gui, P: Show, w100 h100
return


Click() {

   CoordMode, Mouse, Relative
   MouseGetPos, X, Y

   FileAppend, X: %X% Y: %Y%`n,*

    VarSetCapacity(POINT, 8)
    NumPut(X, &POINT, 0, "Int")
    NumPut(Y, &POINT, 4, "Int")
    DllCall("user32\ScreenToClient", Ptr, GuiP, Ptr, &POINT)
    _X := NumGet(&POINT, 0, "Int")
    _Y := NumGet(&POINT, 4, "Int")


   FileAppend, `nX: %X% Y: %Y%`n,*
   FileAppend, _X: %_X% _Y: %_Y%`n,*  

}


pv007
Posts: 93
Joined: 20 Jul 2020, 23:50

Re: How to detect the position an image loaded in a GUI has been clicked?

Post by pv007 » 28 Nov 2021, 16:12

What you mean?
I tried to get the xy position from the MouseGetPos inside of mouseOver but it ain't relative to the picture.

Im trying to get the position relative to the picture, for example:
image.png
image.png (20.73 KiB) Viewed 548 times
Suppose i click where is the mouse, it would represent x15 y15 (example) in the image

User avatar
mikeyww
Posts: 26888
Joined: 09 Sep 2014, 18:38

Re: How to detect the position an image loaded in a GUI has been clicked?  Topic is solved

Post by mikeyww » 28 Nov 2021, 16:27

Code: Select all

image = ....jpg ; Adjust as needed
Global x1, y1
Random, x1, 0, 500
Random, y1, 0, 200
Gui, Add, Picture, x%x1% y%y1%, %image%
Gui, Show, w600 h300
OnMessage(WM_LBUTTONDOWN := 0x0201, "WM_LBUTTONDOWN")
Return

WM_LBUTTONDOWN(wParam, lParam) {
 x2 := lParam & 0xFFFF, y2 := lParam >> 16
 x := x2 - x1, y := y2 - y1, Ctrl := A_GuiControl ? "`n(in control " A_GuiControl ")" : ""
 ToolTip, You left-clicked in Gui window #%A_Gui% at coordinates (%x%`,%y%) relative to the picture. %Ctrl%
}

Post Reply

Return to “Ask for Help (v1)”