A customer wanted to convert dialog box units to pixels, and since GetÂDialogÂBaseÂUnits
is a crock, they switched to using MapÂDialogÂRect
. However, they found that MapÂDialogÂRect
wasn’t working either: It always returned FALSE
. What’s going on?
Upon closer inspection, what’s going on is that the customer’s code wasn’t passing a dialog box handle as the first parameter to the MapÂDialogÂRect
function. It was their program’s top-level window, which wasn’t a dialog.
The size of a dialog unit depends on the dialog, so the MapÂDialogÂRect
function needs to know which dialog’s dialog units you want use for the conversion.
By analogy, if you have instructions like “Go exactly 17 paces north,” and you want to convert that to meters, you need to know the stride of the person who wrote those directions.
If the thing you pass to the MapÂDialogÂRect
function isn’t even a dialog box at all, then the function fails with ERROR_
WINDOW_
NOT_
DIALOG
. That’s like asking someone to convert paces to meters, and saying “I got the measurements from that cave over there.” That cave tells you nothing about the stride length of the person who did the measuring.
If you don’t have a dialog box, then there’s nothing for MapÂDialogÂRect
to use as a basis for calculation. Either find (or create) a dialog box whose font matches the one you want to map, or replicate the calculations of MapÂDialogÂRect
without an actual dialog box: Get the font for your hypothetical dialog box and determine the average character dimensions for that font, and then plug them into the formulas given in the documentation.
Surprising conclusion; I was expecting they didn’t handle WM_GETFONT message in their main window and wondered why it didn’t work.
“determine the average character dimensions” based on? A-Z?
We’re doing it based on “A-Za-z” (see https://github.com/wxWidgets/wxWidgets/blob/1fb7b1381201c3a5aa0a9a975036dd2cde9e3da0/include/wx/private/window.h#L29), but this was found using empirically and could well be wrong, so I’d also like to hear from Raymond the real answer to this question. TIA!