Why does this page.setrect(x,y, w,h) not work properly?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
anhnha
Posts: 116
Joined: 08 Aug 2018, 02:46

Why does this page.setrect(x,y, w,h) not work properly?

28 Dec 2022, 10:24

Why does this page.setrect(x,y, w,h) not work properly?
By setting x := A_ScreenWidth/2, y := A_ScreenHeight/2 I expect the top left corner of the window would move to the center of the screen but it it move to lower right corner. Or probably I misunderstood how it work.
And another question which I'm not sure if it's ok to ask here or better to make a new thread.
How can I make the window open by this Rufaydium always on top?

Code: Select all

#include Rufaydium.ahk 
w := 500
h := 500
x := A_ScreenWidth/2
y := A_ScreenHeight/2 

Chrome := new Rufaydium()
Page := Chrome.NewSession()
; navigate to url
Page.Navigate("https://www.autohotkey.com/")
page.setrect(x,y, w,h)
return


#f12:
Chrome.QuitAllSessions() ; close all session 
Chrome.Driver.Exit() ; then exits driver
exitapp
User avatar
boiler
Posts: 17310
Joined: 21 Dec 2014, 02:44

Re: Why does this page.setrect(x,y, w,h) not work properly?

28 Dec 2022, 10:48

It sounds like it's doing exactly what you asked. Any window whose top-left corner is at the center of the screen would result in a window that appears wholly within the lower-right quadrant of the screen. Are you trying to center the window on the screen? Then you need to position the x location at half of its width to the left of the center and the y location at half of its height above the center.

If it really did move the upper-left corner of the window to the lower-right corner of the screen, then the window would be totally off the screen. Is that what happened?
User avatar
boiler
Posts: 17310
Joined: 21 Dec 2014, 02:44

Re: Why does this page.setrect(x,y, w,h) not work properly?

28 Dec 2022, 11:00

To center the window on the screen:

Code: Select all

w := 500
h := 500
x := (A_ScreenWidth - w) / 2
y := (A_ScreenHeight - h) / 2
anhnha
Posts: 116
Joined: 08 Aug 2018, 02:46

Re: Why does this page.setrect(x,y, w,h) not work properly?

28 Dec 2022, 11:20

It does not work as expected.
The top left corner of the window is not at the point (x,y) that I gave.
I wanted to center the whole window on the center of the screen but I just tested how it work by setting the top left corner at the center of the screen.
If it really did move the upper-left corner of the window to the lower-right corner of the screen, then the window would be totally off the screen. Is that what happened?
About half of the window is off the screen and the top left corner is very far from the center.

Code: Select all

x := (A_ScreenWidth - w) / 2
y := (A_ScreenHeight - h) / 2
This does not work as well. It still moves very far from center to very close to the lower right corner.
User avatar
boiler
Posts: 17310
Joined: 21 Dec 2014, 02:44

Re: Why does this page.setrect(x,y, w,h) not work properly?

28 Dec 2022, 14:44

What are your Windows scaling settings set for?
User avatar
rommmcek
Posts: 1480
Joined: 15 Aug 2014, 15:18

Re: Why does this page.setrect(x,y, w,h) not work properly?

28 Dec 2022, 16:08

He has (most probably still) this monitor configuration.
My DPI is 120 ppi (125%) and Rufaydium's SetRect() doesn't work for me neither. I can correct positon by a certain factor, but this would then work only for a given size (width & height). It doesn't seem to behave linear at all.
@anhnha: Try:

Code: Select all

w:= 500, h:= 500
x:= -Round((1080 - w) / 2), y:=  Round((1920 - h) / 2)
If it will set browser in the middle of the second monitor (the left one) correctly then it would be a strong argument, this is internal Rufaydium's or even driver's issue.

Workaround: Use WinWait & WinMove commands (work for me).
Attachments
anhnha mon config.png
anhnha mon config.png (8.45 KiB) Viewed 678 times
anhnha
Posts: 116
Joined: 08 Aug 2018, 02:46

Re: Why does this page.setrect(x,y, w,h) not work properly?

28 Dec 2022, 22:32

