Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. DPI aware, I am confused

DPI aware, I am confused

Scheduled Pinned Locked Moved C / C++ / MFC
c++graphicsregextutorialquestion
9 Posts 3 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Erich Jarz
    wrote on last edited by
    #1

    Hi I read some articles on how to make a win32 prog DPI aware. I want to make a dialog which has windows controls, bitmaps and gdiplus objects combined. I am using VS2019 and started a MFC app from scratch. What would be the right way to get a DPI aware app, because I set DPI to high in the MFC app setting and used GetDeviceCaps to draw the lines. But when DPI is set to HIGH the lines do not match with the controls anymore. When I switch it off, the graphics look blurry, but are on the right position. I made some screenshots to show what I mean:[

    Windows 100%, DPI aware to HIGH

    ](http://public.stattegg.info/_old/dpiyes_100.jpg)[

    Windows 150%, DPI aware to HIGH

    ](http://public.stattegg.info/_old/dpiyes_150.jpg)[

    Windows 150%, DPI aware to NO

    ](http://public.stattegg.info/_old/dpino_150.jpg)What would be the right approach, so windows controls and graphics match on all resolutions? thanks regards Erich

    L 2 Replies Last reply
    0
    • E Erich Jarz

      Hi I read some articles on how to make a win32 prog DPI aware. I want to make a dialog which has windows controls, bitmaps and gdiplus objects combined. I am using VS2019 and started a MFC app from scratch. What would be the right way to get a DPI aware app, because I set DPI to high in the MFC app setting and used GetDeviceCaps to draw the lines. But when DPI is set to HIGH the lines do not match with the controls anymore. When I switch it off, the graphics look blurry, but are on the right position. I made some screenshots to show what I mean:[

      Windows 100%, DPI aware to HIGH

      ](http://public.stattegg.info/_old/dpiyes_100.jpg)[

      Windows 150%, DPI aware to HIGH

      ](http://public.stattegg.info/_old/dpiyes_150.jpg)[

      Windows 150%, DPI aware to NO

      ](http://public.stattegg.info/_old/dpino_150.jpg)What would be the right approach, so windows controls and graphics match on all resolutions? thanks regards Erich

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      In Windows 10 you make your apps "fluent" and don't worry about that stuff. The user controls text size through "Ease of Access" in the control panel. Stick to Segoe UI fonts; use the "standard resources" for headings and body; size everything divisible by 4; stick to PNGs, using transparent backgrounds when necessary to reduce size.

      It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

      E 1 Reply Last reply
      0
      • L Lost User

        In Windows 10 you make your apps "fluent" and don't worry about that stuff. The user controls text size through "Ease of Access" in the control panel. Stick to Segoe UI fonts; use the "standard resources" for headings and body; size everything divisible by 4; stick to PNGs, using transparent backgrounds when necessary to reduce size.

        It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

        E Offline
        E Offline
        Erich Jarz
        wrote on last edited by
        #3

        Hi Thanks, but that didnt answer my question. How should I draw Gdiplus graphics and controls so that they are always drawn correctly (same position) on all Dpi settings? The code that I use can be seen in the first screenshot. Regards Erich

        L L 2 Replies Last reply
        0
        • E Erich Jarz

          Hi I read some articles on how to make a win32 prog DPI aware. I want to make a dialog which has windows controls, bitmaps and gdiplus objects combined. I am using VS2019 and started a MFC app from scratch. What would be the right way to get a DPI aware app, because I set DPI to high in the MFC app setting and used GetDeviceCaps to draw the lines. But when DPI is set to HIGH the lines do not match with the controls anymore. When I switch it off, the graphics look blurry, but are on the right position. I made some screenshots to show what I mean:[

          Windows 100%, DPI aware to HIGH

          ](http://public.stattegg.info/_old/dpiyes_100.jpg)[

          Windows 150%, DPI aware to HIGH

          ](http://public.stattegg.info/_old/dpiyes_150.jpg)[

          Windows 150%, DPI aware to NO

          ](http://public.stattegg.info/_old/dpino_150.jpg)What would be the right approach, so windows controls and graphics match on all resolutions? thanks regards Erich

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          I am not sure how you are drawing that button, but using the same co-ordinates as you use for the red lines should get it to the right place. See Writing DPI Aware Applications | Windows Vista | Microsoft Windows[^]

          1 Reply Last reply
          0
          • E Erich Jarz

            Hi Thanks, but that didnt answer my question. How should I draw Gdiplus graphics and controls so that they are always drawn correctly (same position) on all Dpi settings? The code that I use can be seen in the first screenshot. Regards Erich

            L Offline
            L Offline
            leon de boer
            wrote on last edited by
            #5

            Short answer you scale everything long answer you scale everything Okay to get scale factor you need the DPI of the screen which you use Win32 API

            HDC Dc = GetDC(GetDesktopWindow()); // Get Screen DC
            int DpiX = GetDeviceCaps(Dc, LOGPIXELSX); // X DPI for this DC (96 for normal screen)
            int DpiY = GetDeviceCaps(Dc, LOGPIXELSY); // Y DPI for this DC (96 for normal screen)
            ReleaseDC(GetDesktopWindow(), Dc); // Release screen DC finished with it now
            if (DpiX != 96 || DpiY != 96) // High DPI mode active
            {
            /* calculate scale */
            double ScaleX = (double)DpiX/96;
            double ScaleY = (double)DpiY/96;
            }

            In vino veritas

            E 1 Reply Last reply
            0
            • L leon de boer

              Short answer you scale everything long answer you scale everything Okay to get scale factor you need the DPI of the screen which you use Win32 API

              HDC Dc = GetDC(GetDesktopWindow()); // Get Screen DC
              int DpiX = GetDeviceCaps(Dc, LOGPIXELSX); // X DPI for this DC (96 for normal screen)
              int DpiY = GetDeviceCaps(Dc, LOGPIXELSY); // Y DPI for this DC (96 for normal screen)
              ReleaseDC(GetDesktopWindow(), Dc); // Release screen DC finished with it now
              if (DpiX != 96 || DpiY != 96) // High DPI mode active
              {
              /* calculate scale */
              double ScaleX = (double)DpiX/96;
              double ScaleY = (double)DpiY/96;
              }

              In vino veritas

              E Offline
              E Offline
              Erich Jarz
              wrote on last edited by
              #6

              Hi Thanks for your reply. I already tried to calculate the coord with GetDeviceCaps, but that does not work correctly! When I set 150% font size in windows I get a ratio of 144/96=1.5 as expected, but the controls and the dialog itself do not scale that way! Here a different example: I have drawn two red rectangles, one with the size of the dialog and an inner one which is the size of a group box. For the size of the dialog rectangle I use GetClientRect. It works on all font sizes (DPI scalings) correct. For the inner rectangle inside the group box I tried to recalculate the coords with GetDeviceCaps, but that does not work. If I change scaling in windows the inner rectangle is completely off. So I tried to figure out the correct coords for the inner rectangle by trial and error. Below are the coords which draw the correct rectangles in relation to the group box. But how can I calculate them, I get some weird values? In the x axis I would get a factor of 1.3328 and on the y axis a factor of 1.62 with 150% scaling, not 1.5 as expected!? Screenshot at 100% // coords at 100% Dialog rectangle coord = 0/0 - 1445/832 group box rectangle coord = 624/9 - 1247/581 Screenshot at 125% // coords at 125% Dialog rectangle coord = 0/0 - 1685/1088 group box rectangle coord = 728/11 - 1456/761 Screenshot at 150% // coords at 150% Dialog rectangle coord = 0/0 - 1926/1344 group box rectangle coord = 831/13 - 1664/941 How can I calculate the right coordinates for these elements ? thanks regards Erich

              L 1 Reply Last reply
              0
              • E Erich Jarz

                Hi Thanks, but that didnt answer my question. How should I draw Gdiplus graphics and controls so that they are always drawn correctly (same position) on all Dpi settings? The code that I use can be seen in the first screenshot. Regards Erich

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                My "answer" was, why bother when the OS / framework will do it for you. Besides dimensions, you have to scale positions (top, left), fonts, pixel fiddling. With a "ViewBox" (as one example) one can do in 1 minute what you'll be spending the next month on. Your graphics are "Border" elements within a Grid, inside a viewbox, with whatever relative dimensions you want.

                It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

                1 Reply Last reply
                0
                • E Erich Jarz

                  Hi Thanks for your reply. I already tried to calculate the coord with GetDeviceCaps, but that does not work correctly! When I set 150% font size in windows I get a ratio of 144/96=1.5 as expected, but the controls and the dialog itself do not scale that way! Here a different example: I have drawn two red rectangles, one with the size of the dialog and an inner one which is the size of a group box. For the size of the dialog rectangle I use GetClientRect. It works on all font sizes (DPI scalings) correct. For the inner rectangle inside the group box I tried to recalculate the coords with GetDeviceCaps, but that does not work. If I change scaling in windows the inner rectangle is completely off. So I tried to figure out the correct coords for the inner rectangle by trial and error. Below are the coords which draw the correct rectangles in relation to the group box. But how can I calculate them, I get some weird values? In the x axis I would get a factor of 1.3328 and on the y axis a factor of 1.62 with 150% scaling, not 1.5 as expected!? Screenshot at 100% // coords at 100% Dialog rectangle coord = 0/0 - 1445/832 group box rectangle coord = 624/9 - 1247/581 Screenshot at 125% // coords at 125% Dialog rectangle coord = 0/0 - 1685/1088 group box rectangle coord = 728/11 - 1456/761 Screenshot at 150% // coords at 150% Dialog rectangle coord = 0/0 - 1926/1344 group box rectangle coord = 831/13 - 1664/941 How can I calculate the right coordinates for these elements ? thanks regards Erich

                  L Offline
                  L Offline
                  leon de boer
                  wrote on last edited by
                  #8

                  You are obviously loading the dialog from a resource file or template. Those have a normal DPI scale of 48 and yes it gets messy. 1.) Don't do it .... manually create the dialog and insert the items. or 2.) If you don't want to do it manually on window entries that draw on the WM_CREATE grab the size and scale of the window just like you did. Which is basically what Gerry is saying below and contain your draws to windows. That means if you want to draw something on the dialog background make an transparent invisible window in the resource so it scales with the rest of the dialog and draw on it. You are sort of creating a mountain out of a molehill.

                  In vino veritas

                  E 1 Reply Last reply
                  0
                  • L leon de boer

                    You are obviously loading the dialog from a resource file or template. Those have a normal DPI scale of 48 and yes it gets messy. 1.) Don't do it .... manually create the dialog and insert the items. or 2.) If you don't want to do it manually on window entries that draw on the WM_CREATE grab the size and scale of the window just like you did. Which is basically what Gerry is saying below and contain your draws to windows. That means if you want to draw something on the dialog background make an transparent invisible window in the resource so it scales with the rest of the dialog and draw on it. You are sort of creating a mountain out of a molehill.

                    In vino veritas

                    E Offline
                    E Offline
                    Erich Jarz
                    wrote on last edited by
                    #9

                    Hi Thanks for your replies. I drew the controls with the MFC resource editor, so the scaling doesnt match as you said. So, I started a Win32 desktop application from scratch without MFC or other wrappers. I will do all drawings and scalings myself for all elements. I hope this will solve my DPI problems. I know that this is much work, but I only have experience with VisualStudio and MFC and I have to learn the basic WIN32 stuff first. I wanted to know the underlying win32 basics anyway. Thanks for your help! regards Erich

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups