This is what I use:
// GetDialogBaseUnits only works for the system font.
// MapDialogRect only works one way.
// ssDuToPixelsX by Sam Steele. 2013-08-18.
// Returns the number of pixels corresponding to the number of
// horizontal dialog units for the specified dialog box.
// If the window is not a dialog box, then 0 is returned.
int ssDuToPixelsX(HWND Dialog, int DuX){
int PixelsX = 0;
if (IsWindow(Dialog) && GetClassLongPtr(Dialog, GCW_ATOM) == (ATOM)WC_DIALOG){
// If we go to 40 du then rounding in MapDialogRect is eliminated.
RECT r = {40, 0, 0, 0};
MapDialogRect(Dialog, &r);
// Add half of the divisor to the dividend for rounding to nearest.
PixelsX = (DuX * r.left + 20) / 40;
}
return PixelsX;
} // ssDuToPixelsX
// ssDuToPixelsY by Sam Steele. 2013-08-18.
// Returns the number of pixels corresponding to the number of
// vertical dialog units for the specified dialog box.
// If the window is not a dialog box, then 0 is returned.
int ssDuToPixelsY(HWND Dialog, int DuY){
int PixelsY = 0;
if (IsWindow(Dialog) && GetClassLongPtr(Dialog, GCW_ATOM) == (ATOM)WC_DIALOG){
// If we go to 80 du then rounding in MapDialogRect is eliminated.
RECT r = {0, 80, 0, 0};
MapDialogRect(Dialog, &r);
// Add half of the divisor to the dividend for rounding to nearest.
PixelsY = (DuY * r.top + 40) / 80;
}
return PixelsY;
} // ssDuToPixelsY
// ssPixelsToDuX by Sam Steele. 2013-08-18.
// Returns the number of horizontal dialog units corresponding
// to the number of pixels for the specified dialog box.
// If the window is not a dialog box, then 0 is returned.
int ssPixelsToDuX(HWND Dialog, int PixelsX){
int DuX = 0;
if (IsWindow(Dialog) && GetClassLongPtr(Dialog, GCW_ATOM) == (ATOM)WC_DIALOG){
// If we go to 20 or 40 du then rounding in MapDialogRect is eliminated.
RECT r = {20, 0, 40, 0};
MapDialogRect(Dialog, &r);
// Add half of the divisor to the dividend for rounding to nearest.
DuX = (PixelsX * 40 + r.left) / r.right;
}
return DuX;
} // ssPixelsToDuX
// ssPixelsToDuY by Sam Steele. 2013-08-18.
// Returns the number of vertical dialog units corresponding
// to the number of pixels for the specified dialog box.
// If the window is not a dialog box, then 0 is returned.
int ssPixelsToDuY(HWND Dialog, int PixelsY){
int DuY = 0;
if (IsWindow(Dialog) && GetClassLongPtr(Dialog, GCW_ATOM) == (ATOM)WC_DIALOG){
// If we go to 40 or 80 du then rounding in MapDialogRect is eliminated.
RECT r = {0, 40, 0, 80};
MapDialogRect(Dialog, &r);
// Add half of the divisor to the dividend for rounding to nearest.
DuY = (PixelsY * 80 + r.top) / r.bottom;
}
return DuY;
} // ssPixelsToDuY