Get position of picture in GUI Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
scriptor2016
Posts: 851
Joined: 21 Dec 2015, 02:34

Get position of picture in GUI

27 Mar 2017, 10:14

Hi, I'm looking to get the position of a picture in a GUI. This is only part of the code, I've snipped out parts that don't matter here. I know the position is x10 y10, but that position will be changing at random. The tooltip isn't returning the position- hoping someone can figure out why

Code: Select all

varwidth=25
varheight=25
Gui 1: Add , picture, vMyPicture x10 y10 w%varwidth% h%varheight% C:\Pic1.bmp
Gui 1: Show, x400 y400 w100 h100, PICTURES
ControlGetPos, x1, y1, w1, h1, MyPicture, PICTURES
tooltip, %x1% %y1% %w1% %h1%
return
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Get position of picture in GUI

27 Mar 2017, 10:21

this work fine for me:

Code: Select all

varwidth=25
varheight=25
Gui 1: Add , picture, vMyPicture x10 y10 w%varwidth% h%varheight%, % "C:\Users\Jérémy\Pictures\Sans titre.png"
Gui 1: Show, x400 y400 w100 h100, PICTURES
ControlGetPos, x1, y1, w1, h1, Static1, PICTURES
tooltip, %x1% %y1% %w1% %h1%
return
the name of the control -- classNN -- (here Static1) can be retrieved using AU3_spy.exe (in the AutoHotkey install directory)

documentation says about Control parameter:
Can be either ClassNN (the classname and instance number of the control) or the control's text, both of which can be determined via Window Spy
see: https://www.autohotkey.com/docs/command ... GetPos.htm


EDIT:

here's an alternative (in addition to Just me solution bellow which allows you operate using ControlOutputVariable MyPicture):

Code: Select all

varwidth=25
varheight=25
Gui 1: Add , picture, hwndID vMyPicture x10 y10 w%varwidth% h%varheight%, % "C:\Users\Jérémy\Pictures\Sans titre.png"
Gui 1: Show, x400 y400 w100 h100, PICTURES
ControlGetPos, x1, y1, w1, h1,, % "ahk_id " . ID
tooltip, %x1% %y1% %w1% %h1%
return
documentation:
To operate upon a control's HWND (window handle), leave the Control parameter blank and specify ahk_id %ControlHwnd% for the WinTitle parameter (this also works on hidden controls even when DetectHiddenWindows is Off).
Last edited by A_AhkUser on 27 Mar 2017, 10:30, edited 4 times in total.
my scripts
just me
Posts: 9442
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Get position of picture in GUI

27 Mar 2017, 10:23

Why do you want to use a Control command?

Code: Select all

GuiControlGet, MyPicture, 1:Pos
Tooltip, %MyPictureX% %MyPictureY% %MyPictureW% %MyPictureH%
scriptor2016
Posts: 851
Joined: 21 Dec 2015, 02:34

Re: Get position of picture in GUI

27 Mar 2017, 10:43

thanks guys - both work. All I had to do was replace "MyPicture" with "Static1" and it returns the proper values. Static2 for the 2nd picture, static 3 for the third, and so on. Thanks for the quick replies :)
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Get position of picture in GUI

27 Mar 2017, 12:58

scriptor2016 wrote:All I had to do was replace "MyPicture" with "Static1" and it returns the proper values. Static2 for the 2nd picture, static 3 for the third, and so on.
Note nevertheless that in this code...

Code: Select all

Gui, Add, Text, x1 y1, blabla
varwidth=25
varheight=25
Gui 1: Add , picture, vMyPicture x100 y10 w%varwidth% h%varheight%, % "C:\Users\Jérémy\Pictures\Sans titre.png"
Gui 1: Show, x400 y400 w400 h400, PICTURES
ControlGetPos, x1, y1, w1, h1, Static1, PICTURES
tooltip, %x1% %y1% %w1% %h1%
return
...the tooltip will display the coordinates of the Text control since both Pictureand Text controls belong to Static class-NN (so the first created, here a Text control represents Static1 here)...
That's why Just me relevantly suggest use GuiControlGet and that's also why I edit consequently my first post taking advantage of control hwnd (ID) this time (in case of despite everything you hold ControlGetPos very dear). GuiControlGet and ControlGetPos (using ID) are actually more reliable since you can choose the name of the variable to check (for instance MyPicture or ID).
my scripts
scriptor2016
Posts: 851
Joined: 21 Dec 2015, 02:34

