Titlebar clock

Share your advanced PureBasic knowledge/code with the community.
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Titlebar clock

Post by gnozal »

Code updated for 5.20+

Just for fun (I was playing with GetForegroundWindow_() ...)

This little app displays the date & time in the titlebar of any foreground window.

Code: Select all

Global PopUpActif.b
If CreatePopupMenu(0)
  MenuItem(1, "Quit TitleBar-Clock")
EndIf
; -----------------------------------------------------------
Procedure.s WMGetText(Handle.l)
  
  Buffer.s = Space(255)
  SendMessage_(Handle, #WM_GETTEXT, 256, @Buffer)
  result.s = PeekS(@Buffer)
  ProcedureReturn result
  
EndProcedure
;
; -----------------------------------------------------------
Procedure WMSetText(Handle.l, Text.s)
  
  SendMessage_(Handle, #WM_SETTEXT, 0, Text.s)
  
EndProcedure
; -----------------------------------------------------------
Procedure DoEvents()
  
  If PopUpActif = 0
    msg.MSG
    If PeekMessage_(msg,0,0,0,1)
      TranslateMessage_(msg)
      DispatchMessage_(msg)
    Else
      Delay(1)
    EndIf
  Else
    Delay(1)
  EndIf
  
EndProcedure
; -----------------------------------------------------------
hWnd.l = 0
Last_hWnd.l = 0
WindowText.s = ""
LastWindowText.s = ""
NewWindowText.s = ""
Heure.s = ""
LastHeure.s = ""
OpenWindow(0,0,0,100,100,"TitleBar-Clock",#PB_Window_Invisible)
Icone1_ID.l = CatchImage(1, ?Icone1)
If Icone1_ID
  AddSysTrayIcon(1, WindowID(0), Icone1_ID)
  SysTrayIconToolTip(1, "TitleBar-Clock")
Else
  End
EndIf
Repeat
  ; Events
  Event.l = WindowEvent()
  PopUpActif = 0
  If Event = #PB_Event_SysTray
    EvenTypeID.l = EventType()
    If EvenTypeID = #PB_EventType_LeftDoubleClick Or EvenTypeID = #PB_EventType_RightClick
      DisplayPopupMenu(0, WindowID(0))
      PopUpActif = 1
    EndIf
  ElseIf Event = #PB_Event_Menu
    EventIDMenu.l = EventMenu()
    If EventIDMenu.l = 1
      CloseWindow(0)
      End
    EndIf
  EndIf
  ; Do Events
  DoEvents()
  ; Clock stuff
  hWnd = GetForegroundWindow_()
  If hWnd <> Last_hWnd
    If WindowText <> ""
      If Last_hWnd <> 0
        WMSetText(Last_hWnd,WindowText)
      EndIf
    EndIf
    If hWnd <> 0
      WindowText = WMGetText(hWnd)
      LastWindowText = WindowText
      Last_hWnd = hWnd
    EndIf
  Else
    If hWnd <> 0
      NewWindowText = WMGetText(hWnd)
      Debug "NewWindowText = " + NewWindowText
      Debug "WindowText = " + WindowText
      Debug "LastWindowText = " + LastWindowText
      If NewWindowText <> LastWindowText
        WindowText = NewWindowText
      EndIf
      Heure.s = FormatDate("%dd/%mm/%yyyy  %hh:%ii:%ss", Date())
      If LastHeure <> Heure
        LastWindowText = WindowText + "  -  " + Heure
        WMSetText(hWnd,LastWindowText)
        SysTrayIconToolTip(1, "TitleBar-Clock - " + Heure)
        LastHeure = Heure
      EndIf
    EndIf
  EndIf
ForEver
DataSection
  Icone1:
  IncludeBinary #PB_Compiler_Home+"Examples\Sources\Data\CDPlayer.ico"
EndDataSection
Hi-Toro
Enthusiast
Enthusiast
Posts: 269
Joined: Sat Apr 26, 2003 3:23 pm

Cool!

Post by Hi-Toro »

Very cool indeed! :)
James Boyd
http://www.hi-toro.com/
Death to the Pixies!
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

that's an interesting piece of code :D which remember me titleclock on the amiga workbench... :roll:
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
Fred
Administrator
Administrator
Posts: 18226
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

Excellent :)
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

Wow! that is a really cool idea :) i might have to tinker with this to put the cpu and system temperature in the title too :twisted:
--Kale

Image
Post Reply