Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Attach a script to a custom toolbar on windows taskbar


  • Please log in to reply
8 replies to this topic
jonib
  • Members
  • 75 posts
  • Last active: Feb 18 2017 04:44 PM
  • Joined: 09 May 2006
Hi all

I started this script as a mod to Veovis Sysinfo script

This sample script puts time and date on a custom toolbar named "CustomToolbar" that can be moved on the taskbar or even separated.

It should be easy to change MainInit and MainUpdate to have whatever you want on the toolbar.

I hope this is useful?
;Test script to show how to attach a script to a custom toolbar on windows
;taskbar, that works even if separated from the taskbar.
;author jonib

;Here you define what folder the custom toolbar is attached to:
ToolbarName=CustomToolbar
;How often script is updated in ms.
UpdateTime=1000

;This initializes the toolbar code and runs custom MainInit.
gosub CreateWindow

return

;Here you define all controls you want to show on the toolbar.
MainInit:
	Gui -Caption ToolWindow
	FormatTime, CurrentTime,,Time
	Gui Add, Text,vTheTime,%CurrentTime%
	FormatTime, CurrentDate,,ShortDate
	Gui Add, Text,vTheDate,%CurrentDate%
	Gui Show, x-639
return

;Here all controls you want to show gets updated using the timer.
MainUpdate:
	FormatTime, CurrentTime,,Time
	GuiControl,,TheTime ,%CurrentTime%
	FormatTime, CurrentDate,,ShortDate
	GuiControl,,TheDate ,%CurrentDate%
return

;This gets the handle to the toolbar, creates the window and attaches it to the
;toolbar, and activates the timer.
CreateWindow:
	hw_tray:=FindToolbar(ToolbarName)
	gosub MainInit
  Process Exist
  WinGet GuiID, ID, ahk_pid %ErrorLevel%
	if Not DockIt(hw_tray,GuiID)
	{
		msgbox, Toolbar not found
		exitapp
	}
	Gui Show, x%tX% y%tY%
	SetTimer WindowUpdate, %UpdateTime%
return

;The timer runs this code that checks if the toolbar has changed and runs the 
;MainUpdate routine
WindowUpdate:
	IfWinNotExist ,ahk_id %hw_tray%
	{
		gosub CreateWindow
	}
	gosub MainUpdate
return

;This just attaches the toolbar
DockIt(Parent,Child)
{
	return DllCall("SetParent",UInt,Child,UInt,Parent)
}

