How to Calculate the Height of Edit Control to Resize ItLast reviewed: March 21, 1996Article ID: Q124315 |
The information in this article applies to:
SUMMARYWhen 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 INFORMATIONThe 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
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |