Page 1 of 1

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

Posted: 28 Nov 2021, 14:18
by pv007
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 649 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,*  

}

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

Posted: 28 Nov 2021, 16:01
by mikeyww

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

Posted: 28 Nov 2021, 16:12
by pv007
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 581 times
Suppose i click where is the mouse, it would represent x15 y15 (example) in the image

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

Posted: 28 Nov 2021, 16:27
by mikeyww

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%
}