Loading ...
Sorry, an error occurred while loading the content.

RE: [libertybasic] Text trouble in a graphicsbox

Expand Messages
  • Stefan Pendl
    ... Below find a sample to do this. ---code start (watch for line wraps) demo to get the strings width and height to center a text inside a graphicsbox
    Message 1 of 3 , Aug 13, 2007
    • 0 Attachment
      >
      > I'm trying to center a string in a graphicsbox. I want the
      > user to select the stri g and the font. I know the
      > stringwidth? command but what can I do about the height?
      >

      Below find a sample to do this.

      '---code start (watch for line wraps)
      ' demo to get the strings width and height
      ' to center a text inside a graphicsbox
      nomainwin

      graphicbox #main.gb, 10, 10, 75, 35
      statictext #main.txt, "Click box to enter text", 10, 55, 200, 25

      open "grahicbox test" for window as #main
      #main "trapclose [quit]"
      #main.gb "when leftButtonDown enterText"
      #main.gb "font times_new_roman 10"
      wait

      [quit]
      close #main
      end

      sub enterText handle$, xPos, yPos
      text$ = "MMggMM"
      prompt "Enter text:"; text$

      call centerText handle$, text$
      end sub

      sub centerText handle$, text$
      boxHandle = hwnd(#handle$)

      if GetWindowRect(boxHandle, boxWidth, boxHeight, dummy, dummy1) then
      boxDC = GetDC(boxHandle)
      fontMetrics$ = GetTextMetrics$(boxDC)
      call ReleaseDC boxHandle, boxDC
      fontSize = val(word$(fontMetrics$, 1))
      fontAscent = val(word$(fontMetrics$, 2))
      fontDescent = val(word$(fontMetrics$, 3))

      #handle$ "stringwidth? text$ length"
      xStart = int((boxWidth - length)/2)
      yStart = int((boxHeight + fontAscent)/2) - fontDescent

      #handle$ "cls;place "; xStart; " "; yStart
      #handle$ "\"; text$
      #handle$ "flush"
      end if
      end sub

      function GetWindowRect(Handle, byref Width, byref Height, byref PosX, byref
      PosY)
      ' returns TRUE or FALSE
      ' fills the variables if success
      struct Rect,_
      left as long,_
      top as long,_
      right as long,_
      bottom as long

      calldll #user32, "GetWindowRect", _
      Handle as ulong,_
      Rect as struct,_
      GetWindowRect as boolean

      if GetWindowRect then
      Width = Rect.right.struct - Rect.left.struct
      Height = Rect.bottom.struct - Rect.top.struct
      PosX = Rect.left.struct
      PosY = Rect.top.struct
      end if
      end function

      function GetDC(Handle)
      calldll #user32, "GetDC",_
      Handle as ulong,_
      GetDC as ulong
      end function

      sub ReleaseDC Handle, hDC
      CallDll #user32, "ReleaseDC",_
      Handle as ulong,_
      hDC as ulong,_
      result as ulong
      end sub

      function GetTextMetrics$(hDC)
      struct textMetric, _
      tmHeight as long, _
      tmAscent as long, _
      tmDescent as long, _
      tmInternalLeading as long, _
      tmExternalLeading as long, _
      tmAveCharWidth as long, _
      tmMaxCharWidth as long, _
      tmWeight as long, _
      tmOverhang as long, _
      tmDigitizedAspectX as long, _
      tmDigitizedAspectY as long, _
      tmFirstChar$ as char[1], _
      tmLastChar$ as char[1], _
      tmDefaultChar$ as char[1], _
      tmBreakChar$ as char[1], _
      tmItalic$ as char[1], _
      tmUnderlined$ as char[1], _
      tmStruckOut$ as char[1], _
      tmPitchAndFamily$ as char[1], _
      tmCharSet$ as char[1]

      calldll #gdi32, "GetTextMetricsA", _
      hDC as ulong, _
      textMetric as struct, _
      result as boolean

      GetTextMetrics$ = textMetric.tmHeight.struct; " "; _
      textMetric.tmAscent.struct; " "; _
      textMetric.tmDescent.struct; " "; _
      textMetric.tmInternalLeading.struct; " "; _
      textMetric.tmExternalLeading.struct; " "; _
      textMetric.tmAveCharWidth.struct; " "; _
      textMetric.tmMaxCharWidth.struct; " "; _
      textMetric.tmWeight.struct; " "; _
      textMetric.tmOverhang.struct; " "; _
      textMetric.tmDigitizedAspectX.struct; " "; _
      textMetric.tmDigitizedAspectY.struct; " "; _
      asc(textMetric.tmFirstChar$.struct); " "; _
      asc(textMetric.tmLastChar$.struct); " "; _
      asc(textMetric.tmDefaultChar$.struct); " "; _
      asc(textMetric.tmBreakChar$.struct); " "; _
      asc(textMetric.tmItalic$.struct); " "; _
      asc(textMetric.tmUnderlined$.struct); " "; _
      asc(textMetric.tmStruckOut$.struct); " "; _
      asc(textMetric.tmPitchAndFamily$.struct); " "; _
      asc(textMetric.tmCharSet$.struct)
      end function
      '---code end

      ---
      Stefan Pendl
    • jcb_2008
      Thanks I ve been trying to do that fpr a while. I ll take that home and see what I can do.
      Message 2 of 3 , Aug 13, 2007
      • 0 Attachment
        Thanks I've been trying to do that fpr a while. I'll take that home and
        see what I can do.
      Your message has been successfully submitted and would be delivered to recipients shortly.