Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Auto Grow an Edit Box
Message
Information générale
Forum:
Visual FoxPro
Catégorie:
Gestionnaire d'écran & Écrans
Versions des environnements
Visual FoxPro:
VFP 9 SP1
OS:
Windows XP SP2
Network:
Windows 2003 Server
Database:
Visual FoxPro
Divers
Thread ID:
01351074
Message ID:
01351228
Vues:
14
Hi John,

Here is a technique that calculates the height similarly to the way it is being rendered onto the screen. I tried to simplify it, but this is about as minimalistic as it gets, unfortunately.

Add this line of code to your Editbox InteractiveChange event (you may need to add it to the Refresh and ProgramaticChange events too, play around and see). Set the MinHeight and MaxHeight to your desired values.
FUNCTION InteractiveChange
#DEFINE MinHeight 40
#DEFINE MaxHeight 500
AutoSizeEditBox(This, MinHeight, MaxHeight)
Then put this function somewhere your Editbox can find it. or just add it to your Editbox base class:
**********************************************************
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
DrawText(lhDC, toEdit.Value, LEN(toEdit.Value), @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
toEdit.Height = lnCalcHeight

** restore our font object or we could get a memory leak
SelectObject(lhDC, lhOldFont)
** Cleanup
DeleteObject(lhFont)
DeleteDC(lhDC)
There are a couple of annoyances, like it doesn't always scroll to the top when you paste. But this should give you a good starting point and you can tweak as needed
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform