; StringWidth Example ; ------------------- Graphics 640,480,0,2 SetBuffer BackBuffer() ; A chunky font for the message, a small one for the info overlay big_font=LoadFont("Arial",32,True) info_font=LoadFont("Arial",14) msg$="INSERT COIN" While Not KeyDown(1) ; Type to edit the message, Backspace deletes key=GetKey() If key>=32 And key<=126 And Len(msg$)<24 Then msg$=msg$+Chr$(key) If key=8 And Len(msg$)>0 Then msg$=Left$(msg$,Len(msg$)-1) Cls SetFont big_font ; StringWidth returns the string's pixel width in the current font - ; the classic use is centring text exactly on the screen w=StringWidth(msg$) h=StringHeight(msg$) x=(640-w)/2 Color 255,255,255 Text x,220,msg$ ; A box drawn exactly around the string using the measured size Color 80,200,255 Rect x-4,216,w+8,h+8,False SetFont info_font Color 255,255,255 Text 0,0,"Type to edit the message Backspace: delete Esc: exit" Text 0,20,"StringWidth(msg$) = "+w+" pixels, so x = (640-"+w+")/2 = "+x Flip Wend End