Issue with custom systray clock

Just starting out? Need help? Post your questions and find answers here.
firace
Addict
Addict
Posts: 947
Joined: Wed Nov 09, 2011 8:58 am

Issue with custom systray clock

Post by firace »

Happy New Year!

I'm fooling around with the below systray clock code (on Windows 10) and it's working fine, except for a little bug: at the start of every minute, the standard Windows clock briefly appears instead of my custom display. What would be a quick way to solve that?

Code: Select all


hwnd = FindWindow_("Shell_TrayWnd",#Null)
hwnd = FindWindowEx_(hwnd,#Null,"TrayNotifyWnd",#Null)
hwnd = FindWindowEx_(hwnd,#Null,"TrayClockWClass",#Null)

GetWindowRect_(hwnd,@Rect.rect)

Window_Width = Rect\right - Rect\left
Window_Height = Rect\bottom - Rect\Top - 4

OpenWindow(0,0,2,Window_Width,Window_Height+10,"Clock",#PB_Window_BorderLess)
; StickyWindow(0,1)
TextGadget(1,1,1,Window_Width,Window_Height,"")


SetParent_(WindowID(0),hwnd)
SetTimer_(WindowID(0), 0, 300, 0)

Repeat
  Select WaitWindowEvent()
      
    Case #WM_TIMER  :  SetGadgetText(1, "> "+ Hour(date()) +":" + Minute(date()) + ":" + second(date()))
      
  EndSelect
Forever

User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Re: Issue with custom systray clock

Post by Lunasole »

Hi, I like stuff you playing with :)
firace wrote:What would be a quick way to solve that?
Maybe just disabling original clock window will help (at least here I've checked and it worked nice without blinking, etc):

Code: Select all

EnableExplicit
Define hwnd = FindWindow_("Shell_TrayWnd",#Null)
Define hwnd2 = FindWindowEx_(hwnd,#Null,"TrayNotifyWnd",#Null)
hwnd = FindWindowEx_(hwnd2,#Null,"TrayClockWClass",#Null)

Define Rect.RECT
GetWindowRect_(hwnd, @Rect)
Define Window_Width = Rect\right - Rect\left
Define Window_Height = Rect\bottom - Rect\Top - 4

OpenWindow(0,0,0,Window_Width,Window_Height+10,"Clock",#PB_Window_BorderLess)
TextGadget(1,1,1,Window_Width,Window_Height,"")

SetParent_(WindowID(0),hwnd)
EnableWindow_(hwnd, #False)

SetTimer_(WindowID(0), 0, 300, 0)

Repeat
	Define E = WaitWindowEvent()
	Select E
		Case #WM_TIMER:  
			SetGadgetText(1, "> "+ Hour(Date()) +":" + Minute(Date()) + ":" + Second(Date()))
			; quit by pressing escape
			If GetAsyncKeyState_(#VK_ESCAPE)
				Break
			EndIf
	EndSelect
ForEver
EnableWindow_(hwnd, #True)
End
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
firace
Addict
Addict
Posts: 947
Joined: Wed Nov 09, 2011 8:58 am

Re: Issue with custom systray clock

Post by firace »

Glad you like that. :)

But unfortunately your version doesn't fix it here. :(
This is under a standard Win 10 x64 installation. I will try on a Win 8.1 machine later.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4997
Joined: Sun Apr 12, 2009 6:27 am

Re: Issue with custom systray clock

Post by RASHAD »

Replace

Code: Select all

EnableWindow_(hwnd, #False)
With

Code: Select all

ShowWindow_(hwnd,#SW_HIDE	)
Egypt my love
firace
Addict
Addict
Posts: 947
Joined: Wed Nov 09, 2011 8:58 am

Re: Issue with custom systray clock

Post by firace »

RASHAD wrote:Replace

Code: Select all

EnableWindow_(hwnd, #False)
With

Code: Select all

ShowWindow_(hwnd,#SW_HIDE	)
Nope, that just completely blanks out the clock area for me. :(
BTW, same result on Win 8.1 x86.

Just to clarify (if needed), the issue appears only when a new minute starts (ie, when seconds = 0)
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Re: Issue with custom systray clock

Post by Lunasole »

firace wrote: This is under a standard Win 10 x64 installation. I will try on a Win 8.1 machine later.
Well if it doesn't help on 10, probably 8 will be the same. I was trying on XP only
RASHAD wrote: With

Code: Select all

ShowWindow_(hwnd,#SW_HIDE	)
I also tried that, It's not a solution while using SetParent_(WindowID(0),hwnd).
Maybe if using SetParent_(WindowID(0),hwnd2) [following that my code], then something can be done this way
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
firace
Addict
Addict
Posts: 947
Joined: Wed Nov 09, 2011 8:58 am

Re: Issue with custom systray clock

Post by firace »

I just found a much quicker way to reproduce the problem without having to wait for a new minute to start:
Just press Win+D a couple of times (minimize all / unminimize all) to see it.
GJ-68
User
User
Posts: 32
Joined: Sun Jun 23, 2013 1:00 pm
Location: France (68)

Re: Issue with custom systray clock

Post by GJ-68 »

This one works on Win7 x64:

Code: Select all

EnableExplicit
Define hwnd = FindWindow_("Shell_TrayWnd",#Null)
Define hwnd2 = FindWindowEx_(hwnd,#Null,"TrayNotifyWnd",#Null)
hwnd = FindWindowEx_(hwnd2,#Null,"TrayClockWClass",#Null)

Define Rect.RECT, Rect2.RECT
GetWindowRect_(hwnd, @Rect)
GetWindowRect_(hwnd2, @Rect2)
Define Window_Width = Rect\right - Rect\left
Define Window_Height = Rect\bottom - Rect\Top - 4

OpenWindow(0,Rect\left - Rect2\left,0,Window_Width,Window_Height+10,"Clock",#PB_Window_BorderLess)
TextGadget(1,1,1,Window_Width,Window_Height,"")

SetParent_(WindowID(0),hwnd2)
ShowWindow_(hwnd,#SW_HIDE)
;EnableWindow_(hwnd, #False)

SetTimer_(WindowID(0), 0, 300, 0)

Repeat
   Define E = WaitWindowEvent()
   Select E
      Case #WM_TIMER: 
         SetGadgetText(1, "> "+ Hour(Date()) +":" + Minute(Date()) + ":" + Second(Date()))
         ; quit by pressing escape
         If GetAsyncKeyState_(#VK_ESCAPE)
            Break
         EndIf
   EndSelect
ForEver
ShowWindow_(hwnd,#SW_SHOW)
;EnableWindow_(hwnd, #True)
End
firace
Addict
Addict
Posts: 947
Joined: Wed Nov 09, 2011 8:58 am

Re: Issue with custom systray clock

Post by firace »

@GJ-68 Yes! This one looks perfect on Win 10 too :)

Thanks everyone!!!
infratec
Always Here
Always Here
Posts: 7666
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Issue with custom systray clock

Post by infratec »

Only as an additional tip:

Code: Select all

SetGadgetText(1, FormatDate("> %hh:%ii:%ss", Date()))
Bernd
firace
Addict
Addict
Posts: 947
Joined: Wed Nov 09, 2011 8:58 am

Re: Issue with custom systray clock

Post by firace »

Argggh! Another issue popped up I'm afraid. If another application adds or removes a systray icon while my code is running, the system clock disappears and positioning gets messed up... Is there some kind of systray change notification I should listen to and trigger a refresh somehow?
Last edited by firace on Sun Jan 01, 2017 10:29 pm, edited 1 time in total.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4997
Joined: Sun Apr 12, 2009 6:27 am

Re: Issue with custom systray clock

Post by RASHAD »

Try

Code: Select all

;EnableExplicit
hwnd = FindWindow_("Shell_TrayWnd",#Null)
hwnd = FindWindowEx_(hwnd,#Null,"TrayNotifyWnd",#Null)
hwnd = FindWindowEx_(hwnd,#Null,"TrayClockWClass",#Null)

GetWindowRect_(hwnd, @Rect.RECT)
Window_Width = Rect\right - Rect\left
Window_Height = Rect\bottom - Rect\Top - 4

LoadFont(0,"Georgia",12)
LoadFont(1,"Tahoma",8)
OpenWindow(0,Rect\right-60,Rect\Top+2,60,Window_Height,"Clock",#PB_Window_BorderLess)
StickyWindow(0,1)
TextGadget(1,1,1,60,Window_Height/2-2,"",#SS_CENTER|#SS_CENTERIMAGE)
SetGadgetFont(1,FontID(0))
TextGadget(2,1,Window_Height/2,60,Window_Height/2,"",#SS_CENTER|#SS_CENTERIMAGE)
SetGadgetFont(2,FontID(1))

;SetParent_(WindowID(0),hwnd2)
ShowWindow_(hwnd,#SW_HIDE)
;EnableWindow_(hwnd, #False)

SetTimer_(WindowID(0), 0, 300, 0)

Repeat
   Define E = WaitWindowEvent()
   Select E
      Case #WM_TIMER:
         SetGadgetText(1, RSet(""+ Hour(Date()),2,"0") +":" + RSet(""+Minute(Date()),2,"0"))
         SetGadgetText(2, RSet(""+ Second(Date()),2,"0"))
         ; quit by pressing escape
         If GetAsyncKeyState_(#VK_ESCAPE)
            Break
         EndIf
   EndSelect
ForEver
ShowWindow_(hwnd,#SW_SHOW)
;EnableWindow_(hwnd, #True)
End
Egypt my love
firace
Addict
Addict
Posts: 947
Joined: Wed Nov 09, 2011 8:58 am

Re: Issue with custom systray clock

Post by firace »

RASHAD wrote:...
No luck I'm afraid. See:

Image
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4997
Joined: Sun Apr 12, 2009 6:27 am

Re: Issue with custom systray clock

Post by RASHAD »

This snippet should be OK

Code: Select all

;EnableExplicit
hwnd = FindWindow_("Shell_TrayWnd",#Null)
hwnd = FindWindowEx_(hwnd,#Null,"TrayNotifyWnd",#Null)
hwnd = FindWindowEx_(hwnd,#Null,"TrayClockWClass",#Null)

GetWindowRect_(hwnd, @Rect.RECT)
Window_Width = Rect\right - Rect\left
Window_Height = Rect\bottom - Rect\Top

LoadFont(0,"Georgia",12)
LoadFont(1,"Tahoma",8)
IconName$ = #PB_Compiler_Home+"examples\sources\data\CdPlayer.ico"

OpenWindow(0,Rect\right-62,Rect\Top+2,64,Window_Height,"Clock",#PB_Window_BorderLess,hwnd)
;StickyWindow(0,1)
TextGadget(1,1,1,60,Window_Height/2,"",#SS_CENTER|#SS_CENTERIMAGE)
SetGadgetFont(1,FontID(0))
TextGadget(2,1,Window_Height/2,60,Window_Height/2,"",#SS_CENTER|#SS_CENTERIMAGE)
SetGadgetFont(2,FontID(1))

SetTimer_(WindowID(0), 0, 300, 0)

AddSysTrayIcon(0, WindowID(0), LoadImage(0,IconName$))

Repeat
   Define E = WaitWindowEvent()
   Select E
      Case #PB_Event_SysTray
        If EventType() = #PB_EventType_LeftDoubleClick
           RemoveSysTrayIcon(0)
        EndIf

      Case #WM_TIMER:
         SetGadgetText(1, RSet(""+ Hour(Date()),2,"0") +":" + RSet(""+Minute(Date()),2,"0"))
         SetGadgetText(2, RSet(""+ Second(Date()),2,"0"))
         ; quit by pressing escape
         If GetAsyncKeyState_(#VK_ESCAPE)
            Break
         EndIf
   EndSelect
ForEver
End
Egypt my love
firace
Addict
Addict
Posts: 947
Joined: Wed Nov 09, 2011 8:58 am

Re: Issue with custom systray clock

Post by firace »

@RASHAD Clever! Thanks for your help.
Post Reply