I see.
I forgot about scaling. I used scale of 150% and I just tried on my laptop with no scale and it works fine.
Can we add something like DPIScale to correct this as we did it the previous thread?
Can we force the chrome window starts at the center of a screen instead of top left corner?
User avatar
rommmcek
Posts: 1480
Joined: 15 Aug 2014, 15:18

Re: Why does this page.setrect(x,y, w,h) not work properly?

29 Dec 2022, 04:31

This seems to work reasonably well:

Code: Select all

#include Rufaydium.ahk 

w := 500
h := 500
x := round((A_ScreenWidth - w)/2) 
y := round((A_ScreenHeight - h)/2) 

Chrome := new Rufaydium()
Chrome.capabilities.addArg("--app=https://korean.dict.naver.com/kovidict/vietnamese/#/search?query=test")
Chrome.capabilities.addArg("--window-position=" ScaleDim(x) "," ScaleDim(y) )
Chrome.capabilities.addArg("--window-size=" ScaleDim(w) "," ScaleDim(h) )
Page := Chrome.NewSession()
return

Esc::
#f12::
Chrome.QuitAllSessions() ; close all session 
Chrome.Driver.Exit() ; then exits driver
exitapp

ScaleDim(val) {
    Return Round(val/(A_ScreenDPI/96))
}
or:

Code: Select all

#include Rufaydium.ahk 

w := 700
h := 500
x := round((A_ScreenWidth - w)/2) 
y := round((A_ScreenHeight - h)/2) 

Chrome := new Rufaydium()
Page := Chrome.NewSession()
page.setrect(ScaleDim(x), ScaleDim(y), ScaleDim(w), ScaleDim(h))
; navigate to url
Page.Navigate("https://www.autohotkey.com/")
return

Esc::
#f12::
Chrome.QuitAllSessions() ; close all session 
Chrome.Driver.Exit() ; then exits driver
exitapp

ScaleDim(val) {
    Return Round(val/(A_ScreenDPI/96))
}
Note: Chrome has minimal size values. I we set them below, we will be off center.
Edit: Introducing a function for scaling.
anhnha
Posts: 116
Joined: 08 Aug 2018, 02:46

Re: Why does this page.setrect(x,y, w,h) not work properly?

29 Dec 2022, 06:09

This seems to work reasonably well:
Yes, it works now.
What would be the reason for the number 96?
Do you know how to make it always on top?
User avatar
rommmcek
Posts: 1480
Joined: 15 Aug 2014, 15:18

Re: Why does this page.setrect(x,y, w,h) not work properly?

29 Dec 2022, 07:49

anhnha wrote:
29 Dec 2022, 06:09
What would be the reason for the number 96?
msdn DpiScale.DpiScaleX Property wrote:For example, if the DPI is 120 , DpiScaleX would be 1.25 (120/96).
So 96 [DPI] (Dots per inch) is by Microsoft definition 100% scaling. You have on first monitor 144/96=1.5 i.e.: 150%
anhnha wrote:
29 Dec 2022, 06:09
Do you know how to make it always on top?
I don't know javascript, but you can use standard AutoHotkey command: WinSet AlwaysOnTop,, ahk_exe chrome.exe
anhnha
Posts: 116
Joined: 08 Aug 2018, 02:46

Re: Why does this page.setrect(x,y, w,h) not work properly?

29 Dec 2022, 09:52

Thanks.
I don't know javascript, but you can use standard AutoHotkey command: WinSet AlwaysOnTop,, ahk_exe chrome.exe
I don't think this is javascript thing?

Code: Select all

 WinSet AlwaysOnTop,, ahk_exe chrome.exe
The problem with this is that I don't want to do this for all chrome windows. I only want apply always on top to the chrome window which is opened by Rufaydium.
User avatar
rommmcek
Posts: 1480
Joined: 15 Aug 2014, 15:18

Re: Why does this page.setrect(x,y, w,h) not work properly?

29 Dec 2022, 10:38

You're right, those arguments are Chromium Command Line Switches, but it seems there is no "AlwaysOnTop" switch.
WinSet AlwaysOnTop,, ahk_exe chrome.exe will affect only the top most chrome window, so you have to ensure that e.g. via WinWaitActive ahk_exe chrome.exe or still better using WinTitle as a parameter, if you know it in advance.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 219 guests