Page 1 of 1

[Solved] Determining which monitor is primary

Posted: Sun Jan 15, 2023 5:24 am
by BarryG
Hi! On Windows, how can I tell which is the primary monitor in a dual-monitor setup? I've got the code below as a starting point.

Win 7 says Monitor 1 is on the left (when I click the "Identify" button), but the Control Panel shows Monitor 2 is on the left. Which is correct? And how can my PureBasic code know which monitor is the main one (like in the first image below)? Thanks!

Code: Select all

m=ExamineDesktops()

Debug "You have "+Str(m)+" monitors."

For n=0 To m-1
  Debug ""
  Debug "Monitor "+Str(n+1)+":"
  Debug "Name = "+DesktopName(n)
  Debug "Freq = "+Str(DesktopFrequency(n))+" Hz"
  Debug "Depth = "+Str(DesktopDepth(n))+"-bit"
  Debug "X = "+Str(DesktopX(n))
  Debug "Y = "+Str(DesktopY(n))
  Debug "W = "+Str(DesktopWidth(n))
  Debug "H = "+Str(DesktopHeight(n))
Next

Image


Image

Re: Determining which monitor is primary

Posted: Sun Jan 15, 2023 9:01 am
by JHPJHP
Hi Barry,

Have you looked at Windows Services & Other Stuff\Other_Stuff\MonitorStuff\GetMonitorInfo.pb?

From the MonitorEnumProc Procedure there is a flag (#MONITORINFOF_PRIMARY) to determine if the monitor is primary and a callback parameter (hMonitor) to return the handle.

Re: Determining which monitor is primary

Posted: Sun Jan 15, 2023 10:28 am
by BarryG
Thanks, JHPJHP! I just tried it and it matches what Win 7 identifies as Monitor 1. Awesome!

Re: Determining which monitor is primary

Posted: Sun Jan 15, 2023 12:19 pm
by Mijikai
Windows optional:

Code: Select all

Procedure.i GetPrimaryMonitor()
  Protected origin.q
  ProcedureReturn MonitorFromPoint_(origin,#MONITOR_DEFAULTTOPRIMARY)
EndProcedure

Re: Determining which monitor is primary

Posted: Sun Jan 15, 2023 12:36 pm
by freak
The PB Desktop library always returns the primary monitor at index 0 after ExamineDesktops(). This works on all platforms.
The index of the desktop. The first index always specifies the primary monitor. The first index value is zero.
https://www.purebasic.com/documentation ... ktopx.html

Re: Determining which monitor is primary

Posted: Sun Jan 15, 2023 12:40 pm
by BarryG
Thanks Mijikai and Freak!