Notice Of Closure

Please note that this website is due to close down on 15th June 2020.

In the interim I will try to move as much content as possible over to my GitHub account. Thanks to everyone who has supported the site over the years.

» Help

Animate tray application windows as they open and close


Have you noticed that when you close the window of a tray application it often doesn't actually close the application but simply hides the window and leaves the application running in the tray: it effectively minimizes the application to the tray. There's usually no visual clue that this has happened as there would be if you minimized a normal application.

We can change this behaviour by using the DrawAnimatedRects Windows API function. (See Tip #46 for an explanation of this function.)

Here is a little static class that your tray applications can use to animate the window caption, zooming to the tray when the window closes and zooming from the tray when it opens.

unit UTrayZoom;



  TTrayZoom = class(TObject)
    class function GetTrayRect: TRect;
    class procedure DoZoom(const Wnd: HWND; const Src, Dest: TRect);
    class procedure ZoomToTray(const Wnd: HWND);
    class procedure ZoomFromTray(const Wnd: HWND);


class procedure TTrayZoom.DoZoom(const Wnd: HWND; const Src, Dest: TRect);
  DrawAnimatedRects(Wnd, IDANI_CAPTION, Src, Dest);

class function TTrayZoom.GetTrayRect: TRect;
  TaskbarWnd, TrayWnd: HWND;
  TaskbarWnd := FindWindow('Shell_TrayWnd', nil);
  TrayWnd := FindWindowEx(TaskbarWnd, 0, 'TrayNotifyWnd', nil);
  GetWindowRect(TrayWnd, Result);

class procedure TTrayZoom.ZoomFromTray(const Wnd: HWND);
  WndRect: TRect;
  GetWindowRect(Wnd, WndRect);
  DoZoom(Wnd, GetTrayRect, WndRect);

class procedure TTrayZoom.ZoomToTray(const Wnd: HWND);
  WndRect: TRect;
  GetWindowRect(Wnd, WndRect);
  DoZoom(Wnd, WndRect, GetTrayRect);


Inspired by code from the article #1862 by Gian Luca Pepe

The DoZoom method simply wraps the call to DrawAnimatedRects. GetTrayRect gets the bounding rectangle of the tray area of the task bar. (See Tip #47 for details of how to find the tray window handle.) The two public methods ZoomToTray and ZoomFromTray zoom the specified window to and from the tray respectively. The Wnd parameter to these methods should be the window handle of the form that you are zooming to and from the tray.


When your tray application is opening from the tray call TTrayZoom.ZoomFromTray. You may possibly do this in a menu item's OnClick event handler. When your application is closing to the tray call TTrayZoom.ZoomToTray. This may be done in the form's OnClose or OnCloseQuery event handlers. Exactly where and when you call TTrayZoom's methods will vary from application to application.

Author: Peter Johnson
Contributor: Peter Johnson
Added: 2007-08-14
Last updated: 2007-08-14

« Return to contents »