October 21st, 2021

Why isn’t MapDialogRect mapping dialog rectangles?

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.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

3 comments

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

  • Joshua Hudson

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

  • skSdnW

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