VBAndCs / sVB-Small-Visual-Basic

Small Visual Basic (sVB) is an educational programming language, created by Eng. Mohammad Hamdy as an evolved version of Microsoft Small Basic (SB). It is meant to be easier and more powerful at the same time, to introduce programming basics to kids and beginners of any age, provided that they can use the English keyboard on the Windows OS.
Other
232 stars 16 forks source link

The length and height of a text in graphics mode ... #25

Closed boa2145 closed 1 year ago

boa2145 commented 1 year ago

Hi Mohammad,

I sometimes need the length and height of a text in "Graphics mode" when using "GraphicsWindow" with "DrawText()" or "DrawBoundText()". How can I calculate the length and height of a text depending on the font name and font size?

Regards ... Gregor

VBAndCs commented 1 year ago

@boa2145 You can't. But you can quite the DrawText method, and start using the Controls.AddLabel method to draw the text in a lable and have full control of it. You can change the font properties of the label, then call the FitContentSize to force the label to resize its width and height to fit its text, so you can use its Width and Height properties to get the text size. Note that sVB allow you to draw all controls (except menus) on the Graphics Window. Try this:

Lbl = Controls.AddLabel("Hello sVB", 100, 100)
Lbl.FontSize = 20
Lbl.FitContentSize()
GraphicsWindow.ShowMessage(
   Text.Format(
      "Width = [1], Height = [2]",
      {Lbl.Width, Lbl.Height}
   ),
   "Text size"
)

image

VBAndCs commented 1 year ago

Note also that sVB controls has more animation methods than the SB shapes, so, you can do a lot of amazing things with the text, like:

Lbl.AnimateAngle(50, 1000)