Center Align String - OS Version specific issue?

Windows specific forum
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Center Align String - OS Version specific issue?

Post by jassing »

the code below sometimes works, sometimes doesn't.
On my laptop (win7 x64) it works, on my dev machine (win2003 x86) it doesn't. Any ideas?

Code: Select all

Macro CenterStringGadget(a)
  SetWindowLong_(GadgetID(a), #GWL_STYLE, GetWindowLong_(GadgetID(a), #GWL_STYLE)|#ES_CENTER)
EndMacro

OpenWindow(0, 370, 321, 535, 227, "Employee Clock In/Out", #PB_Window_SystemMenu|#PB_Window_TitleBar|#PB_Window_WindowCentered|#PB_Window_SizeGadget)
Frame3DGadget(0, 355, 0, 175, 42, "[ Current Time ]")
StringGadget(1, 360, 18, 165, 20, "YYYY-MM-DD HH:MM", #PB_String_ReadOnly)

CenterStringGadget( 1 )

Repeat
Until WaitWindowEvent(10)=#PB_Event_CloseWindow
User avatar
electrochrisso
Addict
Addict
Posts: 989
Joined: Mon May 14, 2007 2:13 am
Location: Darling River

Re: Center Align String - OS Version specific issue?

Post by electrochrisso »

Works ok here on Win7 Starter x86, might be something to do with win2003 being server based, later I will try on Vista x86, I will let you know if it does not work.
PureBasic! Purely the best 8)
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: Center Align String - OS Version specific issue?

Post by jassing »

electrochrisso wrote:Works ok here on Win7 Starter x86, might be something to do with win2003 being server based, later I will try on Vista x86, I will let you know if it does not work.
Thanks - odd that they would pull such a small detail out of an OS because it's server based... but then again, I rarely understand the logic of big companies...
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4955
Joined: Sun Apr 12, 2009 6:27 am

Re: Center Align String - OS Version specific issue?

Post by RASHAD »

@jassing Hi
I see you are using a StringGadget() as #PB_String_ReadOnly
So how about using TextGadget()
In that case you can center the text Hal & val at the same time with Border as well

Code: Select all

; Macro CenterStringGadget(a)
;   SetWindowLong_(GadgetID(a), #GWL_STYLE, GetWindowLong_(GadgetID(a), #GWL_STYLE)|#ES_CENTER)
; EndMacro

OpenWindow(0, 370, 321, 535, 227, "Employee Clock In/Out", #PB_Window_SystemMenu|#PB_Window_TitleBar|#PB_Window_WindowCentered|#PB_Window_SizeGadget)
TextGadget(0, 360, 0, 175, 20, "[ Current Time ]")
TextGadget(1, 360, 20, 165, 40, "YYYY-MM-DD HH:MM", #SS_CENTERIMAGE | #SS_CENTER | #WS_BORDER)

;CenterStringGadget( 1 )

Repeat
Until WaitWindowEvent(10)=#PB_Event_CloseWindow

Egypt my love
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: Center Align String - OS Version specific issue?

Post by jassing »

RASHAD wrote: I see you are using a StringGadget() as #PB_String_ReadOnly
So how about using TextGadget()
It's readonly, initially... If it can't be done, it can't be done..
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Center Align String - OS Version specific issue?

Post by MachineCode »

Try this and lose the macro:

Code: Select all

StringGadget(1, 360, 18, 165, 20, "YYYY-MM-DD HH:MM", #PB_String_ReadOnly | #ES_CENTER)
Works here. :)
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: Center Align String - OS Version specific issue?

Post by jassing »

MachineCode wrote:Try this and lose the macro:
BRILLIANT!

I just couldn't see that approach for some reason -- while I mix & match 'windows' constants & pb constants (ie: messagerequester() and #MB_ICONSTOP) I seem to forget I can do that elsewhere.

Thanks, and yes, it does work on win2003 w/o issue!
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Center Align String - OS Version specific issue?

Post by MachineCode »

Glad I could help. This new year my resolution is to help more, and never argue. :)

Mixing PureBasic constants with API ones can be done pretty much anywhere, although I think Freak said it's not always guaranteed to work in future PureBasic releases. I currently use it for disabling gadgets at design time. For example, if I want a ButtonGadget() to be disabled when created:

I don't use this longer code:

Code: Select all

ButtonGadget(#MyButton,661,261,35,21,"test")
DisableGadget(#MyButton,#True)
I just use this shortened version:

Code: Select all

ButtonGadget(#MyButton,661,261,35,21,"test",#WS_DISABLED)
On another topic, in your first post above you used SetWindowLong_(). In a PureBasic blog post, Freak said we should stop using SetWindowLong_() and use SetWindowLongPtr_() instead; and the same for GetWindowLong_() and to use GetWindowLongPtr_() instead. Using the "ptr" versions is better for 64-bit compatibility, and doesn't affect 32-bit apps on Windows. So, go through your source codes and do a search/replace of them now. :)
Last edited by MachineCode on Tue Jan 01, 2013 8:26 am, edited 1 time in total.
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: Center Align String - OS Version specific issue?

Post by jassing »

MachineCode wrote:]
On another topic, in your first post above you used SetWindowLong_(). In a PureBasic blog post, Freak said we should stop using SetWindowLong_() and use GetWindowLongPtr_() instead; and the same for GetWindowLong_() and to use GetWindowLongPtr_() instead. Using the "ptr" versions is better for 64-bit compatibility, and doesn't affect 32-bit apps on Windows. So, go through your source codes and do a search/replace of them now. :)
Thank you for pointing that out -- maybe that is a bit of a problem I've recently had...
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Center Align String - OS Version specific issue?

Post by MachineCode »

jassing wrote:Thank you for pointing that out -- maybe that is a bit of a problem I've recently had...
Actually, I just re-read the blog post, and apparently it only applies to subclassing. In any case, I've replaced all mine to use the "ptr" versions (even without subclassing) and all my apps still run fine. I'm going by the last sentence here:
http://www.purebasic.fr/blog/?m=200809 wrote:Do NOT use Get/SetWindowLong_() for subclassing anymore. These remain 32bit even on x64. Use Get/SetWindowLongPtr_() instead, which works correctly everywhere. There is really no need to use SetWindowLong_() over SetWindowLongPtr_() actually, so best do a search and replace and replace all of them right away.
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Center Align String - OS Version specific issue?

Post by Josh »

MachineCode wrote:... stop using SetWindowLong_() and use GetWindowLongPtr_() instead
I think, this wouldn't work fine :mrgreen:
sorry for my bad english
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Center Align String - OS Version specific issue?

Post by MachineCode »

Damn. Fixed. :oops:
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
User avatar
Michael Vogel
Addict
Addict
Posts: 2811
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Center Align String - OS Version specific issue?

Post by Michael Vogel »

Does anyone know, if there is an way to center a StringGadget vertically by using (windows specific) flags?

Sometimes it would be also enough to create a TextGadget showing the same border style as a StringGadget - but I don't know, if such a flag exists as well (@Rashad: WS_Border, SS_Sunken, SS_GrayRect etc. look different)

Code: Select all

OpenWindow(0, 370, 321, 535, 227, "Employee Clock In/Out", #PB_Window_SystemMenu|#PB_Window_TitleBar|#PB_Window_WindowCentered|#PB_Window_SizeGadget)
SetWindowColor(0,#White)
TextGadget(0, 160, 18, 165, 50, "YYYY-MM-DD HH:MM",#ES_CENTER|#SS_CENTERIMAGE||#SS_SUNKEN)
StringGadget(1, 360, 18, 165, 50, "YYYY-MM-DD HH:MM",#PB_String_ReadOnly|#ES_CENTER|#SS_CENTERIMAGE)

Repeat
Until WaitWindowEvent(10)=#PB_Event_CloseWindow
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4955
Joined: Sun Apr 12, 2009 6:27 am

Re: Center Align String - OS Version specific issue?

Post by RASHAD »

Hi MV
Happy new year
The TextGadget() is a static control so its flags or what it looks like are not available for
the StringGadget()
But next is a workaround
Do not stuck with MS rules my friend

Code: Select all

OpenWindow(0, 370, 321, 535, 227, "Employee Clock In/Out", #PB_Window_SystemMenu|#PB_Window_TitleBar|#PB_Window_WindowCentered|#PB_Window_SizeGadget)
SetWindowColor(0,#White)
TextGadget(0, 160, 18, 165, 50, "YYYY-MM-DD HH:MM",#ES_CENTER|#SS_CENTERIMAGE|#SS_SUNKEN)
StringGadget(1, 361, 18+(50-18)/2, 163, 18, "YYYY-MM-DD HH:MM",#PB_String_BorderLess|#PB_String_ReadOnly|#ES_CENTER|#WS_CLIPSIBLINGS)
TextGadget(2, 360, 18, 165, 50, "",#SS_SUNKEN|#WS_CLIPSIBLINGS)

Repeat
Until WaitWindowEvent(10)=#PB_Event_CloseWindow
Egypt my love
User avatar
Michael Vogel
Addict
Addict
Posts: 2811
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Center Align String - OS Version specific issue?

Post by Michael Vogel »

RASHAD wrote:Hi MV
Happy new year
Same to you, thank you...
...now I have to play a while with your code to understand clipsiblings and why you are more generous when using ES_Center for a static object :wink:

Below is a version to get a string type border...

Code: Select all

OpenWindow(0, 370, 321, 535, 227, "Employee Clock In/Out", #PB_Window_SystemMenu|#PB_Window_TitleBar|#PB_Window_WindowCentered|#PB_Window_SizeGadget)
SetWindowColor(0,#White)
TextGadget(0, 160, 18, 165, 50, "YYYY-MM-DD HH:MM",#ES_CENTER|#SS_CENTERIMAGE|#SS_SUNKEN)
TextGadget(2, 362, 20, 161, 46, "YYYY-MM-DD HH:MM",#ES_CENTER|#SS_CENTERIMAGE)
StringGadget(1, 360,18, 165, 50, "",#PB_String_ReadOnly|#WS_CLIPSIBLINGS)

Repeat
Until WaitWindowEvent(10)=#PB_Event_CloseWindow
Post Reply