Print This Post

5-Minute-Snack: Determine the vertical refresh rate in Hz of your display

Delphi has taken great strides in the RTL that make using direct Windows API calls more and more redundant. A lot of things can be determined using classes that can either be found in the RTL or VCL. However, the vertical refresh rate seems to have been overlooked as I cannot find it in any of the new classes of the RTL. Neither TScreen nor TMonitor deliver a solution.

First, the solution I provide will be valid for Windows 2000 upwards. Another 5-Minute-Snack will focus on how to get information about your operating system version using TOSVersion. I doubt that anyone still will have to check for versions older than that but better safe than sorry.

Next, you need to know that the Windows API function GetDeviceCaps can be used to retrieve information about a drawing context (DC). In the VCL, you get the DC using the Handle of the Canvas of the window: self.Canvas.Handle. The function also requires a second parameter to determine what kind of information you would like to know. Further, it always will return an integer. Sometimes, you need to refer to the documentation what this integer means. In this case, however, the integer determines the vertical refresh rate. We just need to know the constant for it. In order to request the vertical refresh rate, we can use the constant VREFRESH.

If we place a button and an edit field on a form, we can implement the OnClick-event of the button as follows:

Edit1.Text := 
   Format( '%d Hz', 
      [ GetDeviceCaps( self.Canvas.Handle, VREFRESH ) ] 

This way, the edit field will contain the refresh rate after clicking the button:

Tags: , , , , , , , ,