home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 January / ChipCD_1.03.iso / zkuste / delphi / kompon / d23456 / COOLTRAY.ZIP / demos / MinimizeAnimation / TrayAnimation.pas < prev   
Pascal/Delphi Source File  |  2002-10-29  |  8KB  |  353 lines

  1. unit TrayAnimation;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Classes, Graphics, Forms;
  7.  
  8. type
  9.   TWindowFader = class(TThread)
  10.   private
  11.     BlendValue: Integer;
  12.     procedure Fade;
  13.   public
  14.     FadeOut: Boolean;
  15.     procedure Execute; override;
  16.   end;
  17.  
  18.   TWindowImploder = class(TThread)
  19.   private
  20.     X, Y, W, H: Integer;
  21.     procedure Implode;
  22.   public
  23.     Imploding: Boolean;
  24.     procedure Execute; override;
  25.   end;
  26.  
  27.   TWindowOutlineImploder = class(TThread)
  28.   private
  29.     X, Y, W, H: Integer;
  30.     DesktopCanvas: TCanvas;
  31.     procedure Implode;
  32.   public
  33.     Imploding: Boolean;
  34.     constructor Create;
  35.     destructor Destroy; override;
  36.     procedure Execute; override;
  37.   end;
  38.  
  39.  
  40.   procedure FloatingRectangles(Minimizing, OverrideUserSettings: Boolean);
  41.  
  42. implementation
  43.  
  44. uses
  45.   Types, Math, ShellApi, Messages, Main;
  46.  
  47. {----------------- Stand-alone methods ----------------}
  48.  
  49. procedure FloatingRectangles(Minimizing, OverrideUserSettings: Boolean);
  50. var
  51.   RectFrom, RectTo: TRect;
  52.   GotRectTo: Boolean;
  53.   abd: TAppBarData;
  54.   HTaskbar, HTrayWnd: HWND;
  55.   ResetRegistry: Boolean;
  56.   ai: TAnimationInfo;
  57.  
  58.   procedure SetAnimation(Animation: Boolean);
  59.   begin
  60.     FillChar(ai, SizeOf(ai), 0);
  61.     ai.cbSize := SizeOf(ai);
  62.     if Animation then
  63.       ai.iMinAnimate := 1
  64.     else
  65.       ai.iMinAnimate := 0;
  66.     SystemParametersInfo(SPI_SETANIMATION, 0, @ai, SPIF_SENDCHANGE);
  67.   end;
  68.  
  69. begin
  70.   // Check if user wants window animation
  71.   ResetRegistry := False;
  72.   if OverrideUserSettings then
  73.   begin
  74.     FillChar(ai, SizeOf(ai), 0);
  75.     ai.cbSize := SizeOf(ai);
  76.     SystemParametersInfo(SPI_GETANIMATION, 0, @ai, SPIF_SENDCHANGE);
  77.     if ai.iMinAnimate = 0 then
  78.     begin
  79.       // Temporarily enable window animation
  80.       ResetRegistry := True;
  81.       SetAnimation(True);
  82.     end;
  83.   end;
  84.  
  85.   RectFrom := MainForm.BoundsRect;
  86.   GotRectTo := False;
  87.  
  88.   // Get the traybar's bounding rectangle
  89.   HTaskbar := FindWindow('Shell_TrayWnd', nil);
  90.   if HTaskbar <> 0 then
  91.   begin
  92.     HTrayWnd := FindWindowEx(HTaskbar, 0, 'TrayNotifyWnd', nil);
  93.     if HTrayWnd <> 0 then
  94.       if GetWindowRect(HTrayWnd, RectTo) then
  95.         GotRectTo := True;
  96.   end;
  97.  
  98.   // If that fails, invent a rectangle in the corner where the traybar is
  99.   if not GotRectTo then
  100.   begin
  101.     FillChar(abd, SizeOf(abd), 0);
  102.     abd.cbSize := SizeOf(abd);
  103.     if SHAppBarMessage(ABM_GETTASKBARPOS, abd) = 0 then Exit;
  104.     with Screen, abd.rc do
  105.       if (Top > 0) or (Left > 0) then
  106.         RectTo := Rect(Width-32, Height-32, Width, Height)
  107.       else if (Bottom < Height) then
  108.         RectTo := Rect(Width-32, 0, Width, 32)
  109.       else if (Right < Width) then
  110.         RectTo := Rect(0, Height-32, 32, Height);
  111.   end;
  112.  
  113.   if Minimizing then
  114.     DrawAnimatedRects(MainForm.Handle, IDANI_CAPTION, RectFrom, RectTo)
  115.   else
  116.     DrawAnimatedRects(MainForm.Handle, IDANI_CAPTION, RectTo, RectFrom);
  117.  
  118.   if ResetRegistry then
  119.     SetAnimation(False);               // Disable window animation
  120. end;
  121.  
  122. {-------------------- TWindowFader --------------------}
  123.  
  124. procedure TWindowFader.Execute;
  125. begin
  126.   BlendValue := MainForm.AlphaBlendValue;
  127.   while not Terminated do
  128.   begin
  129.     if FadeOut then
  130.       Dec(BlendValue, 25)
  131.     else
  132.       Inc(BlendValue, 25);
  133.     Sleep(10);
  134. //    Application.ProcessMessages;
  135.     Synchronize(Fade);
  136.     if (BlendValue <= 0) or (BlendValue >= 255) then
  137.       Terminate;
  138.   end;
  139. end;
  140.  
  141.  
  142. procedure TWindowFader.Fade;
  143. begin
  144.   if (BlendValue >= 0) and (BlendValue <= 255) then
  145.     MainForm.AlphaBlendValue := BlendValue;
  146. end;
  147.  
  148. {------------------ TWindowImploder -------------------}
  149.  
  150. procedure TWindowImploder.Execute;
  151. const
  152.   minW = 120;
  153.   minH = 25;
  154.   deltaGrowth = 0.2;
  155. var
  156.   maxW, maxH: Integer;
  157.   deltaW, deltaH: Integer;
  158. begin
  159.   with MainForm do
  160.   begin
  161.     X := Left;
  162.     Y := Top;
  163.     W := Width;
  164.     H := Height;
  165.     if Imploding then
  166.     begin
  167.       // Store current form size
  168.       StartX := Left;
  169.       StartY := Top;
  170.       StartW := Width;
  171.       StartH := Height;
  172.     end;
  173.     // Remember previous form size
  174.     maxW := StartW;
  175.     maxH := StartH;
  176.   end;
  177.  
  178.   while not Terminated do
  179.   begin
  180.     deltaW := Round((W-minW) * deltaGrowth);
  181.     deltaH := Round((H-minH) * deltaGrowth);
  182.     if deltaW = 0 then
  183.       Inc(deltaW);
  184.     if Odd(deltaW) then
  185.       Inc(deltaW);
  186.     if deltaH = 0 then
  187.       Inc(deltaH);
  188.     if Odd(deltaH) then
  189.       Inc(deltaH);
  190.     if Imploding then
  191.     begin
  192.       W := W - deltaW;
  193.       H := H - deltaH;
  194.       X := X + (deltaW div 2);
  195.       Y := Y + (deltaH div 2);
  196.     end
  197.     else
  198.     begin
  199.       W := W + deltaW;
  200.       H := H + deltaH;
  201.       X := X - (deltaW div 2);
  202.       Y := Y - (deltaH div 2);
  203.     end;
  204.     Sleep(10);
  205.  
  206.     if (Imploding and ((W <= minW) or (H <= minH) or (deltaW = 0))) or
  207.        (not Imploding and ((W >= maxW) or (H >= maxH) or (deltaH = 0))) then
  208.       Terminate;
  209.  
  210.     if not Terminated then
  211.       Synchronize(Implode);
  212.     Application.ProcessMessages;
  213.   end;
  214.  
  215.   if not Imploding then
  216.   begin
  217.     with MainForm do
  218.       SetWindowPos(Handle, 0, StartX, StartY, StartW, StartH, SWP_NOZORDER);
  219.     Application.ProcessMessages;
  220.   end;
  221. end;
  222.  
  223.  
  224. procedure TWindowImploder.Implode;
  225. begin
  226.   SetWindowPos(MainForm.Handle, 0, X, Y, W, H, SWP_NOZORDER);
  227. end;
  228.  
  229. {--------------- TWindowOutlineImploder ---------------}
  230.  
  231. constructor TWindowOutlineImploder.Create;
  232. begin
  233.   inherited Create(False);
  234.   DesktopCanvas := TCanvas.Create;
  235.   with DesktopCanvas do
  236.   begin
  237.     Handle := GetDC(0);      // HDC of desktop
  238. //    Handle := GetDC(GetDesktopWindow);
  239.     Pen.Mode := pmNotXor;
  240.     Pen.Style := psDot;
  241.     Pen.Width := 2;
  242.     Pen.Color := clGray;
  243. //    Brush.Color := clGray;
  244. //    Brush.Style := bsDiagCross;
  245.     Brush.Style := bsClear;
  246.   end;
  247. end;
  248.  
  249.  
  250. destructor TWindowOutlineImploder.Destroy;
  251. begin
  252. //  ReleaseDC(GetDesktopWindow, DesktopCanvas.Handle);
  253.   ReleaseDC(0, DesktopCanvas.Handle);
  254.   DesktopCanvas.Handle := 0;
  255.   DesktopCanvas.Free;
  256.   DesktopCanvas := nil;
  257.   inherited Destroy;
  258. end;
  259.  
  260.  
  261. procedure TWindowOutlineImploder.Execute;
  262. const
  263.   minW = 25;
  264.   minH = 25;
  265.   deltaGrowth = 0.25;
  266. var
  267.   maxW, maxH: Integer;
  268.   deltaW, deltaH: Integer;
  269. begin
  270.   with MainForm do
  271.   begin
  272.     if Imploding then
  273.     begin
  274.       X := Left;
  275.       Y := Top;
  276.       W := Width;
  277.       H := Height;
  278.       // Store current form size
  279.       StartX := Left;
  280.       StartY := Top;
  281.       StartW := Width;
  282.       StartH := Height;
  283.       CoolTrayIcon1.HideMainForm;
  284.     end
  285.     else
  286.     begin
  287.       X := StartX + ((StartW-minW) div 2);
  288.       Y := StartY + ((StartH-minH) div 2);
  289.       W := minW;
  290.       H := minH;
  291.     end;
  292.     // Remember previous form size
  293.     maxW := StartW;
  294.     maxH := StartH;
  295.   end;
  296.  
  297.   while not Terminated do
  298.   begin
  299.     deltaW := Round((W-minW) * deltaGrowth);
  300.     deltaH := Round((H-minH) * deltaGrowth);
  301.     if deltaW = 0 then
  302.       Inc(deltaW);
  303.     if Odd(deltaW) then
  304.       Inc(deltaW);
  305.     if deltaH = 0 then
  306.       Inc(deltaH);
  307.     if Odd(deltaH) then
  308.       Inc(deltaH);
  309.     if Imploding then
  310.     begin
  311.       W := W - deltaW;
  312.       H := H - deltaH;
  313.       X := X + (deltaW div 2);
  314.       Y := Y + (deltaH div 2);
  315.     end
  316.     else
  317.     begin
  318.       W := W + deltaW;
  319.       H := H + deltaH;
  320.       X := X - (deltaW div 2);
  321.       Y := Y - (deltaH div 2);
  322.     end;
  323.     Synchronize(Implode);
  324.  
  325.     if (Imploding and ((W <= minW) or (H <= minH) or (deltaW = 0))) or
  326.        (not Imploding and ((W >= maxW) or (H >= maxH) or (deltaH = 0))) then
  327.       Terminate;
  328.   end;
  329. end;
  330.  
  331.  
  332. procedure TWindowOutlineImploder.Implode;
  333. {var
  334.   R: TRect;}
  335. begin
  336.   if not Terminated then
  337.     if DesktopCanvas <> nil then
  338.       if DesktopCanvas.HandleAllocated then
  339.       begin
  340. //        R := Rect(X, Y, X+W, Y+H);
  341. //        InvalidateRect(DesktopCanvas.Handle, @R, True);
  342. //        PostMessage(DesktopCanvas.Handle, WM_SETREDRAW, 1, 0);
  343. //        RedrawWindow(DesktopCanvas.Handle, 0, 0, RDW_ERASE or RDW_INVALIDATE or RDW_ERASENOW);
  344. //        UpdateWindow(DesktopCanvas.Handle);
  345.         DesktopCanvas.Rectangle(X, Y, X+W, Y+H);
  346.         Sleep(10);
  347.         DesktopCanvas.Rectangle(X, Y, X+W, Y+H);
  348.       end;
  349. end;
  350.  
  351. end.
  352.  
  353.