How to calculate dialog box units based on the current font in Visual C++

Article translations Article translations
Article ID: 145994 - View products that this article applies to.
This article was previously published under Q145994
Note Microsoft Visual C++ .NET (2002) supports both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model. The information in this article applies only to unmanaged Visual C++ code.
Expand all | Collapse all

On This Page

SUMMARY

GetDialogBaseUnits() always assumes the system font when calculating dialog units. If you are using any font other than the system font, the return value may not be correct for your dialog box. This article offers two alternatives you can use to calculate dialog box units based on the current font.

MORE INFORMATION

The horizontal base unit is equal to the average width (in pixels) of the characters in the system font. The Help for GetDialogBaseUnits() suggests the following code for calculating the correct units:
pixelX = (dialogunitX * baseunitX) / 4
pixelY = (dialogunitY * baseunitY) / 8
				

However, if you aren't using the system font, you need to do some additional calculations to get the right coordinates. Here are two methods:

Method One

The vertical dialog box unit is equivalent to the character height. The horizontal dialog box unit is equivalent to the average character width of the dialog box's font. The average character width is calculated by finding the average text extent of the alphabetic character set. This is necessary because tmAveCharWidth is not precise. Use this code:
   void CAboutDlg::OnPaint()
   {
       CPaintDC dc(this); // device context for painting

       CFont* pFont = GetFont();
       CFont* oldFont = dc.SelectObject(pFont);
       TEXTMETRIC tm;
       dc.GetTextMetrics( &tm );
       int baseUnitY = tm.tmHeight;
       CSize size;
       size = dc.GetTextExtent(
           "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", 52);
       int baseUnitX = (size.cx / 26 + 1) / 2;
       TRACE("baseUnitX = %d\n", baseUnitX);
       TRACE("baseUnitY = %d\n", baseUnitY);
       dc.SelectObject(oldFont);

   }
				

Method Two

The alternative is even easier. Using MapDialogRect() with a coordinate of x = 4, y = 8 will return the dialog box base units regardless of the dialog box's font. However, in most situations, the programmer doesn't need to know the dialog box base units. MapDialogRect() can be used directly to convert any dialog box units directly to device coordinates. The following sample code shows how it can be used to find the dialog box base units:
   void CAboutDlg::OnPaint()
   {
       CPaintDC dc(this); // device context for painting

       CRect rc( 0, 0, 4, 8 );

       MapDialogRect( &rc );
       int baseUnitY = rc.bottom;
       int baseUnitX = rc.right;
       TRACE("baseUnitX = %d\n", baseUnitX);
       TRACE("baseUnitY = %d\n", baseUnitY);

   }
				

REFERENCES

For additional information, please see the following article in the Microsoft Knowledge Base:
125681 How To Calculate Dialog Base Units with Non-System-Based Font

Properties

Article ID: 145994 - Last Review: November 21, 2006 - Revision: 4.1
APPLIES TO
  • Microsoft Foundation Class Library 4.2, when used with:
    • Microsoft Visual C++ 1.0 Professional Edition
    • Microsoft Visual C++ 1.5 Professional Edition
    • Microsoft Visual C++ 1.51
    • Microsoft Visual C++ 1.52 Professional Edition
    • Microsoft Visual C++ 4.2 Enterprise Edition
    • Microsoft Visual C++ 5.0 Enterprise Edition
    • Microsoft Visual C++ 6.0 Enterprise Edition
    • Microsoft Visual C++ 2.0 Professional Edition
    • Microsoft Visual C++ 2.1
    • Microsoft Visual C++ 6.01 Professional Edition
    • Microsoft Visual C++ 4.0 Professional Edition
    • Microsoft Visual C++ 4.1 Subscription
    • Microsoft Visual C++ 4.2 Professional Edition
    • Microsoft Visual C++ 5.0 Professional Edition
    • Microsoft Visual C++ 6.0 Professional Edition
    • Microsoft Visual C++ 5.0 Learning Edition
    • Microsoft Visual C++ 5.0 Learning Edition
    • Microsoft Visual C++, 32-bit Learning Edition 6.0
    • Microsoft Visual C++ .NET 2002 Standard Edition
    • Microsoft Visual C++ .NET 2003 Standard Edition
Keywords: 
kbhowto kbinfo kbfont kbuidesign kbdlg kbcode KB145994

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com