Microsoft Small Basic

Program Listing:
Embed this in your website
' Measure Text Width in Pixels 0.3
' Copyright (c) 2014 Nonki Takahashi. All rights reserved.
'
' History:
' 2014-03-14 Considered FontBold and FontItalic. (KTK906-1)
' 2014-02-17 Adopted for Silverlight. (KTK906-0)
' 2014-02-13 Created. (KTK906)
'
GraphicsWindow.Title = "Measure Text Width in Pixels 0.3"
Not = "False=True;True=False"
Init()
Main()
Sub Init
  gw = 598
  gh = 428
  GraphicsWindow.Width = gw
  GraphicsWindow.Height = gh
  GraphicsWindow.BrushColor = "LightGray"
  yc = 140
  GraphicsWindow.FillRectangle(0, 0, gw, yc)
  GraphicsWindow.BrushColor = "Black"
  GraphicsWindow.DrawText(10, 10, "Text")
  txt = Controls.AddTextBox(50, 10)
  Controls.SetTextBoxText(txt, "test")
  GraphicsWindow.DrawText(10, 40, "Size")
  size = Controls.AddTextBox(50, 40)
  Controls.SetTextBoxText(size, GraphicsWindow.FontSize)
  GraphicsWindow.DrawText(10, 70, "Font")
  font = Controls.AddTextBox(50, 70)
  Controls.SetTextBoxText(font, GraphicsWindow.FontName)
  GraphicsWindow.DrawText(260, 10, "Bold")
  bold = Controls.AddTextBox(300, 10)
  Controls.SetTextBoxText(bold, GraphicsWindow.FontBold)
  btn = Controls.AddButton("Measure", 10, 100)
  GraphicsWindow.DrawText(260, 70, "Width")
  width = Controls.AddTextBox(300, 70)
  Controls.SetTextBoxText(width, 0)
  clicked = "False"
  Controls.ButtonClicked = OnButtonClicked
EndSub
Sub OnButtonClicked
  clicked = "True"
EndSub
Sub Main
  While "True"
    If clicked Then
      GraphicsWindow.BrushColor = "White"
      GraphicsWindow.FillRectangle(0, yc, gw, gh - yc)
      fs = Controls.GetTextBoxText(size)
      GraphicsWindow.FontSize = fs
      GraphicsWindow.FontName = Controls.GetTextBoxText(font)
      GraphicsWindow.FontBold = Controls.GetTextBoxText(bold)
      GraphicsWindow.BrushColor = "Black"
      GraphicsWindow.DrawText(0, yc, "||")
      y0 = yc
      y1 = yc + fs
      x0 = 0
      x1 = fs * 2
      If gw < x1 Then
        x1 = gw - 1
      EndIf
      Measure()
      GraphicsWindow.BrushColor = "White"
      GraphicsWindow.FillRectangle(0, yc, gw, gh - yc)
      GraphicsWindow.BrushColor = "Gray"
      GraphicsWindow.FillRectangle(0, yc + fs, gw, 1)
      px0 = px
      str = "|" + Controls.GetTextBoxText(txt) + "|"
      GraphicsWindow.BrushColor = "Black"
      GraphicsWindow.DrawText(0, yc, str)
      x1 = fs * Text.GetLength(str)
      If gw < x1 Then
        x1 = gw - 1
      EndIf
      Measure()
      Controls.SetTextBoxText(width, (px - px0))
      clicked = "False"
    Else
      Program.Delay(100)
    EndIf
  EndWhile
EndSub
Sub Measure
  y = Math.Floor((y0 + y1) / 2)
  For x = x0 To x1
    color = GraphicsWindow.GetPixel(x, y)
    If Not[Text.EndsWith(color, "FFFFFF")] Then
      left = x
      x = x1  ' break
    EndIf
  EndFor
  For x = x1 To x0 Step -1
    color = GraphicsWindow.GetPixel(x, y)
    If Not[Text.EndsWith(color, "FFFFFF")] Then
      right = x
      x = x0  ' break
    EndIf
  EndFor
  For x = right To x0 Step -1
    color = GraphicsWindow.GetPixel(x, y)
    If Text.EndsWith(color, "FFFFFF") Then
      right = x + 1
      x = x0  ' break
    EndIf
  EndFor
  px = right - left
EndSub
Copyright (c) Microsoft Corporation. All rights reserved.