[Solved] Determining which monitor is primary

Windows specific forum
BarryG
Addict
Addict
Posts: 4173
Joined: Thu Apr 18, 2019 8:17 am

[Solved] Determining which monitor is primary

Post 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
Last edited by BarryG on Sun Jan 15, 2023 12:40 pm, edited 1 time in total.
User avatar
JHPJHP
Addict
Addict
Posts: 2257
Joined: Sat Oct 09, 2010 3:47 am

Re: Determining which monitor is primary

Post 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.

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
BarryG
Addict
Addict
Posts: 4173
Joined: Thu Apr 18, 2019 8:17 am

Re: Determining which monitor is primary

Post by BarryG »

Thanks, JHPJHP! I just tried it and it matches what Win 7 identifies as Monitor 1. Awesome!
User avatar
Mijikai
Addict
Addict
Posts: 1520
Joined: Sun Sep 11, 2016 2:17 pm

Re: Determining which monitor is primary

Post by Mijikai »

Windows optional:

Code: Select all

Procedure.i GetPrimaryMonitor()
  Protected origin.q
  ProcedureReturn MonitorFromPoint_(origin,#MONITOR_DEFAULTTOPRIMARY)
EndProcedure
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Determining which monitor is primary

Post 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
quidquid Latine dictum sit altum videtur
BarryG
Addict
Addict
Posts: 4173
Joined: Thu Apr 18, 2019 8:17 am

Re: Determining which monitor is primary

Post by BarryG »

Thanks Mijikai and Freak!
Post Reply