Title: Zoom to tray
Question: How do I add a minimize/maximize effect to forms with tray icons?
Answer:
We usually see a 'zoom' effect when a window maximize or minimize. Unfortunately this effect only applies to icons in the application bar and apparently there is no way to add a similar effect to a form that 'minimize to tray area'.
The Windows API enables us to add that effect in a quick and easy way using the DrawAnimatedRects function.
This function requires a window handle and two rectangles with starting and ending screen coordinates.
Here is a snippet of code that show how to use the API:
unit TestForm;
interface
uses
Windows, Classes, Forms, Controls, StdCtrls, ExtCtrls;
type
TZoomAction = (zaMinimize, zaMaximize);
TfrmTest = class(TForm)
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormShow(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmTest: TfrmTest;
implementation
{$R *.DFM}
procedure ZoomEffect(theForm: TForm; theOperation: TZoomAction);
var
rcStart: TRect;
rcEnd: TRect;
rcTray: TRect;
hwndTray : hWnd;
hwndChild: hWnd;
begin
{ Find the system tray area bounding rectangle }
hwndTray := FindWindow('Shell_TrayWnd', nil);
hwndChild := FindWindowEx(hwndTray, 0, 'TrayNotifyWnd', nil);
GetWindowRect(hwndChild, rcTray);
{ Check for minimize/maximize and swap start/end}
if theOperation = zaMinimize then
begin
rcStart := theForm.BoundsRect;
rcEnd := rcTray;
end
else
begin
rcEnd := theForm.BoundsRect;
rcStart := rcTray;
end;
{ Here the magic happens... }
DrawAnimatedRects(theForm.Handle, IDANI_CAPTION, rcStart, rcEnd)
end;
procedure TfrmTest.FormClose(Sender: TObject; var Action: TCloseAction);
begin
ZoomEffect(Self, zaMinimize);
end;
procedure TfrmTest.FormShow(Sender: TObject);
begin
ZoomEffect(Self, zaMaximize);
end;
end.