Page 1 of 2

Center Align String - OS Version specific issue?

Posted: Sun Dec 30, 2012 9:21 pm
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

Re: Center Align String - OS Version specific issue?

Posted: Sun Dec 30, 2012 10:39 pm
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.

Re: Center Align String - OS Version specific issue?

Posted: Sun Dec 30, 2012 10:57 pm
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...

Re: Center Align String - OS Version specific issue?

Posted: Mon Dec 31, 2012 4:01 am
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


Re: Center Align String - OS Version specific issue?

Posted: Mon Dec 31, 2012 4:28 am
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..

Re: Center Align String - OS Version specific issue?

Posted: Mon Dec 31, 2012 2:13 pm
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. :)

Re: Center Align String - OS Version specific issue?

Posted: Mon Dec 31, 2012 6:20 pm
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!

Re: Center Align String - OS Version specific issue?

Posted: Tue Jan 01, 2013 2:56 am
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. :)

Re: Center Align String - OS Version specific issue?

Posted: Tue Jan 01, 2013 4:55 am
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...

Re: Center Align String - OS Version specific issue?

Posted: Tue Jan 01, 2013 6:52 am
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.

Re: Center Align String - OS Version specific issue?

Posted: Tue Jan 01, 2013 8:17 am
by Josh
MachineCode wrote:... stop using SetWindowLong_() and use GetWindowLongPtr_() instead
I think, this wouldn't work fine :mrgreen:

Re: Center Align String - OS Version specific issue?

Posted: Tue Jan 01, 2013 8:26 am
by MachineCode
Damn. Fixed. :oops:

Re: Center Align String - OS Version specific issue?

Posted: Tue Jan 01, 2013 1:04 pm
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

Re: Center Align String - OS Version specific issue?

Posted: Tue Jan 01, 2013 1:48 pm
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

Re: Center Align String - OS Version specific issue?

Posted: Tue Jan 01, 2013 2:13 pm
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