Get Monitor or Desktop Refresh rate From Window ID

Share your advanced PureBasic knowledge/code with the community.
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Get Monitor or Desktop Refresh rate From Window ID

Post by Rescator »

What the hell is this thing? Well simply put, it returns the refresh rate of the current desktop or monitor that a window is on.

To see it in action (and if you happen to have more than 1 monitor), move the window between monitors.
The displayed refresh rate should change, but this will only happen if your monitors have different refresh rates set obviously.

What is this procedure good for anyway?
Well, it might be nice to sync visual updates/drawing with the refresh rate of the desktop or monitor the program window is currently on.


There may be an alternative way to get this info using even less code,
but the quick tests I did on Window DC's didn't work, the window stats was always showing the same refresh rate for some reason.

It's a shame there is no way to do a GetDeviceCaps on a HMON handle or similar.

Anyway, it is possible to make a platform independent variant of this procedure, but I'm too lazy to replicate the behavior of MonitorFromWindow_(hwnd,#MONITOR_DEFAULTTONEAREST) and messy RECT calculations. ;)

Who knows, maybe you can bribe freak or Fred to make a GetWindowRefresh() that behaves similar to this source :P

Code: Select all

EnableExplicit

#MONITORINFOF_PRIMARY=1
#MONITOR_DEFAULTTONULL=0
#MONITOR_DEFAULTTOPRIMARY=1
#MONITOR_DEFAULTTONEAREST=2
Structure MONITORINFOEX
 cbSize.l
 rcMonitor.RECT
 rcWork.RECT
 dwFlags.l
 szDevice.s{#CCHDEVICENAME}
EndStructure
Procedure.i GetMonitorRefreshFromWindow(window.i)
 Protected result.i=#Null,hwnd.i,hmon.i,mi.MONITORINFOEX,n.i,i.i
 If IsWindow(window)
  hwnd=WindowID(window)
  mi\cbSize=SizeOf(mi)
  hmon=MonitorFromWindow_(hwnd,#MONITOR_DEFAULTTONEAREST)
  If hmon
   If GetMonitorInfo_(hmon,mi)
    n=ExamineDesktops()-1
    For i=0 To n
     If mi\szDevice=DesktopName(i)
      result=DesktopFrequency(i)
     EndIf
    Next
   EndIf
  EndIf
 EndIf
 ProcedureReturn result ;If #null, there was an error or monitor not found.
EndProcedure

#Window_Main=1
Define.i event,quit,i.i,fEnabled.l,hz.i,oldhz.i
If OpenWindow(#Window_Main,100,200,195,260,"PureBasic Window",#PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget)

 oldhz=0

 Repeat
  event=WaitWindowEvent()
  If event=#PB_Event_CloseWindow
   quit=1
  ElseIf event=#PB_Event_MoveWindow
   hz=GetMonitorRefreshFromWindow(#Window_Main)
   If hz<>oldhz
    oldhz=hz
    Debug Str(hz)+" Hz"
   EndIf
  EndIf
 Until quit=1
EndIf
User avatar
Tomi
Enthusiast
Enthusiast
Posts: 270
Joined: Wed Sep 03, 2008 9:29 am

Post by Tomi »

Thanks Rescator
work nice on XP
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

rescator wrote:maybe you can bribe freak or Fred to make a GetWindowRefresh()
May I ask how this differs from the desktop lib's DesktopFrequency() command?
BERESHEIT
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

netmaestro wrote: May I ask how this differs from the desktop lib's DesktopFrequency() command?
late night and too much coffee? :)
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Oh, it's using it. I didn't look that close.
BERESHEIT
Post Reply