Centre Gui in middle of current monitor

Post your working scripts, libraries and tools.
robnicholson
Posts: 12
Joined: 07 May 2020, 07:38

Centre Gui in middle of current monitor

Post by robnicholson » 27 Apr 2023, 17:44

Sure somebody will have posted this before, but I've just converted some v1 code to centre a Gui in the middle of the current monitor:

Code: Select all

; GetCurrentMonitor: Gets the current monitor index based upon mouse position
GetCurrentMonitor() {
	CoordMode "Mouse", "Screen"
	MouseGetPos &MouseX, &MouseY
	NumMonitors := MonitorGetCount()
	Loop NumMonitors {
		MonitorGet A_Index, &MonitorLeft, &MonitorTop, &MonitorRight, &MonitorBottom
		If (MouseX >= MonitorLeft And MouseX < MonitorRight And MouseY >= MonitorTop And MouseY < MonitorBottom) 
			Return A_Index
	}
	Return 1 ; should never really execute
}

; ShowCentered: show Gui in centre of current monitor.
ShowCentered(Gui) {
	CurrentMonitor := GetCurrentMonitor()
	MonitorGet CurrentMonitor, &MonitorLeft, &MonitorTop, &MonitorRight, &MonitorBottom
	Gui.GetClientPos(&x, &y, &w, &h)
	x := Integer(((MonitorRight - MonitorLeft - w) / 2) + MonitorLeft)
	y := Integer(((MonitorBottom - MonitorTop - h) / 2) + MonitorTop)
	Gui.Show("x" x " y" y)
}
MedBooster
Posts: 54
Joined: 24 Nov 2022, 12:33

Re: Centre Gui in middle of current monitor

Post by MedBooster » 06 Nov 2023, 03:10

Thank you for the script.
It would have helped a lot if you showed what to write in the "Gui, Show" part of the script


I like that yours use the current monitor the *cursor* is in, as opposed to the monitor the active window is in

Could you maybe link to the V1 version of this script?

as I am getting this error trying to run it in V1, so I guess V1 does not read & the same way as V2
image.png
image.png (9.28 KiB) Viewed 698 times
MedBooster
Posts: 54
Joined: 24 Nov 2022, 12:33

Re: Centre Gui in middle of current monitor

Post by MedBooster » 14 Mar 2024, 07:59

This is working for me in V1, but I have to put the 2nd part separately above the first. See this post for an example of a script with the GUI centre function. viewtopic.php?f=6&t=122359


1st part

Code: Select all

;reference https://www.autohotkey.com/boards/viewtopic.php?t=31716
GetCurrentMonitorIndex(){
	CoordMode, Mouse, Screen
	MouseGetPos, mx, my
	SysGet, monitorsCount, 80

	Loop %monitorsCount%{
		SysGet, monitor, Monitor, %A_Index%
		if (monitorLeft <= mx && mx <= monitorRight && monitorTop <= my && my <= monitorBottom){
			Return A_Index
			}
		}
		Return 1
}

CoordXCenterScreen(WidthOfGUI,ScreenNumber)
{
SysGet, Mon1, Monitor, %ScreenNumber%
	return (( Mon1Right-Mon1Left - WidthOfGUI ) / 2) + Mon1Left
}

CoordYCenterScreen(HeightofGUI,ScreenNumber){
	SysGet, Mon1, Monitor, %ScreenNumber%
	return ((Mon1Bottom-Mon1Top - 30 - HeightofGUI ) / 2) + Mon1Top
}

GetClientSize(hwnd, ByRef w, ByRef h)
{
    VarSetCapacity(rc, 16)
    DllCall("GetClientRect", "uint", hwnd, "uint", &rc)
    w := NumGet(rc, 8, "int")
    h := NumGet(rc, 12, "int")
}

2nd part

Code: Select all

;---------GET CENTER OF CURRENT MONITOR---------
	;get current monitor index
	CurrentMonitorIndex:=GetCurrentMonitorIndex()
	;get Hwnd of current GUI
	DetectHiddenWindows On
	Gui, +LastFound
	Gui, Show, Hide
	GUI_Hwnd := WinExist()
	;Calculate size of GUI
	GetClientSize(GUI_Hwnd,GUI_Width,GUI_Height)
	DetectHiddenWindows Off
	;Calculate where the GUI should be positioned
	GUI_X:=CoordXCenterScreen(GUI_Width,CurrentMonitorIndex)
	GUI_Y:=CoordYCenterScreen(GUI_Height,CurrentMonitorIndex)
;------- / GET CENTER OF CURRENT MONITOR--------- 
	;SHOW GUI AT CENTER OF CURRENT SCREEN
	Gui, Show, % "x" GUI_X " y" GUI_Y, %Gui_Title% ;title of window
	Return
Post Reply

Return to “Scripts and Functions (v2)”