How to obtain the secondary screen physical size info or get the screen dpi directly in C++? RRS feed

  • Question

  • In my C++ project, there is a need for detection of the secondary screen dpi. So far, I can obtain the resolution, and according to the relationship of dpi between resolution and the screen physical size info, the dpi can be calculated. So, my question is, does anyone know how to obtain the secondary screen physical size info or even the direct way to get the screen dpi? It will be greatly appreciated if anyone helps.
    • Edited by mtjj_19 Thursday, September 5, 2019 7:52 AM
    Thursday, September 5, 2019 3:29 AM

All replies

  • Hello,

    have a look at this CMonitor class:

    Regards, Guido

    Thursday, September 5, 2019 5:50 AM
  • What do you mean by "slave screen"?  That's not a standard term.

    In general, Windows has absolutely no idea about the physical size of a monitor.  In some cases, your monitor will have a specific driver (INF) that can expose color format and size, but that's not normal.  Programs that need to align themselves with the physical world will display a ruler on the screen and have the user calibrate with a real-world ruler.  That's the only guaranteed method.

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Thursday, September 5, 2019 6:04 AM
  • Hi,

    Thank you for psting here.

    >>So, my question is, does anyone know how to obtain the slave screen physical size info or even the direct way to get the screen dpi? It will be greatly appreciated if anyone helps.

    According to the issue, I wonder if you mean you want to btain the screen physical size info or get the screen dpi directly? If so. As far as I'm concerned it's not possible in the general case.

    Windows does not know the physical screen size. Windows may know how many pixels your screen has, but it doesn't know how big the screen actually is. You could get the true physical dimensions and DPI for a printer (assuming the driver isn't lying), but not for a screen. At least not reliably.

    The physical size of the monitor are stored in the EDID, but not all monitors provide EDID information.

    I suggest you could refer to the links:

    Best Regards,

    Jeanine Zhang

    Thursday, September 5, 2019 6:39 AM
  • If you are running Windows 8 or earlier, just set your application to be DPI aware and use any of the methods for calculating the system DPI. Since there is no per monitor DPI available for Windows 8 or earlier then just calculating the DPI for the system will work.

    If you are running Windows 8.1 or later, there is GetDpiForMonitor for Windows 8.1 and GetDpiForWindow available from Windows 10. Your application must be set as per monitor DPI aware for these to work.

    Since it is a UI thing, there is no display setting that governs the DPI, this means that the only way to do this is to query the DPI from one of the given methods. The issue is that Windows will lie to the application and tell it that it is at 96 DPI if the application doesn't state that it can handle it. In this case Windows truly does treat it as 96 DPI and then stretches it to fit the window.

    This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    Thursday, September 5, 2019 8:15 AM
  • Thanks everyone for helping me understand the problem. Finally, I uncovered my confusion with the using of two functions: MonitorFromWindow and GetMonitorInfo. The first function tells me which monitor the concerning window is on, while the second returns the monitor's working rectangle coordinate in MONITORINFO, which is helpful for locating the concerning window. Many thanks, wish all of you have  a good day.

    Friday, September 6, 2019 6:46 AM
  • Hi,

    I am glad you have got your solution and thanks for your sharing, I would appreciate it if you mark them as answer and this will be beneficial to other community.

    Best Regards,

    Jeanine Zhang

    Friday, September 6, 2019 8:59 AM