Why isn’t MapDialogRect mapping dialog rectangles?

Raymond Chen

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.

3 comments

Discussion is closed. Login to edit/delete existing comments.

  • skSdnW 0

    “determine the average character dimensions” based on? A-Z?

  • Joshua Hudson 0

    Surprising conclusion; I was expecting they didn’t handle WM_GETFONT message in their main window and wondered why it didn’t work.

Feedback usabilla icon