Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
IntegralHeight Property
Message
From
02/08/2003 13:10:43
Larry Rix
Larry Rix & Associates, Inc.
Westminster, Colorado, United States
 
 
To
28/07/2003 14:10:31
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00814186
Message ID:
00816218
Views:
18
Fabio,

I did come up with a solution to problem. Briefly, it involved the TXTWIDTH() function with a experimentally calculated constant for converting to pixels. For example: TXTWIDTH("a", "arial", 8) = 1.200 in VFP. The return value is certainly not pixels. Thus, I decided to "eye-ball" a test string over the width of my label. Long story short, I came up with a constant value of 5.6667 average pixels per average character in the 8 point arial font type-face.

Thank you for pointing out the FONTMETRIC(1, "arial", 8) to me. This actually took me to FONTMETRIC(6, "arial", 8), which provides the constant value that I was getting by Kentucky-windage (being from Italy, you might have to look that one up! :) ).

Remembering that the original problem is to dynamically size the height of a control based on font metrics and then couple that with the IntergralHeight property, we might come up with one solution like the code at the end of this message.

Hopefully, someone else can use the solution as well and will find it successful.

By the way -- the solution below will actually end up in production code. The behaviour is when a user passes their mouse over the image control, the container that the image is, the container expands to reveal the edit box and the contained text. The text that is placed in the edit box is coming from an external source that there is no telling how much text will be there. Thus, the need for dynamic sizing of all the controls involved (i.e. edit box and container).

DEFINE CLASS form1 AS form

Top = 0
Left = 0
Height = 250
Width = 223
DoCreate = .T.
Caption = "Form1"
Name = "Form1"


ADD OBJECT container1 AS container WITH ;
Top = 12, ;
Left = 12, ;
Width = 200, ;
Height = 25, ;
BorderWidth = 0, ;
Name = "Container1"


ADD OBJECT form1.container1.img_button AS image WITH ;
Picture = "libopen.bmp", ;
Height = 17, ;
Left = 178, ;
Top = 4, ;
Width = 17, ;
Name = "img_button"


ADD OBJECT form1.container1.lab_hyperlink AS label WITH ;
Caption = "Hyperlink Label", ;
Height = 17, ;
Left = 5, ;
Top = 4, ;
Width = 163, ;
Name = "lab_hyperlink"


ADD OBJECT form1.container1.edit1 AS editbox WITH ;
Height = 126, ;
Left = 4, ;
ScrollBars = 0, ;
SpecialEffect = 1, ;
Top = 26, ;
Width = 176, ;
Value = "This is a comment designed to be really " + ;
"big so that that control must size itself to fit.", ;
IntegralHeight = .T., ;
Name = "Edit1"


PROCEDURE img_button.MouseLeave
LPARAMETERS nButton, nShift, nXCoord, nYCoord
THIS.Parent.Height = 25
ENDPROC


PROCEDURE img_button.MouseEnter
LPARAMETERS nButton, nShift, nXCoord, nYCoord
THIS.Parent.Height = ;
THIS.Parent.edit1.Top + ;
THIS.Parent.edit1.Height

return
ENDPROC


PROCEDURE edit1.Init
local ;
ln_average_text_width as Integer, ;
ln_average_font_width as Integer, ;
ln_average_value_width as Integer

ln_average_text_width = ;
txtwidth(THIS.Value, THIS.FontName, THIS.FontSize)

ln_average_font_width = ;
fontmetric(6, THIS.FontName, THIS.FontSize)

ln_average_value_width = ;
ln_average_text_width * ln_average_font_width

*-- This is the critical calculation, because it tells the
*-- control how large it should be to properly display the
*-- text value. The additional 5 is for good measure. The
*-- IntegralHeight propery now becomes a cleanup mechanism
*-- for our rough estimate. You may play with this constant
*-- to see how well your text fits on a certain control.
*-- If you make this value a property on the control then
*-- you can add this code to a class that has the behaviour
*-- of dynamic sizing, but is "tunable" to the particular
*-- need at run-time.

THIS.Height = ;
ln_average_value_width / THIS.Width * ;
fontmetric(1, THIS.FontName, THIS.FontSize) + ;
fontmetric(1, THIS.FontName, THIS.FontSize) + 5

return
ENDPROC


ENDDEFINE
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform