Get Desktop Metrics In Multi-Display Setup
Posted: Mon Dec 05, 2016 7:22 am
This cross-platform (Windows, MacOS, Linux) routine determines:
1. the main desktop
2. the mouse position
3. the desktop that the mouse is hovering on, and
4. the desktop that the app is positioned in (based on app window x/y position)
Suggestions, improvements, and feedback are welcome! 
1. the main desktop
2. the mouse position
3. the desktop that the mouse is hovering on, and
4. the desktop that the app is positioned in (based on app window x/y position)
Code: Select all
;===========================================================
;
; getDesktopInfo() returns various metrics
; regarding the mouse, app, and desktops
;
; tested & working with PureBasic v5.73 LTS (x64) on:
; - MacOS Catalina 10.15.5
; - Linux Ubuntu 20.04.2 LTS
; - Windows 8.1 Professional
;
; by TI-994A - free to use, improve, share...
;
; 5th December 2016
;
; Updated: 23rd March 2021
;
;===========================================================
Structure xPoint
x.i
y.i
EndStructure
Structure desktop
app.xPoint
mouse.xPoint
appDesktop.i
mouseDesktop.i
mainDesktop.i
appWin.i
desktop.i
EndStructure
;just drop this routine into any application (requires the desktop structure above)
;and simply poll the relevant elements to determine the required desktop metrics
;
Procedure getDesktopInfo(*ds.desktop)
Protected desktops = ExamineDesktops()
Protected i, dx, dy, dw, dh
With *ds
\mouse\x = DesktopMouseX()
\mouse\y = DesktopMouseY()
\app\x = WindowX(\appWin)
\app\y = WindowY(\appWin)
For i = 0 To desktops -1
dx = DesktopX(i)
dy = DesktopY(i)
dw = DesktopWidth(i)
dh = DesktopHeight(i)
If \app\x >= dx And \app\x <= dx + dw And
\app\y >= dy And \app\y <= dy + dh
\appDesktop = i
EndIf
If \mouse\x >= dx And \mouse\x <= dx + dw And
\mouse\y >= dy And \mouse\y <= dy + dh
\mouseDesktop = i
If dx = 0 And dy = 0
\mainDesktop = i
EndIf
Break
EndIf
Next i
EndWith
EndProcedure
;demo
;
desktop.desktop
LoadFont(0, "Arial", 16)
SetGadgetFont(#PB_Default, FontID(0))
wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
mainWin = OpenWindow(#PB_Any, #PB_Ignore, #PB_Ignore,
500, 300, "Active Desktops", wFlags)
desktopInfo = TextGadget(#PB_Any, 50, 50, 400, 50, "")
mousePos = TextGadget(#PB_Any, 50, 120, 400, 50, "")
appPos = TextGadget(#PB_Any, 50, 190, 400, 50, "")
AddWindowTimer(mainWin, 0, 100)
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
appQuit = #True
Default
desktop\appWin = mainWin
getDesktopInfo(@desktop)
With desktop
SetGadgetText(desktopInfo, "Desktop " +
Str(\mainDesktop) + " is the main desktop")
SetGadgetText(mousePos, "The mouse is at " + Str(\mouse\x) + ", " +
Str(\mouse\y) + " on desktop " + Str(\mouseDesktop))
SetGadgetText(appPos, "The app is on desktop " + Str(\appDesktop))
EndWith
EndSelect
Until appQuit
