** Add a space to the value so the DT_CALCRECT will pick up the last CR DrawText(lhDC, toEdit.Value+" ", LEN(toEdit.Value)+1, @lqRect, ; BITOR(DT_WORDBREAK,DT_EDITCONTROL,DT_CALCRECT,DT_NOPREFIX)) ** ... ** If we change the height, refresh the control IF NOT toEdit.Height = lnCalcHeight toEdit.Height = lnCalcHeight toEdit.Refresh() ENDIFThe code below reflects this change
********************************************************** FUNCTION AutoSizeEditBox ********************************************************** ** Calcualte the height of the text displayed in an Editbox and ** adjust the height of the edit box to match the text height ** Parameters: ** toEdit - the editbox to calculate and adjust ** tnMinHeight - Optional, the minimum height the edit box should be adjusted, in pixels ** defaults to 20 ** tnMaxHeight - Optional, the maximum height the editbox should be adjusted, in pixels ** defaults to 999 LPARAMETERS toEdit AS EditBox, tnMinHeight, tnMaxHeight #DEFINE LOGPIXELSY 90 #DEFINE DT_WORDBREAK 0x0010 #DEFINE DT_EDITCONTROL 0x2000 #DEFINE DT_CALCRECT 0x0400 #DEFINE DT_NOPREFIX 0x0800 #DEFINE ANSI_CHARSET 0 #DEFINE OUT_DEFAULT_PRECIS 0 #DEFINE OUT_DEVICE_PRECIS 5 #DEFINE OUT_OUTLINE_PRECIS 8 #DEFINE CLIP_DEFAULT_PRECIS 0 #DEFINE CLIP_STROKE_PRECIS 2 #DEFINE DEFAULT_QUALITY 0 #DEFINE PROOF_QUALITY 2 #DEFINE DEFAULT_PITCH 0 #DEFINE FW_BOLD 700 && use to bold && Not sure how this is derived, but is seems accurate #DEFINE VFP_EDITPADDING 10 ** Initialize the DECLARES DECLARE Long CreateDC IN WIN32API String, String, String, String DECLARE Long DeleteDC IN WIN32API Long DECLARE Long SelectObject IN WIN32API Long hdc, Long hObject DECLARE Long GetDeviceCaps IN WIN32API Long, Long DECLARE Long DeleteObject IN WIN32API Long hObject DECLARE Long CreateFont IN WIN32API ; Long nHeight, Long nWidth,; Long nEscapement, Long nOrientation,; Long fnWeight, Long fdwItalic,; Long fdwUnderline, Long fdwStrikeOut,; Long fdwCharSet,; Long fdwOutputPrecision,; Long fdwClipPrecision,; Long fdwQuality,; Long fdwPitchAndFamily,; String lpszFace DECLARE Long DrawText IN WIN32API ; Long hDC,; String lpString,; Long nCount,; String @lpRect,; Long uFormat LOCAL lhDC, lhFont, lnLPHeight, lqRect, lnCalcHeight, lhOldFont, lnRectWidth tnMinHeight = EVL(tnMinHeight,20) tnMaxHeight = EVL(tnMaxHeight,999) ** Create a device context relative to the screen lhDC = CreateDC("DISPLAY",NULL,NULL,NULL) ** Calculate the vertical DPI (usually 96) lnLPHeight = GetDeviceCaps(lhDC,LOGPIXELSY) ** Create a GDI font object that matches the current font of the Editbox lhFont = CreateFont(-toEdit.FontSize*lnLPHeight/72, ; 0,0,0, ; IIF(toEdit.FontBold, FW_BOLD, 0), ; toEdit.FontItalic, ; toEdit.FontUnderline, ; toEdit.FontStrikethru, ; ANSI_CHARSET,OUT_OUTLINE_PRECIS, CLIP_STROKE_PRECIS, ; PROOF_QUALITY, DEFAULT_PITCH, ; toEdit.FontName+0h00) ** Assign the font to our DC (save the old font) lhOldFont = SelectObject(lhDC, lhFont) ** Calculate the current render width of the Editbox lnRectWidth = toEdit.Width ; - IIF(toEdit.ScrollBars= 2, SYSMETRIC(14), 0) ; - VFP_EDITPADDING ** Create a binary rectangle structure lqRect=0h+BINTOC(0,"4rs")+BINTOC(0,"4rs")+ ; BINTOC(lnRectWidth,"4rs")+ ; BINTOC(9999999,"4rs") ** Draw the text using DT_CALCRECT so it will update our rectangle ** structure with the rendered height ** Add a space to the value so the DT_CALCRECT will pick up the last CR DrawText(lhDC, toEdit.Value+" ", LEN(toEdit.Value)+1, @lqRect, ; BITOR(DT_WORDBREAK,DT_EDITCONTROL,DT_CALCRECT,DT_NOPREFIX)) ** Calculate the adjusted height based on min and max values lnCalcHeight = CTOBIN(SUBSTR(lqRect,13,4),"4rs")+10 lnCalcHeight = MAX(lnCalcHeight, tnMinHeight) lnCalcHeight = MIN(lnCalcHeight, tnMaxHeight) ** assign the height to the editbox ** If we change the height, refresh the control IF NOT toEdit.Height = lnCalcHeight toEdit.Height = lnCalcHeight toEdit.Refresh() ENDIF ** restore our font object or we could get a memory leak SelectObject(lhDC, lhOldFont) ** Cleanup DeleteObject(lhFont) DeleteDC(lhDC)