3

I use the following to get the physical width and height of a monitor (e.g., in millimeters or inches)

dimX = GetDeviceCaps(hDC, DevCap.HORZSIZE)
dimY = GetDeviceCaps(hDC, DevCap.VERTSIZE)

It appears to only work for Windows10, and not Windows7

Can someone confirm?

VB6 code

Private Sub Command1_Click()

    Dim hWnd As Long
    Dim hMonitor As Long
    Dim tMonitorInfo As MONITORINFOEX
    Dim hDC As Long

    hWnd = GetActiveWindow()
    hMonitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTONULL)
    
    tMonitorInfo.cbSize = Len(tMonitorInfo)
    Call GetMonitorInfo(hMonitor, tMonitorInfo)
    
    hDC = CreateDC(tMonitorInfo.szDevice, 0, 0, 0)

    Dim dimX As Double  'monitor width  in millimeters
    Dim dimY As Double  'monitor height in millimeters

    dimX = GetDeviceCaps(hDC, DevCap.HORZSIZE)
    dimY = GetDeviceCaps(hDC, DevCap.VERTSIZE)

    MsgBox "monitor size = " & _
           Round(dimX / 25.4, 2) & """" & " x " & _
           Round(dimY / 25.4, 2) & """"
    
    DeleteDC (hDC)
    
End Sub
m s
  • 39
  • 2
  • 2
    This answer might help: https://stackoverflow.com/a/4631379/4641116 – Eljay May 10 '22 at 17:22
  • Thanks, but that solution is for monitor resolution in pixels, not in millimeters or inches as I need – m s May 10 '22 at 17:36
  • 1
    I think that call will use whatever EDID supplies, or if not supplied by EDID it will calculate it from resolution using some fallback (60ppi? 72? 96?). Ergo, it isn't a reliable accurate value. If a precise value is needed, the user will have to enter the value for the monitor at a given resolution. (Just like we used to have to do back with X11.) You may find [dump EDID](https://www.nirsoft.net/utils/dump_edid.html) utility of interest. – Eljay May 10 '22 at 17:53
  • I have code to get these values from EDID also, but these don't seem to always be 100% accurate either. All the monitors that I test on Windows10 report dimensions CORRECTLY, and only on Windows7 machines do I get incorrect values, so I thought others might be able to either confirm or refute my finding. – m s May 10 '22 at 21:17
  • @ms You did not explain where `hDC` is coming from, or whether your app is DPI-aware or not. Please provide a [mcve]. – Remy Lebeau May 10 '22 at 21:43
  • From a quick bit of googling: https://social.msdn.microsoft.com/Forums/vstudio/en-US/6f06fa5e-1626-4668-b0ee-1f0d07e8d175/getdevicecapshorzsize-is-incorrect-in-windows-7?forum=vcgeneral - "Drawing measures are only accurate on printing devices, not on screen devices." but then https://stackoverflow.com/questions/30471954/physical-screen-size-acquired-by-getdevicecaps-is-not-the-actual-physical-size-o has some suggestions... – TheUndeadFish May 10 '22 at 23:56
  • I added code above. This (for me) always reports correct monitor size if on Windows10 machine, but always wrong on Windows7 machine. – m s May 11 '22 at 11:48

0 Answers0