TB2Anim.pas 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. unit TB2Anim;
  2. {
  3. Toolbar2000
  4. Copyright (C) 1998-2005 by Jordan Russell
  5. All rights reserved.
  6. The contents of this file are subject to the "Toolbar2000 License"; you may
  7. not use or distribute this file except in compliance with the
  8. "Toolbar2000 License". A copy of the "Toolbar2000 License" may be found in
  9. TB2k-LICENSE.txt or at:
  10. https://jrsoftware.org/files/tb2k/TB2k-LICENSE.txt
  11. Alternatively, the contents of this file may be used under the terms of the
  12. GNU General Public License (the "GPL"), in which case the provisions of the
  13. GPL are applicable instead of those in the "Toolbar2000 License". A copy of
  14. the GPL may be found in GPL-LICENSE.txt or at:
  15. https://jrsoftware.org/files/tb2k/GPL-LICENSE.txt
  16. If you wish to allow use of your version of this file only under the terms of
  17. the GPL and not to allow others to use your version of this file under the
  18. "Toolbar2000 License", indicate your decision by deleting the provisions
  19. above and replace them with the notice and other provisions required by the
  20. GPL. If you do not delete the provisions above, a recipient may use your
  21. version of this file under either the "Toolbar2000 License" or the GPL.
  22. $jrsoftware: tb2k/Source/TB2Anim.pas,v 1.11 2005/02/16 08:15:58 jr Exp $
  23. }
  24. interface
  25. {$I TB2Ver.inc}
  26. {$Q-}
  27. uses
  28. Windows, Messages, SysUtils, Classes;
  29. const
  30. WM_TB2K_STEPANIMATION = WM_USER + $555;
  31. WM_TB2K_ANIMATIONENDED = WM_USER + $556;
  32. type
  33. TTBAnimationDirection = set of (tbadLeft, tbadRight, tbadDown, tbadUp);
  34. procedure TBStartAnimation(const AWnd: HWND; const ABlend: Boolean;
  35. const ADirection: TTBAnimationDirection);
  36. procedure TBStepAnimation(const Msg: TMessage);
  37. procedure TBEndAnimation(const Wnd: HWND);
  38. implementation
  39. { Notes to self:
  40. - It originally had the NOMIRRORBITMAP flag on the BitBlt calls, because
  41. Windows 2000's AnimateWindow function has it. But it had to be removed
  42. because on Windows 98 with the Standard VGA or VMware video driver, it
  43. caused no bits to be blitted, even though Windows 98 is supposed to
  44. support NOMIRRORBITMAP according to the documentation. I don't think it's
  45. necessary anyway.
  46. }
  47. const
  48. DCX_USESTYLE = $10000;
  49. WS_EX_LAYERED = $80000;
  50. NOMIRRORBITMAP = $80000000;
  51. ULW_ALPHA = 2;
  52. type
  53. PAnimateThreadFuncData = ^TAnimateThreadFuncData;
  54. TAnimateThreadFuncData = record
  55. SequenceID: Integer;
  56. Wnd: HWND;
  57. Time: Integer;
  58. Blending: Boolean;
  59. CurStep: Integer;
  60. DC, BmpDC: HDC;
  61. Bmp: HBITMAP;
  62. ScreenClientRect: TRect;
  63. Size: TPoint;
  64. LastPos: TPoint;
  65. Direction: TTBAnimationDirection;
  66. AnimateThreadAbort: BOOL;
  67. AnimationEnded: BOOL;
  68. StepMessagePending: BOOL;
  69. end;
  70. function AnimateThreadFunc(Parameter: Pointer): Integer;
  71. var
  72. StartTime, FrameStartTime, NextFrameStartTime: DWORD;
  73. StartStep, ElapsedTime, I: Integer;
  74. P: TPoint;
  75. begin
  76. Result := 0;
  77. StartTime := GetTickCount;
  78. FrameStartTime := StartTime;
  79. with PAnimateThreadFuncData(Parameter)^ do begin
  80. StartStep := CurStep;
  81. while not AnimateThreadAbort do begin
  82. ElapsedTime := FrameStartTime - StartTime;
  83. if (ElapsedTime < 0) or (ElapsedTime >= Time) then
  84. Break;
  85. I := StartStep + ((255 * ElapsedTime) div Time);
  86. if (I < 0) or (I >= 255) then
  87. Break;
  88. GetCursorPos(P);
  89. if (P.X <> LastPos.X) or (P.Y <> LastPos.Y) then begin
  90. if PtInRect(ScreenClientRect, P) then
  91. Break;
  92. LastPos := P;
  93. end;
  94. if I > CurStep then begin
  95. CurStep := I;
  96. if InterlockedExchange(Integer(StepMessagePending), 1) = 0 then
  97. SendNotifyMessage(Wnd, WM_TB2K_STEPANIMATION, 0, SequenceID);
  98. end;
  99. { Wait until the timer has ticked at least 10 msec }
  100. NextFrameStartTime := GetTickCount;
  101. while NextFrameStartTime - FrameStartTime < 10 do begin
  102. { We don't know the rate of the system timer, so check every 5 msec
  103. to see if it's incremented }
  104. Sleep(5);
  105. NextFrameStartTime := GetTickCount;
  106. end;
  107. FrameStartTime := NextFrameStartTime;
  108. end;
  109. AnimationEnded := True;
  110. SendNotifyMessage(Wnd, WM_TB2K_STEPANIMATION, 0, SequenceID);
  111. end;
  112. end;
  113. threadvar
  114. AnimateThreadHandle: THandle;
  115. AnimateData: TAnimateThreadFuncData;
  116. AnimationSequenceID: Integer;
  117. procedure FinalizeAnimation;
  118. begin
  119. with PAnimateThreadFuncData(@AnimateData)^ do begin
  120. if Blending then
  121. SetWindowLong(Wnd, GWL_EXSTYLE,
  122. GetWindowLong(Wnd, GWL_EXSTYLE) and not WS_EX_LAYERED)
  123. else
  124. SetWindowRgn(Wnd, 0, False);
  125. BitBlt(DC, 0, 0, Size.X, Size.Y, BmpDC, 0, 0, SRCCOPY);
  126. DeleteDC(BmpDC);
  127. DeleteObject(Bmp);
  128. ReleaseDC(Wnd, DC);
  129. SendNotifyMessage(Wnd, WM_TB2K_ANIMATIONENDED, 0, 0);
  130. end;
  131. end;
  132. procedure TBEndAnimation(const Wnd: HWND);
  133. begin
  134. if (AnimateThreadHandle <> 0) and
  135. ((Wnd = 0) or (AnimateData.Wnd = Wnd)) then begin
  136. AnimateData.AnimateThreadAbort := True;
  137. WaitForSingleObject(AnimateThreadHandle, INFINITE);
  138. CloseHandle(AnimateThreadHandle);
  139. AnimateThreadHandle := 0;
  140. FinalizeAnimation;
  141. end;
  142. end;
  143. procedure TBStartAnimation(const AWnd: HWND; const ABlend: Boolean;
  144. const ADirection: TTBAnimationDirection);
  145. var
  146. ZeroPt: TPoint;
  147. R: TRect;
  148. ThreadID: TThreadID;
  149. Blend: TBlendFunction;
  150. Rgn: HRGN;
  151. begin
  152. TBEndAnimation(0);
  153. ZeroPt.X := 0;
  154. ZeroPt.Y := 0;
  155. Inc(AnimationSequenceID);
  156. FillChar(AnimateData, SizeOf(AnimateData), 0);
  157. { Note: The pointer cast avoids GetTls calls for every field access }
  158. with PAnimateThreadFuncData(@AnimateData)^ do begin
  159. SequenceID := AnimationSequenceID;
  160. Wnd := AWnd;
  161. Blending := ABlend;
  162. Direction := ADirection;
  163. GetCursorPos(LastPos);
  164. GetClientRect(Wnd, ScreenClientRect);
  165. MapWindowPoints(Wnd, 0, ScreenClientRect, 2);
  166. GetWindowRect(Wnd, R);
  167. DC := GetDCEx(Wnd, 0, DCX_WINDOW or DCX_CACHE {or DCX_USESTYLE ?});
  168. Size.X := R.Right - R.Left;
  169. Size.Y := R.Bottom - R.Top;
  170. Bmp := CreateCompatibleBitmap(DC, Size.X, Size.Y {or $01000000 ?});
  171. BmpDC := CreateCompatibleDC(DC);
  172. // AnimateWindow calls SetLayout, but I'm not sure that we need to.
  173. //if Assigned(SetLayoutProc) then
  174. // SetLayoutProc(BmpDC, 0);
  175. SelectObject(BmpDC, Bmp);
  176. //SetBoundsRect(BmpDC, nil, DCB_RESET or DCB_ENABLE);
  177. SendMessage(Wnd, WM_PRINT, WPARAM(BmpDC), PRF_NONCLIENT or PRF_CLIENT or
  178. PRF_ERASEBKGND or PRF_CHILDREN);
  179. //GetBoundsRect
  180. if Blending then begin
  181. SetWindowLong(Wnd, GWL_EXSTYLE, GetWindowLong(Wnd, GWL_EXSTYLE) or WS_EX_LAYERED);
  182. Time := 175; { actually more like ~147 because CurStep starts at 40 }
  183. CurStep := 40;
  184. Longint(Blend) := 0;
  185. Blend.BlendOp := AC_SRC_OVER;
  186. Blend.SourceConstantAlpha := CurStep;
  187. UpdateLayeredWindow(Wnd, 0, @R.TopLeft, @Size, BmpDC, @ZeroPt, 0,
  188. @Blend, ULW_ALPHA);
  189. end
  190. else begin
  191. Time := 150;
  192. CurStep := 0;
  193. Rgn := CreateRectRgn(0, 0, 0, 0);
  194. if not BOOL(SetWindowRgn(Wnd, Rgn, False)) then
  195. DeleteObject(Rgn); { just in case }
  196. end;
  197. { These are the same flags AnimateWindow uses. SWP_ASYNCWINDOWPOS is
  198. needed or else it doesn't "save bits" properly.
  199. Note: SWP_ASYNCWINDOWPOS seems to have no effect on Windows 95 & NT 4.0,
  200. so bits behind the window are not saved & restored correctly. }
  201. SetWindowPos(Wnd, 0, 0, 0, 0, 0, SWP_NOSIZE or SWP_NOMOVE or
  202. SWP_NOZORDER or SWP_NOACTIVATE or SWP_SHOWWINDOW or SWP_NOREDRAW or
  203. SWP_NOOWNERZORDER or SWP_ASYNCWINDOWPOS);
  204. end;
  205. AnimateThreadHandle := BeginThread(nil, 0, AnimateThreadFunc, @AnimateData,
  206. CREATE_SUSPENDED, ThreadID);
  207. if AnimateThreadHandle = 0 then begin
  208. { just in case... }
  209. FinalizeAnimation;
  210. Exit;
  211. end;
  212. ResumeThread(AnimateThreadHandle);
  213. end;
  214. procedure TBStepAnimation(const Msg: TMessage);
  215. var
  216. Rgn: HRGN;
  217. Blend: TBlendFunction;
  218. X, Y: Integer;
  219. begin
  220. if Msg.LParam <> AnimationSequenceID then
  221. { ignore messages dangling in the queue from aborted animation sequences }
  222. Exit;
  223. with PAnimateThreadFuncData(@AnimateData)^ do begin
  224. if not AnimationEnded then begin
  225. if Blending then begin
  226. Longint(Blend) := 0;
  227. Blend.BlendOp := AC_SRC_OVER;
  228. Blend.SourceConstantAlpha := AnimateData.CurStep;
  229. UpdateLayeredWindow(Wnd, 0, nil, nil, 0, nil, 0, @Blend, ULW_ALPHA);
  230. end
  231. else begin
  232. if tbadDown in Direction then
  233. Y := MulDiv(Size.Y, AnimateData.CurStep, 255) - Size.Y
  234. else if tbadUp in Direction then
  235. Y := Size.Y - MulDiv(Size.Y, AnimateData.CurStep, 255)
  236. else
  237. Y := 0;
  238. if tbadRight in Direction then
  239. X := MulDiv(Size.X, AnimateData.CurStep, 255) - Size.X
  240. else if tbadLeft in Direction then
  241. X := Size.X - MulDiv(Size.X, AnimateData.CurStep, 255)
  242. else
  243. X := 0;
  244. Rgn := CreateRectRgn(X, Y, X + Size.X, Y + Size.Y);
  245. if not BOOL(SetWindowRgn(Wnd, Rgn, False)) then
  246. DeleteObject(Rgn); { just in case }
  247. BitBlt(DC, X, Y, Size.X, Size.Y, BmpDC, 0, 0, SRCCOPY);
  248. end;
  249. end
  250. else
  251. TBEndAnimation(Wnd);
  252. StepMessagePending := False;
  253. end;
  254. end;
  255. initialization
  256. finalization
  257. TBEndAnimation(0);
  258. end.