Align 2D Text to BaseLine [Windows]

Share your advanced PureBasic knowledge/code with the community.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Align 2D Text to BaseLine [Windows]

Post by RASHAD »

Hi

Code: Select all

Procedure BaseLine(hdc,font)
  SelectObject_(hdc, FontID(font))
  GetTextMetrics_(hdc, @tm.TEXTMETRIC)
  bLine = tm\tmDescent
  ProcedureReturn bLine
EndProcedure  

LoadFont(0, "Arial", 12)
LoadFont(1, "Cambria", 32)
LoadFont(2, "Times New Roman", 45)
LoadFont(3, "Verdana", 24)
LoadFont(4, "Courier New", 16)
  
If OpenWindow(0, 0, 0, 800, 600, "DrawText Exemple", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

  iniH = 20
  CreateImage(0, 800, 600)
  hdc = StartDrawing(ImageOutput(0))
  DrawingMode(#PB_2DDrawing_Transparent )
  
  DrawingFont(FontID(0))
  bLine = BaseLine(hdc,0)
  h1 = TextHeight("Hello, ")
  x1 = DrawText(10,iniH+bLine,"Hello, ",RGB(Random(255), Random(255), Random(255)))
  
  DrawingFont(FontID(1))
  bLine = BaseLine(hdc,1)
  h2 = TextHeight("this ")
  x2 = DrawText(x1+2 ,iniH+bLine + h1-h2,"this ",RGB(Random(255), Random(255), Random(255)))
  
  DrawingFont(FontID(2))
  bLine = BaseLine(hdc,2)
  h3 = TextHeight("a ")
  x3 = DrawText(x2+2,iniH+bLine + h1-h3,"a ",RGB(Random(255), Random(255), Random(255)))
  
  DrawingFont(FontID(3))
  bLine = BaseLine(hdc,3)
  h4 = TextHeight("Multi-Font ")
  x4 = DrawText(x3+2,iniH+bLine + h1-h4,"Multi-Font ",RGB(Random(255), Random(255), Random(255)))
  
  DrawingFont(FontID(4))
  bLine = BaseLine(hdc,4)
  h5 = TextHeight("Text !")
  x5 = DrawText(x4+2,iniH+bLine + h1-h5,"Text !",RGB(Random(255), Random(255), Random(255)))  
  
  StopDrawing()
  ImageGadget(0, 0, 0, 200, 200, ImageID(0))
  For f = 0 To 4
    FreeFont(f)
  Next  
    
  Repeat
    Event = WaitWindowEvent()
  Until Event = #PB_Event_CloseWindow
EndIf

Egypt my love
ShadowStorm
Enthusiast
Enthusiast
Posts: 237
Joined: Tue Feb 14, 2017 12:07 pm

Re: Align 2D Text to BaseLine [Windows]

Post by ShadowStorm »

Thank you very much :D
I am French, I do not speak English.
My apologies for the mistakes.

I have sometimes problems of expression
I am sometimes quite clumsy, please excuse me and let me know.
Post Reply