How to Calculate the Height of Edit Control to Resize It

Last reviewed: March 21, 1996
Article ID: Q124315
The information in this article applies to:
  • Microsoft Windows Software Development Kit (SDK), version 3.1
  • Microsoft Win32 Software Development Kit (SDK), versions 3.5 and 3.51

SUMMARY

When a program changes the font of an edit control, it must calculate the new height of the control so that text is displayed correctly. When an edit control contains a border, the control automatically adds white space around the text so the text won't interfere with the border.

This article shows by example how a program can modify the height of an edit control so that text displayed in the control looks right after the program changes the font.

MORE INFORMATION

The height of the control on creation is calculated as the height of the control's font plus half of the smaller of the height of the control's font or the height of the system font. You can use a function similar to the one below to calculate the new height of an edit control when the font in the control is changed.

Sample Code

void ResizeEdit(HWND hwndEdit, HFONT hNewFont)
{
   HFONT       hSysFont,
               hOldFont;
   HDC         hdc;
   TEXTMETRIC  tmNew,
               tmSys;
   RECT        rc;
   int         nTemp;

   //get the DC for the edit control
   hdc = GetDC(hwndEdit);

   //get the metrics for the system font
   hSysFont = GetStockObject(SYSTEM_FONT);
   hOldFont = SelectObject(hdc, hSysFont);
   GetTextMetrics(hdc, &tmSys);

   //get the metrics for the new font
   SelectObject(hdc, hNewFont);
   GetTextMetrics(hdc, &tmNew);

   //select the original font back into the DC and release the DC
   SelectObject(hdc, hOldFont);
   DeleteObject(hSysFont);
   ReleaseDC(hwndEdit, hdc);

   //calculate the new height for the edit control
   nTemp = tmNew.tmHeight + (min(tmNew.tmHeight, tmSys.tmHeight)/2) +
   (GetSystemMetrics(SM_CYEDGE) * 2);

   //re-size the edit control
   GetWindowRect(hwndEdit, &rc);
   MapWindowPoints(HWND_DESKTOP, GetParent(hwndEdit), (LPPOINT)&rc, 2);
   MoveWindow( hwndEdit,
               rc.left,
               rc.top,
               rc.right - rc.left,
               nTemp,
               TRUE);
}


Additional reference words: 3.10 3.50 win16sdk
KBCategory: kbprg kbcode
KBSubcategory: UsrCtl


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: March 21, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.