Re: Get position of picture in GUI

28 Mar 2017, 10:33

I have another question regarding this GUI I'm trying to build. I'll be happy to share it once it's complete. My question is, upon a left click inside the GUI, I want to be able to determine if the click occurred on a ClassNN control. It can be any ClassNN control, so long as it is a control. The control will be one of hundreds of individual colored progressbars (like swatches). If the click happens anywhere else (ie. not ontop of a control, or on the titlebar, etc) then nothing should happen except a regular left click. Because it's a progressbar, I'm making the left click funcionality inside a function. Here's an example of what I mean:

Code: Select all

color001=A2BF87
Gui,1: Add, Progress,  x0 y0  w20 h20 Background%color001% vbutton001
Gui,1: Show, x500 y500 w50 h50 ,SWATCHES
OnMessage(0x201, "WM_LBUTTONDOWN")
return

WM_LBUTTONDOWN(wParam, lParam)
{
;check first if the left click happened ontop of a control- can be [i]any[/i] control for the time being (I might need to specify [i]which[/i] control later)
;if so, do stuff
;if not, just regular left click
return
}
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Get position of picture in GUI

28 Mar 2017, 18:33

You can use MouseGetPos. The fourth parameter OutputVarControl is according to documentation:
the name of the variable in which to store the name (ClassNN) of the control under the mouse cursor.
see: https://www.autohotkey.com/docs/command ... GetPos.htm

Here's a template with progress control:

Code: Select all

color001=A2BF87
Gui,1: Add, Progress,  x0 y0  w20 h20 Background%color001% vbutton001
Gui,1: Show, x500 y500 w550 h550 ,SWATCHES
OnMessage(0x201, "WM_LBUTTONDOWN")
return

WM_LBUTTONDOWN(wParam, lParam)
{
MouseGetPos,,,, OutputVarControl
if (InStr(OutputVarControl, "progress")) ; for example msctls_progress321 class NN
MsgBox % OutputVarControl
}
hope it helps you!
my scripts
scriptor2016
Posts: 851
Joined: 21 Dec 2015, 02:34

Re: Get position of picture in GUI

29 Mar 2017, 01:05

yes, it absolutely helps me :) Thanks so much- I'll post the final script when it's done - but I'm sure I might have another few questions before I get there- although it's close now :)
scriptor2016
Posts: 851
Joined: 21 Dec 2015, 02:34

Re: Get position of picture in GUI

30 Mar 2017, 00:21

I'm almost there now - hopefully on to the last hurdle... by the way, I'm attemtping to build a custom swatches panel for Photoshop in case anyone was wondering what this is ;) The last problem (hopefully) is inside the WM_LBUTTONDOWN(wParam, lParam) function. A quick explanation:

When left-clicked, the control (swatch) that was clicked on will hide and then another will take its place, the only difference being that the new one will be a few pixels smaller - this will create a "border" around the new swatch. The purpose of this is to make it easy to see which swatch was the last one/most recently clicked.

I have that part taken care of - but when we left click on a different swatch the next time, I'm trying to make the swatch with the border (the previously clicked swatch) return to its full size, and a border will now be drawn around the new swatch that we just clicked. I hope this makes sense. And, if the swatch with a current border is clicked on twice in succession (or 3 times, 4 times, etc) - the border around it should just stay the way it is. Problem is, right now if you click the same swatch several times in a row, the border keeps growing in size and the swatch shrinks - not what is intended.

I've included as much as possible here for it all to make sense, but have also omitted most of the script to save space in this topic. Right now, the script runs as bare-bones and hopefully someone can take a quick look at this.. thanks yet again

Code: Select all

