mfcbackwards-compatibilitydpi-aware

MFC picture control changes size when DPI awareness disabled or running on Win7


I made an MFC app for my friend using VS2015 in Win10. It looks like this, which is exactly the same as in resource editor.

this.

But when he ran the app on his computer in Win7, the Bitmap image in Picture Control enlarges and covers up some text boxes below, which looks like this.

this.

After I searched and realized it may be related with DPI awareness. I disabled DPI-Awareness in property page of Manifest Tool and rebuilt. The same thing happened even when it runs in Win10.

this

Could someone help me explain the cause of this and find a solution to fix the size of the image control? Thanks.


Solution

  • The main problem is that a dialog from a resource is always measured in DLUs.

    And DLUs are calculated from the size of the font, that is used for the dialog.

    See this article how dialog base units are calculated.

    Now you have a static picture control that is sized in DLUs. The bitmap is just scaled in pixels and is never resized, when you assign it to a static dialog control. And because the real size of the static control depends on the used font, you get different layouts for your dialog and your bitmap.

    And because just the font changes when you choose no DPI awareness and because the font changes from windows version to windows version your dialog always look different.

    Advice: Paint you picture your own and stretch it accordingly.

    Also this stackoverflow question is nice documents and shows the effect of DLUs.

    And here some code for auto sizeing picture controls.

    1. An auto-sizing bitmap picture control
    2. A simple image preview class using GDI+
    3. CxImage