Page 1 of 3
Posted: Wed Dec 21, 2005 2:04 am
by Sparkie
How about something like this...
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
Posted: Wed Dec 21, 2005 2:07 am
by rsts
wow Sparkie, some nice code.
cheers
Posted: Wed Dec 21, 2005 2:38 am
by srod
Bloody hell, I'd never have thought of doing that! So simple.
I guess you'd have to call it every time new lines were added or deleted etc.
Posted: Wed Dec 21, 2005 2:43 am
by Sparkie
Thanks guys.
@srod: Yes, as it stands it only accepts 3 lines of text. If I get time this weekend, I'll see if I can improve on it. Or better yet, maybe I'll just let you handle the rest of it.

Posted: Wed Dec 21, 2005 3:14 am
by srod
Now that sound like a challenge to me!
Here ya go...
***EDIT: It now scrolls correctly and accounts for any scrollbars! -Twit that I am!
Code: Select all
; ************************************************
; Code: Verticaly and Horizontaly centered text
; in a multiline StringGadget
; Author: Sparkie + srod! (But mostly 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)
eRect.RECT
;--> Set rect coordinates for StringGadget
;The following line is used to set the left and right boundaries and takes any scrollbars
;into account.
getclientrect_(GadgetID(gadNum), eRect)
eRect\top = (GadgetHeight(gadNum) - textXY\cy*lineCount) / 2
eRect\bottom = eRect\top + textXY\cy*lineCount
;Check if the rectangle is too high.
if eRect\bottom<gadgetheight(gadNum)
SendMessage_(GadgetID(gadNum), #EM_SETRECT, 0, eRect)
endif
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|#WS_VSCROLL|#ES_AUTOVSCROLL)
StringGadgetVCenter(0)
numlines = sendmessage_(gadgetid(0), #EM_GETLINECOUNT,0,0)
Repeat
eventid=waitwindowevent()
if eventtype() = #PB_EventType_Change and eventgadgetid() = 0 and numlines<>sendmessage_(gadgetid(0), #EM_GETLINECOUNT,0,0)
numlines = sendmessage_(gadgetid(0), #EM_GETLINECOUNT,0,0)
StringGadgetVCenter(0)
endif
Until eventid = #PB_Event_CloseWindow
EndIf
End
Posted: Tue Dec 27, 2005 8:22 pm
by oryaaaaa
Sparkie Thanks!
srod Thanks!
Very Nice code! PureBasic lovers increases!

Posted: Wed Dec 28, 2005 9:58 am
by akira takama
The all of you that thank you very much.
I am a japanese PB user.
This next, please help it.
sorry for my bad english.
Posted: Mon Jul 02, 2007 6:45 pm
by Fluid Byte
* BUMP *
Just needed this and took me a while to find it as it seems like a rather rare request. While I tested this I noticed some flaws. First off, the code doesn't take the currently selected font into account wich makes the lines get splitted into 2 or more blocks. Second, if you made this fix you also need to add the 2 x 2 pixel border when calculating 'eRect\bottom' or it causes the same problem.
Here's my fixed version for future searches:
Code: Select all
Procedure StringGadgetVCenter(GadgetID)
hwndEdit = GadgetID(GadgetID)
LineCount = SendMessage_(hwndEdit,#EM_GETLINECOUNT, 0, 0)
hdc = GetDC_(hwndEdit)
SelectObject_(hdc,GetGadgetFont(0))
GetTextExtentPoint32_(hdc,"ABC",3,fsz.SIZE)
ReleaseDC_(hwndEdit,hdc)
GetClientRect_(hwndEdit,eRect.RECT)
eRect\top = (GadgetHeight(GadgetID) - fsz\cy * LineCount) / 2
eRect\bottom = eRect\top + (fsz\cy * LineCount) + 4
If eRect\bottom < GadgetHeight(GadgetID)
SendMessage_(hwndEdit,#EM_SETRECT,0,eRect)
EndIf
EndProcedure
Text$ = "Vertical and Horizontal" + #CRLF$ + "Centered Text in" + #CRLF$ + "Multiline StringGadget"
OpenWindow(0,0,0,300,200,"Edit Control VCenter",#WS_SYSMENU | #WS_CAPTION | 1)
CreateGadgetList(WindowID(0))
StringGadget(0,10,10,280,180,Text$,#ES_MULTILINE | #ES_CENTER )
SetGadgetFont(0,LoadFont(0,"Arial",12))
StringGadgetVCenter(0)
LinesNum = SendMessage_(GadgetID(0), #EM_GETLINECOUNT,0,0)
Repeat
EventID = WaitWindowEvent()
Select EventGadget()
Case 0
If EventType() = #PB_EventType_Change And LinesNum <> SendMessage_(GadgetID(0),#EM_GETLINECOUNT,0,0)
LinesNum = SendMessage_(GadgetID(0),#EM_GETLINECOUNT,0,0)
StringGadgetVCenter(0)
EndIf
EndSelect
Until EventID = #PB_Event_CloseWindow
Re: StringGadget Vertical?
Posted: Thu Jan 07, 2010 10:15 am
by Kwai chang caine
Hello at all
Thanks to all for all this precious codes

The code of FLUID BYTE is exactely what i want, because it see the size of the font
But he have again a little problem
I can't add a line, in the string gadget, when i do RETURN

And i also can't add line, when the numbers of characters, is upper at the size of the line
The line, can just to be add, with the code not with the keyboard :roll:
Code: Select all
Procedure StringGadgetVCenter(GadgetID)
hwndEdit = GadgetID(GadgetID)
LineCount = SendMessage_(hwndEdit,#EM_GETLINECOUNT, 0, 0)
hdc = GetDC_(hwndEdit)
SelectObject_(hdc,GetGadgetFont(0))
GetTextExtentPoint32_(hdc,"ABC",3,fsz.SIZE)
ReleaseDC_(hwndEdit,hdc)
GetClientRect_(hwndEdit,eRect.RECT)
eRect\top = (GadgetHeight(GadgetID) - fsz\cy * LineCount) / 2
eRect\bottom = eRect\top + (fsz\cy * LineCount) + 4
If eRect\bottom < GadgetHeight(GadgetID)
SendMessage_(hwndEdit,#EM_SETRECT,0,eRect)
EndIf
EndProcedure
Text$ = "Vertical and Horizontal" + #CRLF$ + "Centered Text in" + #CRLF$ + "Multiline StringGadget"
OpenWindow(0,0,0,300,200,"Edit Control VCenter",#WS_SYSMENU|#WS_CAPTION | 1)
StringGadget(0,10,10,280,180,Text$,#ES_MULTILINE|#ES_CENTER )
SetGadgetFont(0,LoadFont(0,"Arial",12))
StringGadgetVCenter(0)
LinesNum = SendMessage_(GadgetID(0), #EM_GETLINECOUNT,0,0)
Repeat
EventID = WaitWindowEvent()
Select EventGadget()
Case 0
If EventType() = #PB_EventType_Change And LinesNum <> SendMessage_(GadgetID(0),#EM_GETLINECOUNT,0,0)
LinesNum = SendMessage_(GadgetID(0),#EM_GETLINECOUNT,0,0)
StringGadgetVCenter(0)
EndIf
EndSelect
Until EventID = #PB_Event_CloseWindow
Re: StringGadget Vertical?
Posted: Thu Jan 07, 2010 3:13 pm
by RASHAD
KCC
change
Code: Select all
StringGadget(0,10,10,280,180,Text$,#ES_MULTILINE|#ES_CENTER )
to
Code: Select all
StringGadget(0,10,10,280,180,Text$,#ES_MULTILINE | #ES_CENTER|#WS_VSCROLL|#ES_AUTOVSCROLL)
Re: StringGadget Vertical?
Posted: Thu Jan 07, 2010 3:23 pm
by Kwai chang caine
Thanks RASHAD
It's exactely what i want

You are too strong
I wish you a very very good day
Re: StringGadget Vertical?
Posted: Thu Oct 25, 2012 5:17 pm
by Puffolino
I haven't found a possibility to do a static text box which looks like a StringGadget (same border color etc.) and is vertically centered.
I think, I have tried all codes in the forum - and most of them are doing a nice job, but are not perfect. Using a TextGadget with the flag #WS_Border creates a different border than StringGadget. And using StringGadgets like with the code below, draws the text some pixels to low (at least here).
Code: Select all
Procedure StringGadgetVCenter(GadgetID)
hwndEdit=GadgetID(GadgetID)
hdc=GetDC_(hwndEdit)
SelectObject_(hdc,GetGadgetFont(0))
GetTextExtentPoint32_(hdc,"ABC",3,fsz.SIZE)
ReleaseDC_(hwndEdit,hdc)
GetClientRect_(hwndEdit,eRect.RECT)
eRect\top=(GadgetHeight(GadgetID)-fsz\cy)>>1
eRect\bottom = eRect\top + (fsz\cy) + 4
If eRect\bottom < GadgetHeight(GadgetID)
SendMessage_(hwndEdit,#EM_SETRECT,0,eRect)
EndIf
EndProcedure
Text$ = "Test Vertical and Horizontal Centered Text in Multiline StringGadget for MyTrial"
OpenWindow(0,0,0,300,200,"Edit Control VCenter",#WS_SYSMENU|#WS_CAPTION)
StringGadget(0,10,10,280,24,Text$,#ES_MULTILINE|#PB_String_ReadOnly)
SetGadgetColor(0,#PB_Gadget_FrontColor,$0103FE)
SetGadgetColor(0,#PB_Gadget_BackColor,$D0FFFF)
StringGadgetVCenter(0)
Repeat
EventID = WaitWindowEvent()
Select EventGadget()
EndSelect
Until EventID = #PB_Event_CloseWindow
Re: StringGadget Vertical?
Posted: Mon Jan 25, 2016 6:51 pm
by Michael Vogel
Based on the codes above I am trying to get the single line text of a string gadget vertical centered - which seems to work fine for left aligned text (Gadget 0) but not for right aligned text (Gadget 1).
Is it possible to get the right aligned version of the string gadget also done to allow only a single line text? (enter some text or reduce windows width to see the problem)
Another point to be careful - after resizing a gadget it is needed to be centered again (change the window width to see this problem)
Code: Select all
Procedure StringGadgetVCenter(gadget)
Protected hwndEdit
Protected hdc
Protected fsz.SIZE
Protected erect.RECT
Protected height
height=GadgetHeight(gadget)
hwndEdit=GadgetID(gadget)
hdc=GetDC_(hwndEdit)
SelectObject_(hdc,GetGadgetFont(Gadget))
GetTextExtentPoint32_(hdc,"ABC",3,fsz)
ReleaseDC_(hwndEdit,hdc)
GetClientRect_(hwndEdit,eRect)
eRect\left=2;
eRect\top=height>>1-fsz\cy<<4/20
eRect\bottom=height+fsz\cy>>1
SendMessage_(hwndEdit,#EM_SETRECT,0,eRect)
EndProcedure
Text$ = "Test for vertical centered text - horizontal (above) or right aligned (below)..."
OpenWindow(0,0,0,300,200,"Edit Control VCenter",#PB_Window_SizeGadget)
StringGadget(0,10,10,280,40,Text$,#ES_MULTILINE)
StringGadgetVCenter(0)
StringGadget(1,10,60,280,40,StringField(Text$,1,"-"),#ES_MULTILINE|#ES_RIGHT)
StringGadgetVCenter(1)
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
End
Case #PB_Event_SizeWindow
ResizeGadget(0,#PB_Ignore,#PB_Ignore,WindowWidth(0)-20,#PB_Ignore)
;StringGadgetVCenter(0)
ResizeGadget(1,#PB_Ignore,#PB_Ignore,WindowWidth(0)-20,#PB_Ignore)
;StringGadgetVCenter(1)
EndSelect
ForEver
Re: StringGadget Vertical?
Posted: Mon Jan 25, 2016 7:39 pm
by RASHAD
Hi
Code: Select all
Procedure StringGadgetVCenter(gadget)
Protected hwndEdit
Protected hdc
Protected fsz.SIZE
Protected erect.RECT
Protected height
height=GadgetHeight(gadget)
hwndEdit=GadgetID(gadget)
hdc=GetDC_(hwndEdit)
SelectObject_(hdc,GetGadgetFont(Gadget))
GetTextExtentPoint32_(hdc,"ABC",3,fsz)
ReleaseDC_(hwndEdit,hdc)
GetClientRect_(hwndEdit,eRect)
eRect\left=2;
eRect\top=height>>1-fsz\cy<<4/20
eRect\bottom=height+fsz\cy>>1
If gadget = 1
SetWindowLongPtr_(GadgetID(1),#GWL_STYLE,GetWindowLongPtr_(GadgetID(1),#GWL_STYLE)|#ES_LEFT)
EndIf
SendMessage_(hwndEdit,#EM_SETRECT,0,eRect)
EndProcedure
Procedure sizecb()
StringGadgetVCenter(0)
StringGadgetVCenter(1)
EndProcedure
Text$ = "Test for vertical centered text - horizontal (above) or right aligned (below)..."
OpenWindow(0,0,0,300,200,"Edit Control VCenter",#PB_Window_SizeGadget)
StringGadget(0,10,10,280,40,Text$,#ES_MULTILINE)
StringGadgetVCenter(0)
StringGadget(1,10,60,280,40,StringField(Text$,1,"-"),#ES_MULTILINE|#ES_RIGHT)
StringGadgetVCenter(1)
BindEvent(#PB_Event_Repaint,@sizecb())
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
End
Case #PB_Event_SizeWindow
ResizeGadget(0,#PB_Ignore,#PB_Ignore,WindowWidth(0)-20,#PB_Ignore)
ResizeGadget(1,#PB_Ignore,#PB_Ignore,WindowWidth(0)-20,#PB_Ignore)
EndSelect
ForEver
Re: StringGadget Vertical?
Posted: Mon Jan 25, 2016 8:45 pm
by Michael Vogel
RASHAD wrote:Hi [..]
Hey!
Rashad, is it possible to keep the text in a single line? Reduce the window width and you will see the text will be displayed in two (or more) lines, also in your code (btw. the vertical alignment does work also in my code above, when the commented lines are active).