Page 1 of 1

String Gadget Vertical Centre Simple Hack?

Posted: Fri Feb 28, 2020 12:54 am
by IdeasVacuum
If we want the text in a String Gadget to be held vertically central, we can get exactly that using #ES_MULTILINE

Unfortunately though, combined with #PB_String_BorderLess, the effect is lost. If it's only one or two String Gadgets a cheap workaround is to set them individually on containers (so you have a white String Gadget on a white Container and can centre the text that way), but maybe someone has a more elegant solution?

Re: String Gadget Vertical Centre Simple Hack?

Posted: Fri Feb 28, 2020 6:21 am
by RASHAD
Hi IV
StringGadget() centered Val & Hal

Code: Select all

Procedure StringWH(gadget, font , text$)
  SetGadgetFont(gadget,FontID(font))
  hdc = GetDC_(GadgetID(gadget))
  SelectObject_(hdc, FontID(font))
  GetTextMetrics_(hdc, @tm.TEXTMETRIC)
  h = tm\tmHeight + tm\tmInternalLeading
  GetTextExtentPoint32_(hdc , @text$ ,Len(text$),@s.SIZE)
  lrm = 40  ;Left & Right Margin
  SendMessage_(GadgetID(gadget), #EM_SETMARGINS,#EC_LEFTMARGIN ,lrm | 0 << 16)
  w = s\cx+4+lrm*2
  ReleaseDC_(GadgetID(gadget), hdc)
  ResizeGadget(gadget,GadgetX(gadget, #PB_Gadget_WindowCoordinate), GadgetY(gadget,#PB_Gadget_WindowCoordinate),w,h)
EndProcedure

LoadFont(0,"Arial",32)
text$ = "ABCDJQabcjqQJDCBA" 
OpenWindow(0, 0, 0, 600, 400, "StringGadget ", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
SmartWindowRefresh(0,1)

StringGadget(1, 10,10,580,40,Text$)
  SetGadgetColor(1 , #PB_Gadget_BackColor , $C2FEFC)
  SetGadgetColor(1,  #PB_Gadget_FrontColor , $0000FF )
StringWH(1,0 ,text$)

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Quit = 1
      
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 1
          text$ = GetGadgetText(1)
          StringWH(1,0 ,text$)
      EndSelect
  EndSelect
Until Quit = 1

Re: String Gadget Vertical Centre Simple Hack?

Posted: Sun Mar 01, 2020 12:58 pm
by IdeasVacuum
That's effective Rashad, thanks for sharing!

I wonder why the developers of the API made such a mess of little things like that.

Re: String Gadget Vertical Centre Simple Hack?

Posted: Sun Mar 01, 2020 1:06 pm
by IdeasVacuum
Just stumbled upon another, similar way:

Code: Select all

Procedure PfStringGdtVertCtr(iID.i)
;#---------------------------------
;Ensure the Gadget font is set before calling this Procedure. Also #ES_MULTILINE must be set.
Protected  iHwndEdit.i = GadgetID(iID)
Protected iLineCount.i = SendMessage_(iHwndEdit,#EM_GETLINECOUNT, 0, 0)
Protected       iHdc.i = GetDC_(iHwndEdit)
Protected      eRect.RECT
Protected        fsz.SIZE

                       SelectObject_(iHdc, GetGadgetFont(iID))
               GetTextExtentPoint32_(iHdc, "ABC", 3, fsz)
                          ReleaseDC_(iHwndEdit, iHdc)
                      GetClientRect_(iHwndEdit, eRect)

                     eRect\top = (GadgetHeight(iID) - fsz\cy * iLineCount) / 2
                  eRect\bottom = eRect\top + (fsz\cy * iLineCount) + 4
               If eRect\bottom < GadgetHeight(iID)

                         SendMessage_(iHwndEdit, #EM_SETRECT, 0, eRect)
               EndIf
EndProcedure