Page 1 of 3
					
				
				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).
 
			 
			
					
				Re: StringGadget Vertical?
				Posted: Tue Jan 26, 2016 5:13 am
				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))
   Text$ = GetGadgetText(gadget)
   GetTextExtentPoint32_(hdc,Text$,Len(text$),fsz)
   limit = fsz\cx
   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)
   If gadget = 1 And eRect\right <= limit
      eRect\right = limit + 2
      SendMessage_(GadgetID(1),#EM_SETRECT,0,eRect)
   EndIf
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|#PB_Window_ScreenCentered)
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)
   Case #PB_Event_Gadget
      gadget=EventGadget()
      Select gadget
      Case 0,1
         Select EventType()
         Case #PB_EventType_Change
            Debug "Do "+Str(gadget)
            StringGadgetVCenter(gadget)
         EndSelect
      EndSelect
   EndSelect
ForEver
 
			 
			
					
				Re: StringGadget Vertical?
				Posted: Tue Jan 26, 2016 9:30 am
				by Michael Vogel
				RASHAD wrote:Hi
[...]
Ha!
Nice idea, how did you have calculated the text width - this has to be done because the content may change and I don't want to do a Start/StopDrawing section to get the TextWidth...
 
			 
			
					
				Re: StringGadget Vertical?
				Posted: Tue Jan 26, 2016 10:08 am
				by RASHAD
				Previous post updated
			 
			
					
				Re: StringGadget Vertical?
				Posted: Tue Jan 26, 2016 10:33 am
				by Michael Vogel
				RASHAD wrote:Previous post updated
Not bad  
 
But still an issue here when entering text, couldn't fix it for now...
Code: Select all
Global Text$
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,StringField(Text$,1,"-"),Len(StringField(Text$,1,"-")),fsz)
	limit = fsz\cx
	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)
	If gadget = 1 And eRect\right <= limit
		eRect\right = limit + 2
		SendMessage_(GadgetID(1),#EM_SETRECT,0,eRect)
	EndIf
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|#PB_Window_ScreenCentered)
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)
	Case #PB_Event_Gadget
		gadget=EventGadget()
		Select gadget
		Case 0,1
			Select EventType()
			Case #PB_EventType_Change
				Debug "Do "+Str(gadget)
				StringGadgetVCenter(gadget)
			EndSelect
		EndSelect
	EndSelect
ForEver
 
			 
			
					
				Re: StringGadget Vertical?
				Posted: Tue Jan 26, 2016 10:55 am
				by RASHAD
				Previous post updated again
Center text dynamically vertical
			 
			
					
				Re: StringGadget Vertical?
				Posted: Tue Jan 26, 2016 11:32 am
				by Michael Vogel
				RASHAD wrote:Previous post updated again
Center text dynamically vertical
Nearly perfect - thanks, Rashad!
When entering text, some flickering can be seen from time to time, but that doesn't hurt to much.
But there's still one point, when the complete text is eliminated, the cursor jumps to the bottom...
...a quick and dirty workaround for this issue:
Code: Select all
	limit = fsz\cx
	:
	If Text$=""
		Text$="|"
		GetTextExtentPoint32_(hdc,Text$,Len(text$),fsz)
	EndIf
	:
	ReleaseDC_(hwndEdit,hdc)
 
			 
			
					
				Re: StringGadget Vertical?
				Posted: Tue Jan 26, 2016 5:21 pm
				by Michael Vogel
				Nope, still not so fine, when entering text like "j j j jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj" into the text field, I see two lines again...