windowswinapidialog

Inconsistency in DLUs/pixels ratio


Microsoft claims that MapDialogRect converts DLUs to pixels using baseunitX/4 and baseunitY/8 coefficients returned by GetDialogBaseUnits.

However, I don't see these coefficients in my tests.

For example, I have a button of 50x14 DLUs in a dialog using MS Shell Dlg font (8x16). Display scaling is 100%, DPI awareness is not enabled for the process.

Calling GetDialogBaseUnits, I get 8 for X and 16 for Y, so the coefficients should be 2,2.

But calling MapDialogRect for a rectangle of 50x14 DLUs, I get a rectangle of 75x23 pixels, so the actual coefficients are 1.5,1.5. Button size in pixels is the same.

I tested this in XP, Win 7 and Win 10, with the same results.

Why the coefficients used by MapDialogRect don't correspond to base units returned by GetDialogBaseUnits?


Solution

  • According to the blog provided by Luke: GetDialogBaseUnits is a crock

    Since there is no HWND parameter to GetDialogBaseUnits, it doesn’t know which dialog box’s DLUs you want to retrieve.

    That is why you must use MapDialogRect. The MapDialogRect function accepts a dialog box handle so it can use the correct font.