It is currently Sat Aug 24, 2019 5:32 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 41 posts ]  Go to page 1, 2, 3  Next
Author Message
 Post subject:
PostPosted: Wed Dec 21, 2005 2:04 am 
Offline
PureBatMan Forever
PureBatMan Forever
User avatar

Joined: Tue Feb 10, 2004 3:07 am
Posts: 2307
Location: Ohio, USA
How about something like this...
Code:
; ************************************************
; 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


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Dec 21, 2005 2:07 am 
Offline
Addict
Addict

Joined: Wed Aug 24, 2005 8:39 am
Posts: 2736
Location: Southwest OH - USA
wow Sparkie, some nice code.

cheers


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Dec 21, 2005 2:38 am 
Offline
PureBasic Expert
PureBasic Expert

Joined: Wed Oct 29, 2003 4:35 pm
Posts: 10524
Location: Beyond the pale...
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.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Dec 21, 2005 2:43 am 
Offline
PureBatMan Forever
PureBatMan Forever
User avatar

Joined: Tue Feb 10, 2004 3:07 am
Posts: 2307
Location: Ohio, USA
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


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Dec 21, 2005 3:14 am 
Offline
PureBasic Expert
PureBasic Expert

Joined: Wed Oct 29, 2003 4:35 pm
Posts: 10524
Location: Beyond the pale...
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:
; ************************************************
; 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.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue Dec 27, 2005 8:22 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Mon Jan 12, 2004 11:40 pm
Posts: 760
Location: Okazaki, JAPAN
Sparkie Thanks!
srod Thanks!

Very Nice code! PureBasic lovers increases! :D

_________________
My general site & Bug head technology - Ultimate high quality sound player by BASS
My facebook page.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Dec 28, 2005 9:58 am 
Offline
New User
New User

Joined: Wed Dec 28, 2005 8:44 am
Posts: 1
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.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Mon Jul 02, 2007 6:45 pm 
Offline
Addict
Addict
User avatar

Joined: Fri Jul 21, 2006 4:41 am
Posts: 2336
Location: Berlin, Germany
* 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:
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?


Top
 Profile  
Reply with quote  
 Post subject: Re: StringGadget Vertical?
PostPosted: Thu Jan 07, 2010 10:15 am 
Offline
Addict
Addict
User avatar

Joined: Sun Nov 05, 2006 11:42 pm
Posts: 4489
Location: Lyon - France
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:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: StringGadget Vertical?
PostPosted: Thu Jan 07, 2010 3:13 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Apr 12, 2009 6:27 am
Posts: 3381
KCC

change

Code:
StringGadget(0,10,10,280,180,Text$,#ES_MULTILINE|#ES_CENTER )


to

Code:
StringGadget(0,10,10,280,180,Text$,#ES_MULTILINE | #ES_CENTER|#WS_VSCROLL|#ES_AUTOVSCROLL)

_________________
Egypt my love


Top
 Profile  
Reply with quote  
 Post subject: Re: StringGadget Vertical?
PostPosted: Thu Jan 07, 2010 3:23 pm 
Offline
Addict
Addict
User avatar

Joined: Sun Nov 05, 2006 11:42 pm
Posts: 4489
Location: Lyon - France
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


Top
 Profile  
Reply with quote  
 Post subject: Re: StringGadget Vertical?
PostPosted: Thu Oct 25, 2012 5:17 pm 
Offline
User
User

Joined: Thu Jan 05, 2012 12:27 am
Posts: 49
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:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: StringGadget Vertical?
PostPosted: Mon Jan 25, 2016 6:51 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Feb 09, 2006 11:27 pm
Posts: 2431
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:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: StringGadget Vertical?
PostPosted: Mon Jan 25, 2016 7:39 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Apr 12, 2009 6:27 am
Posts: 3381
Hi
Code:


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


Top
 Profile  
Reply with quote  
 Post subject: Re: StringGadget Vertical?
PostPosted: Mon Jan 25, 2016 8:45 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Feb 09, 2006 11:27 pm
Posts: 2431
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).


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 41 posts ]  Go to page 1, 2, 3  Next

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 14 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye