VrAnimate.pas
上传用户:hbszzs
上传日期:2008-08-20
资源大小:628k
文件大小:9k
源码类别:

Delphi控件源码

开发平台:

Delphi

  1. {*****************************************************}
  2. {                                                     }
  3. {     Varian Component Workshop                       }
  4. {                                                     }
  5. {     Varian Software NL (c) 1996-2000                }
  6. {     All Rights Reserved                             }
  7. {                                                     }
  8. {*****************************************************}
  9. unit VrAnimate;
  10. {$I VRLIB.INC}
  11. interface
  12. uses
  13.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  14.   VrTypes, VrControls, VrThreads;
  15. type
  16.   TVrAnimate = class(TVrGraphicImageControl)
  17.   private
  18.     FAutoSize: Boolean;
  19.     FStretch: Boolean;
  20.     FBitmap: TBitmap;
  21.     FFrameCount: Integer;
  22.     FTimer: TVrTimer;
  23.     FLoop: Boolean;
  24.     FActive: Boolean;
  25.     FOrientation: TVrOrientation;
  26.     FCurrentFrame: Integer;
  27.     FThreaded: Boolean;
  28.     FImageWidth: Integer;
  29.     FImageHeight: Integer;
  30.     FOnNotify: TNotifyEvent;
  31.     function GetInterval: Integer;
  32.     procedure SetInterval(Value: integer);
  33.     procedure SetBitmap(Value: TBitmap);
  34.     procedure SetActive(Value: Boolean);
  35.     procedure SetAutoSize(Value: Boolean);
  36.     procedure SetStretch(Value: Boolean);
  37.     procedure SetFrameCount(Value: Integer);
  38.     procedure SetOrientation(Value: TVrOrientation);
  39.     procedure SetCurrentFrame(Value: Integer);
  40.     procedure SetThreaded(Value: Boolean);
  41.     procedure UpdateImage;
  42.     procedure BitmapChanged(Sender: TObject);
  43.     procedure TimerEvent(Sender: TObject);
  44.   protected
  45.     function GetPalette: HPALETTE; override;
  46.     procedure Loaded; override;
  47.     procedure AdjustBounds;
  48.     procedure Paint; override;
  49.     function DestRect: TRect;
  50.     function GetImageRect(Index: Integer): TRect;
  51.   public
  52.     constructor Create(AOwner: TComponent); override;
  53.     destructor Destroy; override;
  54.     procedure SetBounds(ALeft, ATop, AWidth, AHeight: Integer); override;
  55.     property CurrentFrame: Integer read FCurrentFrame write SetCurrentFrame;
  56.   published
  57.     property Threaded: Boolean read FThreaded write SetThreaded default True;
  58.     property Interval: Integer read GetInterval write SetInterval default 150;
  59.     property FrameCount: integer read FFrameCount write SetFrameCount default 1;
  60.     property AutoSize: Boolean read FAutoSize write SetAutoSize default false;
  61.     property Stretch: Boolean read FStretch write SetStretch default false;
  62.     property Bitmap: TBitmap read FBitmap write SetBitmap;
  63.     property Loop: Boolean read FLoop write FLoop default True;
  64.     property Orientation: TVrOrientation read FOrientation write SetOrientation default voHorizontal;
  65.     property Active: Boolean read FActive write SetActive default false;
  66.     property OnNotify: TNotifyEvent read FOnNotify write FOnNotify;
  67.     property Transparent default false;
  68. {$IFDEF VER110}
  69.     property Anchors;
  70.     property Constraints;
  71. {$ENDIF}
  72.     property Color default clBlack;
  73.     property DragCursor;
  74. {$IFDEF VER110}
  75.     property DragKind;
  76. {$ENDIF}
  77.     property DragMode;
  78.     property Hint;
  79.     property ParentColor default false;
  80.     property ParentShowHint;
  81.     property PopupMenu;
  82.     property ShowHint;
  83.     property Visible;
  84.     property OnClick;
  85. {$IFDEF VER130}
  86.     property OnContextPopup;
  87. {$ENDIF}
  88.     property OnDblClick;
  89.     property OnDragDrop;
  90.     property OnDragOver;
  91. {$IFDEF VER110}
  92.     property OnEndDock;
  93. {$ENDIF}
  94.     property OnEndDrag;
  95.     property OnMouseDown;
  96.     property OnMouseMove;
  97.     property OnMouseUp;
  98. {$IFDEF VER110}
  99.     property OnStartDock;
  100. {$ENDIF}
  101.     property OnStartDrag;
  102.   end;
  103. implementation
  104. constructor TVrAnimate.Create(AOwner: TComponent);
  105. begin
  106.   inherited Create(AOwner);
  107.   ControlStyle := ControlStyle + [csOpaque] - [csDoubleClicks, csSetCaption];
  108.   Width := 50;
  109.   Height := 50;
  110.   Color := clBtnFace;
  111.   ParentColor := false;
  112.   Transparent := false;
  113.   FActive := false;
  114.   FFrameCount := 1;
  115.   FAutoSize := false;
  116.   FStretch := false;
  117.   FLoop := True;
  118.   FOrientation := voHorizontal;
  119.   FBitmap := TBitmap.Create;
  120.   FBitmap.OnChange := BitmapChanged;
  121.   FThreaded := True;
  122.   FTimer := TVrTimer.Create(Self);
  123.   FTimer.Enabled := false;
  124.   FTimer.Interval := 150;
  125.   FTimer.OnTimer := TimerEvent;
  126. end;
  127. destructor TVrAnimate.Destroy;
  128. begin
  129.   FTimer.Free;
  130.   FBitmap.Free;
  131.   inherited Destroy;
  132. end;
  133. procedure TVrAnimate.Loaded;
  134. begin
  135.   inherited Loaded;
  136.   UpdateImage;
  137. end;
  138. procedure TVrAnimate.SetBitmap(Value: TBitMap);
  139. begin
  140.   FBitmap.Assign(Value);
  141. end;
  142. procedure TVrAnimate.BitmapChanged(Sender: TObject);
  143. begin
  144.   Active := false;
  145.   UpdateImage;
  146.   UpdateControlCanvas;
  147. end;
  148. function TVrAnimate.GetInterval: Integer;
  149. begin
  150.   Result := FTimer.Interval;
  151. end;
  152. procedure TVrAnimate.SetInterval(Value: Integer);
  153. begin
  154.   FTimer.Interval := Value;
  155. end;
  156. procedure TVrAnimate.SetActive(Value: Boolean);
  157. begin
  158.   if FActive <> Value then
  159.   begin
  160.     FActive := Value;
  161.     if Value then FCurrentFrame := 0;
  162.     if not Designing then
  163.       FTimer.Enabled := Value;
  164.   end;
  165. end;
  166. procedure TVrAnimate.SetAutoSize(Value: Boolean);
  167. begin
  168.   if FAutoSize <> Value then
  169.   begin
  170.     FAutoSize :=  Value;
  171.     AdjustBounds;
  172.     UpdateControlCanvas;
  173.   end;
  174. end;
  175. procedure TVrAnimate.SetStretch(Value: Boolean);
  176. begin
  177.   if FStretch <> Value then
  178.   begin
  179.     FStretch := Value;
  180.     UpdateControlCanvas;
  181.   end;
  182. end;
  183. procedure TVrAnimate.SetFrameCount(Value: Integer);
  184. begin
  185.   if (FFrameCount <> Value) and (Value > 0) then
  186.   begin
  187.     FFrameCount := Value;
  188.     UpdateImage;
  189.     UpdateControlCanvas;
  190.   end;
  191. end;
  192. procedure TVrAnimate.SetOrientation(Value: TVrOrientation);
  193. begin
  194.   if FOrientation <> Value then
  195.   begin
  196.     FOrientation := Value;
  197.     UpdateImage;
  198.     UpdateControlCanvas;
  199.   end;
  200. end;
  201. procedure TVrAnimate.SetThreaded(Value: Boolean);
  202. begin
  203.   if FThreaded <> Value then
  204.   begin
  205.     FThreaded := Value;
  206.     if Value then FTimer.TimerType := ttThread
  207.     else FTimer.TimerType := ttSystem;
  208.   end;
  209. end;
  210. function TVrAnimate.GetPalette: HPALETTE;
  211. begin
  212.   Result := 0;
  213.   if not Bitmap.Empty then Result := Bitmap.Palette;
  214. end;
  215. procedure TVrAnimate.AdjustBounds;
  216. begin
  217.   if (AutoSize) and (not Bitmap.Empty) then
  218.     SetBounds(Left, Top, FImageWidth, FImageHeight);
  219. end;
  220. procedure TVrAnimate.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
  221. begin
  222.   if AutoSize then
  223.     if not Bitmap.Empty then
  224.     begin
  225.       AWidth := FImageWidth;
  226.       AHeight := FImageHeight;
  227.     end;
  228.   inherited SetBounds(ALeft, ATop, AWidth, AHeight);
  229. end;
  230. procedure TVrAnimate.UpdateImage;
  231. begin
  232.   FImageWidth := 1;
  233.   FImageHeight := 1;
  234.   FCurrentFrame := 0;
  235.   if not Bitmap.Empty then
  236.   begin
  237.     if Orientation = voHorizontal then
  238.     begin
  239.       FImageWidth := Bitmap.Width div FrameCount;
  240.       FImageHeight := Bitmap.Height;
  241.     end
  242.     else
  243.     begin
  244.       FImageWidth := Bitmap.Width;
  245.       FImageHeight := Bitmap.Height div FrameCount;
  246.     end;
  247.   end;
  248.   AdjustBounds;
  249. end;
  250. function TVrAnimate.GetImageRect(Index: Integer): TRect;
  251. begin
  252.   if Orientation = voHorizontal then
  253.     Result := Bounds(Index * FImageWidth, 0, FImageWidth, FImageHeight)
  254.   else Result := Bounds(0, Index * FImageHeight, FImageWidth, FImageHeight);
  255. end;
  256. function TVrAnimate.DestRect: TRect;
  257. var
  258.   MidX, MidY: Integer;
  259. begin
  260.   if Stretch then Result := ClientRect
  261.   else
  262.   begin
  263.     MidX := (ClientWidth - FImageWidth) div 2;
  264.     MidY := (ClientHeight - FImageHeight) div 2;
  265.     Result := Bounds(MidX, MidY, FImageWidth, FImageHeight);
  266.   end;
  267. end;
  268. procedure TVrAnimate.Paint;
  269. begin
  270.   ClearBitmapCanvas;
  271.   if not Bitmap.Empty then
  272.     with BitmapCanvas do
  273.     begin
  274.       if Transparent then Brush.Style := bsClear
  275.       else Brush.Style := bsSolid;
  276.       BrushCopy(DestRect, Bitmap, GetImageRect(FCurrentFrame), Self.Color);
  277.     end;
  278.   if Designing then
  279.     with BitmapCanvas do
  280.     begin
  281.       Pen.Style := psDot;
  282.       Brush.Style := bsClear;
  283.       Rectangle(0, 0, Width, Height);
  284.     end;
  285.   inherited Paint;
  286. end;
  287. procedure TVrAnimate.SetCurrentFrame(Value: Integer);
  288. begin
  289.   if not Active then
  290.     if (FCurrentFrame <> Value) and (Value < FrameCount - 1) then
  291.     begin
  292.       FCurrentFrame := Value;
  293.       UpdateControlCanvas;
  294.     end;
  295. end;
  296. procedure TVrAnimate.TimerEvent(Sender: TObject);
  297. begin
  298.   if not Loading then
  299.   begin
  300.     if FCurrentFrame < FrameCount - 1 then Inc(FCurrentFrame)
  301.     else
  302.     begin
  303.       if not Loop then
  304.       begin
  305.         Active := false;
  306.         if Assigned(FOnNotify) then FOnNotify(Self);
  307.       end else FCurrentFrame := 0;
  308.     end;
  309.     UpdateControlCanvas;
  310.   end;
  311. end;
  312. end.