Page 1 of 1

Vertically center string gadget

Posted: Thu Feb 22, 2024 8:18 am
by coco2
Can you vertically center the text in a string gadget?

Re: Vertically center string gadget

Posted: Thu Feb 22, 2024 8:40 am
by Bisonte
here in this post from Shardik are some links : https://www.purebasic.fr/english/viewto ... 51#p537851

Re: Vertically center string gadget

Posted: Thu Feb 22, 2024 11:36 am
by coco2
Thanks I got it working. Here it is for future people:

Code: Select all

Procedure SetGadgetTextVCenter(gadNum.i)
  Protected.i hdc = GetDC_(GadgetID(gadNum))
  Protected.RECT eRect
  Protected.SIZE textXY
  Protected.s GadgetText = GetGadgetText(gadNum)
  GetTextExtentPoint32_(hdc, GadgetText, Len(GadgetText), @textXY)
  ReleaseDC_(GadgetID(gadNum), hdc)
  GetClientRect_(GadgetID(gadNum), eRect)
  eRect\top = (eRect\Bottom - textXY\cy) / 2
  SendMessage_(GadgetID(gadNum), #EM_SETRECT, 0, eRect)
EndProcedure
#CENTERED_HORIZ = 1
Define.i Event
LoadFont(0, "Arial", 11)
OpenWindow(0, 0, 0, 500, 400, "Vertical Center", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
StringGadget(0, 10, 10, 80, 30, "24", #CENTERED_HORIZ | #PB_String_Numeric | #ES_MULTILINE)
SetGadgetFont(0, FontID(0))
SetGadgetTextVCenter(0)
Repeat
  Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
End

Re: Vertically center string gadget

Posted: Thu Feb 22, 2024 12:01 pm
by Sergey
On Windows you may use #SS_CENTERIMAGE

Code: Select all

If OpenWindow(0, 0, 0, 270, 110, "TextGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
	TextGadget(0, 10, 10, 250, 40, "TextGadget Center + Border", #PB_Text_Center | #PB_Text_Border)
	TextGadget(1, 10, 60, 250, 40, "TextGadget Center + Border", #PB_Text_Center | #PB_Text_Border | #SS_CENTERIMAGE)
	Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf

Re: Vertically center string gadget

Posted: Thu Feb 22, 2024 12:14 pm
by coco2
Does that work for string gadget? Looks like it is only for text gadget

Re: Vertically center string gadget

Posted: Thu Feb 22, 2024 1:46 pm
by jacdelad
Yeah, #SS_... is for Static Controls only. (=TextGadget)