It is currently Sat Aug 24, 2019 1:13 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: 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  
 Post subject: Re: StringGadget Vertical?
PostPosted: Tue Jan 26, 2016 5:13 am 
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))
   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

_________________
Egypt my love


Last edited by RASHAD on Tue Jan 26, 2016 10:53 am, edited 2 times in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: StringGadget Vertical?
PostPosted: Tue Jan 26, 2016 9:30 am 
Offline
Addict
Addict
User avatar

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


Top
 Profile  
Reply with quote  
 Post subject: Re: StringGadget Vertical?
PostPosted: Tue Jan 26, 2016 10:08 am 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Apr 12, 2009 6:27 am
Posts: 3381
Previous post updated

_________________
Egypt my love


Top
 Profile  
Reply with quote  
 Post subject: Re: StringGadget Vertical?
PostPosted: Tue Jan 26, 2016 10:33 am 
Offline
Addict
Addict
User avatar

Joined: Thu Feb 09, 2006 11:27 pm
Posts: 2431
RASHAD wrote:
Previous post updated

Not bad :lol:

But still an issue here when entering text, couldn't fix it for now...

Code:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: StringGadget Vertical?
PostPosted: Tue Jan 26, 2016 10:55 am 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Apr 12, 2009 6:27 am
Posts: 3381
Previous post updated again
Center text dynamically vertical

_________________
Egypt my love


Top
 Profile  
Reply with quote  
 Post subject: Re: StringGadget Vertical?
PostPosted: Tue Jan 26, 2016 11:32 am 
Offline
Addict
Addict
User avatar

Joined: Thu Feb 09, 2006 11:27 pm
Posts: 2431
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:
   limit = fsz\cx
   :
   If Text$=""
      Text$="|"
      GetTextExtentPoint32_(hdc,Text$,Len(text$),fsz)
   EndIf
   :
   ReleaseDC_(hwndEdit,hdc)


Top
 Profile  
Reply with quote  
 Post subject: Re: StringGadget Vertical?
PostPosted: Tue Jan 26, 2016 5:21 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Feb 09, 2006 11:27 pm
Posts: 2431
Nope, still not so fine, when entering text like "j j j jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj" into the text field, I see two lines again...


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: VB6_to_PBx and 13 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