Code: Select all
Declare CenterWindow(hWnd, HMONITOR, WWidth, WHeight)
Define pt.Point
WWidth = 400
WHeight = 200
If OpenWindow(0, 0, 0, WWidth, WHeight, "Center window over multiple monitors...", #PB_Window_SystemMenu | #PB_Window_SizeGadget)
hWnd = WindowID(0)
Repeat
MyEvent = WindowEvent()
If GetCursorPos_(@pt)
HMONITOR = MonitorFromPoint_(PeekQ(@pt), #MONITOR_DEFAULTTONEAREST)
If HMONITOR <> HMONITOROLD
If CenterWindow(hWnd, HMONITOR, WWidth, WHeight)
HMONITOROLD = HMONITOR
Debug "Window changed..."
EndIf
EndIf
EndIf
Until MyEvent = #PB_Event_CloseWindow
EndIf
Procedure CenterWindow(hWnd, HMONITOR, WWidth, WHeight)
Protected Left, Top
Define mi.MONITORINFO
mi\cbSize = SizeOf(mi)
If GetMonitorInfo_(HMONITOR, @mi)
Left = mi\rcMonitor\left + (mi\rcMonitor\right - mi\rcMonitor\left - WWidth) / 2
Top = mi\rcMonitor\top + (mi\rcMonitor\bottom - mi\rcMonitor\top - WHeight) / 2
SetWindowPos_(hWnd, #Null, Left, Top, 0, 0, #SWP_NOSIZE | #SWP_NOZORDER | #SWP_NOACTIVATE)
Else
ProcedureReturn #False
EndIf
ProcedureReturn #True
EndProcedure