StringGadget Vertical?

Just starting out? Need help? Post your questions and find answers here.
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post 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
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Post by rsts »

wow Sparkie, some nice code.

cheers
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post 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.
I may look like a mule, but I'm not a complete ass.
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post 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. :grin:
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post 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 
I may look like a mule, but I'm not a complete ass.
User avatar
oryaaaaa
Enthusiast
Enthusiast
Posts: 791
Joined: Mon Jan 12, 2004 11:40 pm
Location: Okazaki, JAPAN

Post by oryaaaaa »

Sparkie Thanks!
srod Thanks!

Very Nice code! PureBasic lovers increases! :D
akira takama
New User
New User
Posts: 1
Joined: Wed Dec 28, 2005 8:44 am

Post 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.
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post 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
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5353
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: StringGadget Vertical?

Post by Kwai chang caine »

Hello at all

Thanks to all for all this precious codes 8)
The code of FLUID BYTE is exactely what i want, because it see the size of the font :wink:

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

ImageThe happiness is a road...
Not a destination
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4663
Joined: Sun Apr 12, 2009 6:27 am

Re: StringGadget Vertical?

Post 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)
Egypt my love
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5353
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: StringGadget Vertical?

Post by Kwai chang caine »

Thanks RASHAD 8)

It's exactely what i want :wink:
You are too strong

I wish you a very very good day
ImageThe happiness is a road...
Not a destination
Puffolino
User
User
Posts: 49
Joined: Thu Jan 05, 2012 12:27 am

Re: StringGadget Vertical?

Post 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
User avatar
Michael Vogel
Addict
Addict
Posts: 2677
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: StringGadget Vertical?

Post 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
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4663
Joined: Sun Apr 12, 2009 6:27 am

Re: StringGadget Vertical?

Post 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
Egypt my love
User avatar
Michael Vogel
Addict
Addict
Posts: 2677
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: StringGadget Vertical?

Post 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).
Post Reply