It is currently Sat Nov 25, 2017 2:57 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: StringGadget Text Vertically Centred
PostPosted: Thu Sep 07, 2017 12:04 am 
Offline
Always Here
Always Here

Joined: Fri Oct 23, 2009 2:33 am
Posts: 5688
Location: Wales, UK
It would be good to have Vertically Centred text, either via a flag option or as the default. Currently, text is set top-left in the String Gadget and it's very ugly! :mrgreen:

Windows work-around:
Code:
Enumeration
#Win
#Pnl
#Tab0
#Cont
#Str1
#Str2
#Font16
EndEnumeration

LoadFont(#Font16, "Arial", 16, #PB_Font_HighQuality)

Procedure StrGdgtVertCtr(iGdgtID.i, iLeftMargin.i)
;#------------------------------------------------
;NOTE, for this to work, the StringGadget MUST have the #ES_MULTILINE flag applied
;
Protected      rRect.RECT
Protected      fSize.SIZE
Protected      iHwnd.i = GadgetID(iGdgtID)
Protected   iLineCnt.i = SendMessage_(iHwnd, #EM_GETLINECOUNT, 0, 0)
Protected       iHdc.i = GetDC_(iHwnd)
Protected       sStr.s = "ABgy"        ;length = 4 chars
Protected    iStrLen.i = Len(sStr)
Protected iVertTweak.i = 0 ;corrects the displayed result (requires more logic!)

                      SelectObject_(iHdc, GetGadgetFont(iGdgtID))
              GetTextExtentPoint32_(iHdc, sStr, iStrLen, fSize) ;width & height of sStr
                     GetClientRect_(iHwnd, rRect)
                         ReleaseDC_(iHwnd, iHdc)

                   rRect\left = iLeftMargin
                    rRect\top = ((GadgetHeight(iGdgtID) - (fSize\cy * iLineCnt)) / 2)
                 rRect\bottom = (rRect\top + (fSize\cy * iLineCnt) + iVertTweak)

              Debug "                 Text-->" + GetGadgetText(iGdgtID) + "<--"
              Debug "              iGdgtID-->" + Str(iGdgtID) + "<--"
              Debug "             fSize\cy-->" + Str(fSize\cy) + "<--"
              Debug "             iLineCnt-->" + Str(iLineCnt) + "<--"
              Debug "            rRect\Top-->" + Str(rRect\Top) + "<--"
              Debug "         rRect\Bottom-->" + Str(rRect\Bottom) + "<--"
              Debug "GadgetHeight(iGdgtID)-->" + Str(GadgetHeight(iGdgtID)) + "<--"
              Debug "========================================="
              Debug " "

              If rRect\bottom < GadgetHeight(iGdgtID)

                      SendMessage_(iHwnd, #EM_SETRECT, 0, rRect)
              EndIf
EndProcedure

If OpenWindow(#Win,0,0,500,250,"STRING GADGET TEXT",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)

        SetGadgetFont(#PB_All,FontID(#Font16))
          PanelGadget(#Pnl,0,0,500,250)
        AddGadgetItem(#Pnl, #Tab0, "Tab 0")
      ContainerGadget(#Cont,10,10,470,190)
         StringGadget(#Str1,10,10,450,50,"TEXT")
         StringGadget(#Str2,10,80,450,50,"TEXT VERTICALLY CENTERED", #ES_MULTILINE|#PB_String_BorderLess)
       SetGadgetColor(#Cont, #PB_Gadget_BackColor, RGB(96,96,96))
      CloseGadgetList()
      CloseGadgetList()
       StrGdgtVertCtr(#Str2, 6)

       Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf


This can also be achieved without API using a StringGadget() set inside a ContainerGadget().

EDIT: Tweaked the above code, should all make sense with the exception of how the Vertical Tweak value is determined. I have found that single lines of text look fine without tweaking, it's all a matter of preference.

_________________
IdeasVacuum
If it sounds simple, you have not grasped the complexity.


Last edited by IdeasVacuum on Thu Sep 07, 2017 3:40 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: StringGadget Text Vertically Centred
PostPosted: Thu Sep 07, 2017 11:06 am 
Offline
Addict
Addict
User avatar

Joined: Tue Oct 09, 2007 2:15 am
Posts: 871
+1 I also want to...

@IdeasVacuum: Your Windows workaround... It is very important, that the StringGadget have the flag #ES_MULTILINE. Otherwise it don't work.
It cost much time and searching to find this out ;)

_________________
PureBasic 5.45 LTS / 5.61 (Windows x86/x64) | Windows10 Pro x64 | Z87-PLUS | i7 4770k | 32GB RAM | iChill GeForce GTX 980 X4 Ultra | HAF XF Evo​​
English is not my native language...


Top
 Profile  
Reply with quote  
 Post subject: Re: StringGadget Text Vertically Centred
PostPosted: Thu Sep 07, 2017 2:29 pm 
Offline
Always Here
Always Here

Joined: Fri Oct 23, 2009 2:33 am
Posts: 5688
Location: Wales, UK
Hi Bisonte

Sorry about that, I thought the code explained - it's not my work, it is listed in RS Basic's API library. In actual fact, I think it can be enhanced - the "magic number" 4 for example - there should be some logic behind that value. I should point out that I added the left-side margin rRect\left = 6. This would be better if the value was passed to the Procedure, since it's firstly a matter of preference and secondly it is only good if the StringGadget is Left or Right Justified.

It is good that PB works so seamlessly with Windows API but I think this is one of many examples where PB should have it's own cross-platform function.

_________________
IdeasVacuum
If it sounds simple, you have not grasped the complexity.


Top
 Profile  
Reply with quote  
 Post subject: Re: StringGadget Text Vertically Centred
PostPosted: Wed Sep 20, 2017 11:11 am 
Offline
Enthusiast
Enthusiast

Joined: Fri Aug 28, 2015 6:10 pm
Posts: 727
Location: Portugal
+1

When I design a form with a string gadget the caption is shown centered but when the programme is run it is top left?

Not using the code above and running on windows.

CD

_________________
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.


Top
 Profile  
Reply with quote  
 Post subject: Re: StringGadget Text Vertically Centred
PostPosted: Wed Sep 20, 2017 7:18 pm 
Offline
Enthusiast
Enthusiast

Joined: Mon Nov 25, 2013 6:41 am
Posts: 607
+1


Top
 Profile  
Reply with quote  
 Post subject: Re: StringGadget Text Vertically Centred
PostPosted: Wed Sep 20, 2017 10:50 pm 
Offline
Addict
Addict

Joined: Fri Nov 09, 2012 11:04 pm
Posts: 1462
Location: Uttoxeter, UK
+1

_________________
DE AA EB


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 9 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