;This function checks what kind the toolbar is and gets the windows handle
FindToolbar(Name)
{	
	global tX tY
	loop
	{
		tX=5
		tY=5
		hwnd:=WinExist("ahk_class BaseBar",Name) ; Find Toolbar window
		if Not hwnd
		{
			tX=0
			tY=0
			WinExist("ahk_class Shell_TrayWnd") ; Find Systemtray
			WinGet, Controls ,ControlList ;Get list of all controls
			Loop, Parse, Controls, `n
			{
				ControlGetText, CurControl , %A_LoopField%
 	  		if CurControl=%Name% ;Find the toolbar we want by comparing controls Text
					ControlGet, hwnd, Hwnd,,%A_LoopField% ;Get the handle for the toolbar
			}
		}
		if hwnd
			break
	}
	return hwnd
}


Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
Nice! Could you mod it, such that the GUI could be larger than the toolbar it is docked to, or just hangs out in one side? I'd like to cover the name of the toolbar, too.

evl
  • Members
  • 1237 posts
  • Last active: Oct 20 2010 11:41 AM
  • Joined: 24 Aug 2005
@Laszlo: Did you know you can hide the name of the toolbar? If you right click on the taskbar and unlock it you can then right click on a toolbar's handle or name and choose a few extra options like hiding its title.

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
Thanks evl, and jonib! I did not know the title disabling trick. It is really handy. With that the Gui can cover completely the toolbar, what I wanted to achieve.

evl
  • Members
  • 1237 posts
  • Last active: Oct 20 2010 11:41 AM
  • Joined: 24 Aug 2005
@ jonib:

Nice idea here (just had a chance to check it out properly). I played with the code a little and posted an example along with my other SetParent functions:
http://www.autohotke...pic.php?p=60226

jonib
  • Members
  • 75 posts
  • Last active: Feb 18 2017 04:44 PM
  • Joined: 09 May 2006
@ evl

Cool idea to connect to a diffrent gui, makes it easy to have a master window and one or more toolbars, I will have to steal that. :D

jonib
  • Members
  • 75 posts
  • Last active: Feb 18 2017 04:44 PM
  • Joined: 09 May 2006
I'm having a bit of a problem, I wanted to put a progress bar on my toolbar, and it's working fine, but if I separate the toolbar from the taskbar
or attach it back to the taskbar the progress bar is not updated anymore but the clock is.

I assume it's because I recreate the Gui everytime the toolbar is attached to or detached from the taskbar, but the clock is working fine.

if I use reload instead of CreateWindow when the toolbar has changed it works.

Anybody have any ideas why the progress bar is not updated when the window is moved?

This code has a progress bar with CPU meter from Sysinfo otherwise it's the same as my last code.
;Test script to show how to attach a script to a custom toolbar on windows
;taskbar, that works even if separated from the taskbar.
;author jonib

;Here you define what folder the custom toolbar is attached to:
ToolbarName=CustomToolbar
;How often script is updated in ms.
UpdateTime=1000

;This initializes the toolbar code and runs custom MainInit.
gosub CreateWindow

return

;Here you define all controls you want to show on the toolbar.
MainInit:
	Gui Margin, 0, 0 
   Gui -Caption ToolWindow
   FormatTime, CurrentTime,,Time
   Gui Add, Text,vTheTime,%CurrentTime%
   FormatTime, CurrentDate,,ShortDate
   Gui Add, Text,vTheDate,%CurrentDate%
	Gui, Add, Progress,vMyProgress zx0 zy0 w80 h10 cBlue
   
   
   Gui Show, x-639
return

;Here all controls you want to show gets updated using the timer.
MainUpdate:
   FormatTime, CurrentTime,,Time
   GuiControl,,TheTime ,%CurrentTime%
   FormatTime, CurrentDate,,ShortDate
   GuiControl,,TheDate ,%CurrentDate%
	gosub ProcUpdate

return

ProcUpdate:
	;-----Get Processor------
	IdleTime0 = %IdleTime%  ; Save previous values
	Tick0 = %Tick%
	DllCall("kernel32.dll\GetSystemTimes", "uint",&IdleTicks, "uint",0, "uint",0)
	IdleTime := *(&IdleTicks)
		Loop 7                  ; Ticks when Windows was idle
	IdleTime += *( &IdleTicks + A_Index ) << ( 8 * A_Index )
	Tick := A_TickCount     ; #Ticks all together
	load := 100 - 0.01*(IdleTime - IdleTime0)/(Tick - Tick0)
	GuiControl,, MyProgress, %load%
return

;This gets the handle to the toolbar, creates the window and attaches it to the
;toolbar, and activates the timer.
CreateWindow:
   hw_tray:=FindToolbar(ToolbarName)
   gosub MainInit
  Process Exist
  WinGet GuiID, ID, ahk_pid %ErrorLevel%
   if Not DockIt(hw_tray,GuiID)
   {
      msgbox, Toolbar not found
      exitapp
   }
   Gui Show, x%tX% y%tY%
   SetTimer WindowUpdate, %UpdateTime%
return

;The timer runs this code that checks if the toolbar has changed and runs the
;MainUpdate routine
WindowUpdate:
   IfWinNotExist ,ahk_id %hw_tray%
   {
		gosub CreateWindow
;		reload
   }
   gosub MainUpdate
return

;This just attaches the toolbar
DockIt(Parent,Child)
{
   return DllCall("SetParent",UInt,Child,UInt,Parent)
}

;This function checks what kind the toolbar is and gets the windows handle
FindToolbar(Name)
{   
   global tX tY
   loop
   {
      tX=5
      tY=5
      hwnd:=WinExist("ahk_class BaseBar",Name) ; Find Toolbar window
      if Not hwnd
      {
         tX=0
         tY=0
         WinExist("ahk_class Shell_TrayWnd") ; Find Systemtray
         WinGet, Controls ,ControlList ;Get list of all controls
         Loop, Parse, Controls, `n
         {
            ControlGetText, CurControl , %A_LoopField%
            if CurControl=%Name% ;Find the toolbar we want by comparing controls Text
               ControlGet, hwnd, Hwnd,,%A_LoopField% ;Get the handle for the toolbar
         }
      }
      if hwnd
         break
   }
   return hwnd
}


Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
This works fine, even if I resize the toolbar. Why and how do you want to separate the toolbar from the taskbar and attach it back?

jonib
  • Members
  • 75 posts
  • Last active: Feb 18 2017 04:44 PM
  • Joined: 09 May 2006

This works fine, even if I resize the toolbar. Why and how do you want to separate the toolbar from the taskbar and attach it back?

Well the custom toolbar can be dragged and moved from the taskbar onto the desktop and then docked on the sides of the desktop.

I want to support this functionality, but there seems to be a problem recreating some controls with AHK.