Code: Select all
; ************************************************
; Code: Verticaly and Horizontaly centered text
; in a multiline StringGadget
; Author: Sparkie
; Date: December 20, 2005
; OS: Windows only
; ************************************************
; ************************************************
; Procedure: Center text in Multiline StringGadget
; ************************************************
Procedure StringGadgetVCenter(gadNum)
;--> Get line count of StringGadget
lineCount = SendMessage_(GadgetID(gadNum), #EM_GETLINECOUNT, 0, 0)
myText$ = GetGadgetText(gadNum)
;--> Get width and height of text on one line
hdc = GetDC_(GadgetID(gadNum))
GetTextExtentPoint32_(hdc, myText$, Len(myText$), @textXY.SIZE)
ReleaseDC_(GadgetID(gadNum), hdc)
;--> Set rect coordinates for StringGadget
eRect.RECT
eRect\left = 0
eRect\top = (GadgetHeight(gadNum) - textXY\cy*lineCount) / 2
eRect\right = GadgetWidth(gadNum) - (eRect\left * 2)
eRect\bottom = eRect\top + textXY\cy*lineCount
SendMessage_(GadgetID(gadNum), #EM_SETRECT, 0, eRect)
EndProcedure
; ************************************************
; Main Window
; ************************************************
If OpenWindow(0, 0, 0, 300, 200, #PB_Window_SystemMenu | #PB_Window_ScreenCentered, "StringGadget Centered Text") And CreateGadgetList(WindowID(0))
;--> #ES_CENTER will not work with a singleline StringGadget on Win95/NT4
StringGadget(0, 50, 10, 200, 80, "Vertical and Horizontal" + #CRLF$ + "Centered Text in" + #CRLF$ + "Multiline StringGadget", #PB_String_MultiLine | #ES_CENTER)
StringGadgetVCenter(0)
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
End