2

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.

ricecakebear
  • 301
  • 4
  • 15
  • 2
    For a solution, you can put a hidden static control in where the picture is supposed to be. Override dialog's `OnPaint` and draw the bitmap using `StretchBlt`. You would need `m_static.GetWindowRect(rect)` and `ScreenToClient(rect)` to find the right coordinates. – Barmak Shemirani Sep 21 '17 at 08:49

2 Answers2

3

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
xMRi
  • 14,982
  • 3
  • 26
  • 59
  • 2
    Could you elaborate a little more on how to "paint my own picture and stretch" with my bmp file? Thank you. – ricecakebear Sep 21 '17 at 01:04
  • Just serach. I just started google and found: [An auto-sizing bitmap picture control](https://www.codeproject.com/Articles/32/An-auto-sizing-bitmap-picture-control) – xMRi Sep 21 '17 at 06:14
  • 2: [A simple image preview class using GDI+](https://www.codeproject.com/articles/6354/a-simple-image-preview-class-using-gdi) and 3. [CxImage](https://www.codeproject.com/Articles/1300/CxImage) Changed my answer – xMRi Sep 21 '17 at 06:17
1

Normally, I prefer to keep control in my hand by using SetWindowPos() to set the size of image I want in different situations. You can use below two lines to control/set position and size of your image.

Assume ID of the Picture Control is IDC_STATIC2 then you can use like:

CStatic * pStatic = (CStatic *) GetDlgItem(IDC_STATIC2);
pStatic->SetWindowPos(NULL,20,20,50,50,0);
MKR
  • 19,739
  • 4
  • 23
  • 33