Login  
Search All Forums
Dart Home | PowerTCP Emulation for .NET | Custom Development Reply | PowerTCP Emulation for .NET Topics | Forums   
AuthorForum: PowerTCP Emulation for .NET
Topic: VT Font resizing based on form size
zaphod

From: St-Eustache, QC Canada
Posts: 7
Member Since: 06/09/10
posted June 9, 2010 2:15 PM

Hello All,

The sample code that comes with the VT control shows how to resize the form based on the VT font size. I am trying to acomplish the reverse. For example, say I have a VT control on a form, which is docked to fill the form, I want the VT font size to dynamically change based on the size of the VT control on the form. Does anyone have sample code that can accomplish this ?
Nick B (Admin)

From: Utica, NY USA
Posts: 619
Member Since: 05/25/10

Extra Support Options
Custom Application Development

posted June 10, 2010 9:52 AM

Hello,

This information may be found in the help documentation under 'Emulation Overview.' The last two examples on that page demonstrate this.
zaphod

From: St-Eustache, QC Canada
Posts: 7
Member Since: 06/09/10
posted June 10, 2010 11:06 AM

I have tried the sample code in the documentation. It does not work well at all. If you make the form bigger, and then make it smaller, the font size just keeps getting bigger.

Essentially, I am trying to change the font to make sure I always see 80 columns by 24 rows. The width is more important in this case.

I have code that comes close to this but it is not perfect:
Private Sub ResizeFont()
    'Calculate new Font Size
    Dim newSize As Single = (Vt1.Size.Width / Vt1.Columns)
    Dim newHeight As Single
    'Set FontEx to use new Font Size
    Dim f As Font = New Font(Vt1.Font.Name, newSize, GetFontStyle(Vt1.Font.Bold, Vt1.Font.Italic))
    Vt1.Font = f

    'If new font is too big to fit 24 rows in vt1 height than change font size
    newHeight = (Vt1.Rows * Vt1.CellSize.Height) + Vt1.Padding.Top
    Do While newHeight > Me.Height
      newSize -= 1
      f = New Font(Vt1.Font.Name, newSize, GetFontStyle(Vt1.Font.Bold, Vt1.Font.Italic))
      Vt1.Font = f
      newHeight = (Vt1.Rows * Vt1.CellSize.Height) + Vt1.Padding.Top
    Loop

    'Resize VT based on size of font
    Vt1.Height = (Vt1.Rows * Vt1.CellSize.Height)
zaphod

From: St-Eustache, QC Canada
Posts: 7
Member Since: 06/09/10
posted June 10, 2010 11:11 AM

I forgot to mention that I do not dock the VT control, instead I anchor it to Top,Left and Right edges and I calculate the height of the VT control based on the code mentionned earlier.
Nick B (Admin)

From: Utica, NY USA
Posts: 619
Member Since: 05/25/10

Extra Support Options
Custom Application Development

posted June 10, 2010 12:18 PM

You may need to modify this to suit your specific circumstances, but we found this to work for another of our customers in the vt1_resize event:

Dim g As Graphics = CreateGraphics()
Dim cellWidth As Single = CSng(Math.Floor(((vt1.Size.Width - (vt1.CellSize.Width + 12)) / CDbl(vt1.Columns))))
Dim cellHeight As Single = CSng(Math.Floor(vt1.Size.Height / CDbl(vt1.Rows)))
Dim fontSize As Single = vt1.Font.Size - 2
Dim s As SizeF = New SizeF(cellWidth, cellHeight)
Dim s1 As SizeF = g.MeasureString("w", vt1.Font, 1000, StringFormat.GenericTypographic)
Dim Dir As Single
Dir = If(((s1.Width > s.Width) OrElse (s1.Height > s.Height)), -0.25F, 0.25F)
s = New SizeF()
Do
  fontSize += Dir
  Dim f As Font = New Font(vt1.Font.Name, fontSize, vt1.Font.Style)
  s = g.MeasureString("w", f, 1000, StringFormat.GenericTypographic)
Loop While If((Dir > 0), Math.Ceiling(s.Width) < cellWidth, Math.Ceiling(s.Width) > cellWidth)
vt1.Font = New Font(vt1.Font.Name, CInt(Fix(fontSize)), vt1.Font.Style)
vt1.CellSize = New Size(CInt(Fix(cellWidth)), CInt(Fix(cellHeight)))
vt1.Refresh()

If you have any display issues you may want to try playing around with these values:

Dir = If(((s1.Width > s.Width) OrElse (s1.Height > s.Height)), -0.25F, 0.25F)
to:
Dir = If(((s1.Width > s.Width) OrElse (s1.Height > s.Height)), -0.10F, 0.10F)
or this line:
vt1.CellSize = New Size(CInt(Fix(cellWidth)), CInt(Fix(cellHeight)))
to:
vt1.CellSize = New Size(CInt(Fix(cellWidth)), CInt(Fix(s.Height)))
The top change will likely cause the resizing to happen slower, but the results may be smoother. The second line will result in more blank space at the bottom of the VT window.
zaphod

From: St-Eustache, QC Canada
Posts: 7
Member Since: 06/09/10
posted June 10, 2010 2:16 PM

Thanks Nick,

That was very helpful.

It's all good now.
Reply | PowerTCP Emulation for .NET Topics | Forums   
This site is powered by PowerTCP WebServer Tool PowerTCP WebServer for ActiveX