varwidth=35 ;change this value to change the size of the swatch widths
varheight=35 ;change this value to change the size of the swatch heights

x+=%varwidth%
y+=%varheight%
x+=1
y+=0  
 
001=f4e7bd  
002=CFDEC7
003=A3BF96
004=B25921

Gui 1: -Caption +Border +ToolWindow +AlwaysOnTop 
Gui 1: Color, 000000
Gui,1:Add,Progress,  x0  y%y%  w%varwidth% h%varheight%  Background%001%   vbutton001
Gui,1:Add,Progress,  x%x% y%y% w%varwidth% h%varheight%  Background%002%   vbutton002
x+=%varwidth%
y+=%varheight%
x+=1 ;add a single pixel vertical line in between swatches

Gui,1:Add,Progress,  x%x% y%varheight%  w%varwidth% h%varheight%  Background%003%   vbutton003
x+=%varwidth%
y+=%varheight%
x+=1 ;add a single pixel vertical line in between swatches

Gui,1:Add,Progress,  x%x% y%varheight%  w%varwidth% h%varheight%  Background%004%   vbutton004
x+=%varwidth%
y+=%varheight%
x+=1 ;add a single pixel vertical line in between swatches

j=4
k=2
m:=(varwidth*j)
n:=(varheight*k)

Gui,1: Show, x400 y400 w%m% h%n% ,SWATCHES
OnMessage(0x201, "WM_LBUTTONDOWN")
return

WM_LBUTTONDOWN(wParam, lParam)
{
MouseGetPos,,,, OutputVarControl
if (InStr(OutputVarControl, "progress"))
{
CoordMode Pixel, Screen
CoordMode Mouse, Screen
MouseGetPos X, Y

PixelGetColor Color, %X%, %Y%, RGB

clipboard=%Color%
String := clipboard
StringTrimLeft, OutputVar, String, 2     
Clipboard := OutputVar

ControlGetPos, x, y, w, h, %OutputVarControl%, SWATCHES
PreviousOutputVarControl :=OutputVarControl 

x+=1
y+=1
w-=4
h-=4
GuiControl, Hide, %OutputVarControl%
Gui,1:Add,Progress,  x%x% y%y%   w%w% h%h%  Background%clipboard% 
return
   }
   else
   return
   }

just me
Posts: 9442
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Get position of picture in GUI

30 Mar 2017, 05:54

Hi scriptor2016,

playing around with your code I noticed some color issues when resizing the Progress controls. So that's what I would do:

Code: Select all

; Requires AHK 1.1.23.+
#NoEnv
SetBatchLines, -1
VarWidth := 35  ;change this value to change the size of the swatch widths
VarHeight := 35 ;change this value to change the size of the swatch heights
X := 0
Y := VarHeight

Swatches := 4

BG1 := "f4e7bd"
BG2 := "CFDEC7"
BG3 := "A3BF96"
BG4 := "B25921"

SelectedControl := ""

Gui 1:Margin, 0, 0
Gui 1:-Caption +Border +ToolWindow +AlwaysOnTop
Gui 1:Color, 000000
Loop, %Swatches%
{
   HBM := CreateDIB("0x" . BG%A_Index%)
   Gui 1:Add, Pic, X%X% Y%Y% w%VarWidth% h%VarHeight% vSwatch%A_Index% gSelectColor, HBITMAP:%HBM%
   X += VarWidth + 1 ;add a single pixel vertical line in between Swatches
}

Gui,1: Show, x400 y400, Swatches
Return

SelectColor:
   If (A_GuiControl <> SelectedControl)
   {
      If (SelectedControl)
      {
         GuiControlGet, P, 1:Pos, %SelectedControl%
         PX -= 2
         PY -= 2
         PW += 4
         PH += 4
         GuiControl, 1:MoveDraw, %SelectedControl%, X%PX% Y%PY% w%PW% h%PH%
      }
      ColorIndex := SubStr(A_GuiControl, 7)
      Clipboard := BG%ColorIndex%
      GuiControlGet, P, 1:Pos, %A_GuiControl%
      PX += 2
      PY += 2
      PW -= 4
      PH -= 4
      GuiControl, 1:MoveDraw, %A_GuiControl%, X%PX% Y%PY% w%PW% h%PH%
      SelectedControl := A_GuiControl
      Tooltip, %Clipboard% ; just for testing
   }
Return

; ==================================================================================================================================
; Modified version of a function by SKAN, 01-Apr-2014, autohotkey.com/boards/viewtopic.php?t=3203
; Creates an uni-colored bitmap of 2 * 2 pixels.
; ==================================================================================================================================
CreateDIB(Color) {
   Static W := 2, H := 2
   Static SLL := (W * 3) ; length of a scan line in a 24-bit (3-byte) bitmap (must be word aligned)
   VarSetCapacity(BMBITS, SLL * H, 0)
   P := &BMBITS
   Loop, % H           ; loop thru the rows
      Loop, % W        ; loop thru the pixels per row
         P := Numput(Color, P + 0, "UInt") - 1
   HBM := DllCall("CreateBitmap", "Int", W, "Int", H, "UInt", 1, "UInt", 24, "Ptr", 0, "UPtr")
   HBM := DllCall("CopyImage", "Ptr", HBM, "UInt", 0, "Int", 0, "Int", 0, "UInt", 0x2008, "UPtr")
   DllCall("SetBitmapBits", "Ptr", HBM, "UInt", SLL * H, "Ptr", &BMBITS)
   Return DllCall("CopyImage", "Ptr", HBM, "UInt", 0, "Int", W, "Int", H, "UInt", 0x200C, "UPtr")
}
scriptor2016
Posts: 851
Joined: 21 Dec 2015, 02:34

Re: Get position of picture in GUI

30 Mar 2017, 08:30

Thanks just me... the problem is now, the GUI is showing all black, without any color swatches. The tooltips work, but no colors are showing up
just me
Posts: 9442
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Get position of picture in GUI

30 Mar 2017, 09:35

Do you run AHK 1.1.23 or later?
scriptor2016
Posts: 851
Joined: 21 Dec 2015, 02:34

Re: Get position of picture in GUI

30 Mar 2017, 09:48

1.1.22.03

If I upgrade, will it affect any of my current scripts?
just me
Posts: 9442
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Get position of picture in GUI

30 Mar 2017, 09:51

scriptor2016 wrote:If I upgrade, will it affect any of my current scripts?
I don't think so.

But if you don't want to upgrade, try this changes:

Code: Select all

While (Index <= Swatches) {
   X := "xm"
   Loop, % SwatchesPerRow
   {
      Gui 1:Add, Pic, %X% Y%Y% w%VarWidth% h%VarHeight% vSwatch%Index% hwndHCTL 0x4E gSelectColor
      DllCall("SendMessage", "Ptr", HCTL, "UInt", 0x0172, "Ptr", 0, "Ptr", CreateDIB("0x" . BG%Index%))
      X := "x+1"
      Index++
   } Until (Index > Swatches)
   Y += VarHeight + 1
}
scriptor2016
Posts: 851
Joined: 21 Dec 2015, 02:34

Re: Get position of picture in GUI

30 Mar 2017, 10:01

Unfortunately nothing shows up now. Here's the current code:

Code: Select all

; Requires AHK 1.1.23.+
#NoEnv
SetBatchLines, -1
VarWidth := 35  ;change this value to change the size of 

the swatch widths
VarHeight := 35 ;change this value to change the size of 

the swatch heights
X := 0
Y := VarHeight

Swatches := 4

BG1 := "f4e7bd"
BG2 := "CFDEC7"
BG3 := "A3BF96"
BG4 := "B25921"

SelectedControl := ""

Gui 1:Margin, 0, 0
Gui 1:-Caption +Border +ToolWindow +AlwaysOnTop
Gui 1:Color, 000000
While (Index <= Swatches) {
   X := "xm"
   Loop, % SwatchesPerRow
   {
      Gui 1:Add, Pic, %X% Y%Y% w%VarWidth% h%VarHeight% 

vSwatch%Index% hwndHCTL 0x4E gSelectColor
      DllCall("SendMessage", "Ptr", HCTL, "UInt", 0x0172, 

"Ptr", 0, "Ptr", CreateDIB("0x" . BG%Index%))
      X := "x+1"
      Index++
   } Until (Index > Swatches)
   Y += VarHeight + 1
}
Gui,1: Show, x400 y400, Swatches
Return

