String Gadget Vertical Centre Simple Hack?

Windows specific forum
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

String Gadget Vertical Centre Simple Hack?

Post 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?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4664
Joined: Sun Apr 12, 2009 6:27 am

Re: String Gadget Vertical Centre Simple Hack?

Post 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
Egypt my love
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: String Gadget Vertical Centre Simple Hack?

Post 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.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: String Gadget Vertical Centre Simple Hack?

Post 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
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Post Reply