SelectColor:
   If (A_GuiControl <> SelectedControl)
   {
      If (SelectedControl)
      {
         GuiControlGet, P, 1:Pos, %SelectedControl%
         PX -= 2
         PY -= 2
         PW += 4
         PH += 4
         GuiControl, 1:MoveDraw, %SelectedControl%, X%PX% 

Y%PY% w%PW% h%PH%
      }
      ColorIndex := SubStr(A_GuiControl, 7)
      Clipboard := BG%ColorIndex%
      GuiControlGet, P, 1:Pos, %A_GuiControl%
      PX += 2
      PY += 2
      PW -= 4
      PH -= 4
      GuiControl, 1:MoveDraw, %A_GuiControl%, X%PX% Y%PY% 

w%PW% h%PH%
      SelectedControl := A_GuiControl
      Tooltip, %Clipboard% ; just for testing
   }
Return

CreateDIB(Color) {
   Static W := 2, H := 2
   Static SLL := (W * 3) ; length of a scan line in a 24-

bit (3-byte) bitmap (must be word aligned)
   VarSetCapacity(BMBITS, SLL * H, 0)
   P := &BMBITS
   Loop, % H           ; loop thru the rows
      Loop, % W        ; loop thru the pixels per row
         P := Numput(Color, P + 0, "UInt") - 1
   HBM := DllCall("CreateBitmap", "Int", W, "Int", H, 

"UInt", 1, "UInt", 24, "Ptr", 0, "UPtr")
   HBM := DllCall("CopyImage", "Ptr", HBM, "UInt", 0, 

"Int", 0, "Int", 0, "UInt", 0x2008, "UPtr")
   DllCall("SetBitmapBits", "Ptr", HBM, "UInt", SLL * H, 

"Ptr", &BMBITS)
   Return DllCall("CopyImage", "Ptr", HBM, "UInt", 0, 

"Int", W, "Int", H, "UInt", 0x200C, "UPtr")
}
just me
Posts: 9442
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Get position of picture in GUI

30 Mar 2017, 10:13

Apparently there are some hard line breaks in the code you posted. Try to remove them.
scriptor2016
Posts: 851
Joined: 21 Dec 2015, 02:34

Re: Get position of picture in GUI

30 Mar 2017, 10:18

Alright, I'll try that- but what is meant by 'hard line breaks'
scriptor2016
Posts: 851
Joined: 21 Dec 2015, 02:34

Re: Get position of picture in GUI

30 Mar 2017, 10:24

oh I see what you're saying. Gimme a second and I'll fix it..
just me
Posts: 9442
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Get position of picture in GUI  Topic is solved

30 Mar 2017, 10:25

Sorry, my bad! I had changed my test script for further testing. The change for my original version must be:

Code: Select all

Loop, %Swatches%
{
   Gui 1:Add, Pic, %X% Y%Y% w%VarWidth% h%VarHeight% vSwatch%A_Index% hwndHCTL 0x4E gSelectColor
   DllCall("SendMessage", "Ptr", HCTL, "UInt", 0x0172, "Ptr", 0, "Ptr", CreateDIB("0x" . BG%A_Index%))
   X += VarWidth + 1 ;add a single pixel vertical line in between Swatches
}
scriptor2016
Posts: 851
Joined: 21 Dec 2015, 02:34

Re: Get position of picture in GUI

30 Mar 2017, 10:30

ah-ha!! That works perfect now - can't thank you enough. The only thing for me now is to re-work my original script to fit this one. The original script is 18 swatches wide, and 6 rows, for a total of 108 colors. I will try to re-program my code to fit your code and I will get back to you on how things are going. Once again, thanks so much :)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], mmflume, ShatterCoder and 100 guests