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

Delphi控件源码

开发平台:

Delphi

  1. {*****************************************************}
  2. {                                                     }
  3. {     Varian Component Workshop                       }
  4. {                                                     }
  5. {     Varian Software NL (c) 1996-2000                }
  6. {     All Rights Reserved                             }
  7. {                                                     }
  8. {*****************************************************}
  9. unit VrSlideShow;
  10. {$I VRLIB.INC}
  11. interface
  12. uses
  13.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  14.   VrTypes, VrClasses, VrControls, VrSysUtils, VrThreads, VrSystem;
  15. type
  16.   TVrTransitionEffect =
  17.    (StretchFromLeft, StretchFromRight, StretchFromTop,
  18.     StretchFromBottom, StretchFromTopLeft, StretchFromBottomRight,
  19.     StretchFromXcenter, StretchFromYcenter, PushFromBottom, PushFromLeft,
  20.     PushFromRight, PushFromTop, SlideFromLeft, SlideFromRight, SlideFromTop,
  21.     SlideFromBottom, SlideFromTopLeft, SlideFromBottomRight,Zoom);
  22.   TVrSlideShow = class(TVrGraphicImageControl)
  23.   private
  24.     FActive: Boolean;
  25.     FBitmapList: TVrBitmapList;
  26.     FBitmapListLink: TVrChangeLink;
  27.     FImage1: TBitmap;
  28.     FImage2: TBitmap;
  29.     FImageIndex1: Integer;
  30.     FImageIndex2: Integer;
  31.     FNewImage: Boolean;
  32. //  FImageOrg: TBitmap;
  33. //  FImageNew: TBitmap;
  34.     FCurrentStep: Integer;
  35.     FSteps: Integer;
  36.     FLoop: Boolean;
  37.     FSlideCount: Integer;
  38.     FTransition: TVrTransitionEffect;
  39.     FAnimateInit: Boolean;
  40.     FTimer: TVrTimer;
  41.     FThreaded: Boolean;
  42.     FOnNotify: TNotifyEvent;
  43.     FOnNextSlide: TNotifyEvent;
  44.     sglGrowX, sglGrowY: Double;
  45.     function GetInterval: Integer;
  46.     procedure SetActive(Value: Boolean);
  47.     procedure SetInterval(Value: Integer);
  48.     procedure SetSteps(Value: Integer);
  49. //  procedure SetTransition(Value: TVrTransitionEffect);
  50.     procedure SetThreaded(Value: Boolean);
  51.     procedure SetImageIndex1(Value: Integer);
  52.     procedure SetImageIndex2(Value: Integer);
  53.     procedure SetBitmapList(Value: TVrBitmapList);
  54.     procedure TimerEvent(Sender: TObject);
  55.     procedure BitmapListChanged(Sender: TObject);
  56.   protected
  57.     procedure CalcViewParams;
  58.     procedure Paint; override;
  59.     procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  60.     procedure Step;
  61.     procedure Stop;
  62.     procedure Next;
  63.     function GetBitmap(Index: Integer): TBitmap;
  64. //  procedure ExchangeImages;
  65.   public
  66.     destructor Destroy; override;
  67.     constructor Create(AOwner: TComponent);override;
  68.   published
  69.     property Threaded: Boolean read FThreaded write SetThreaded default True;
  70.     property Interval: integer read GetInterval write SetInterval;
  71.     property BitmapList: TVrBitmapList read FBitmapList write SetBitmapList;
  72.     property ImageIndex1: Integer read FImageIndex1 write SetImageIndex1 default -1;
  73.     property ImageIndex2: Integer read FImageIndex2 write SetImageIndex2 default -1;
  74. //  property ImageOrg: TBitmap read FImageOrg write SetImageOrg;
  75. //  property ImageNew: TBitmap read FImageNew write SetImageNew;
  76.     property Steps: integer read FSteps write SetSteps default 10;
  77.     property Transition: TVrTransitionEffect read FTransition write FTransition;
  78.     property Loop: Boolean read FLoop write FLoop default True;
  79.     property Active: Boolean read FActive write SetActive default false;
  80.     property OnNotify: TNotifyEvent read FOnNotify Write FOnNotify;
  81.     property OnNextSlide: TNotifyEvent read FOnNextSlide Write FOnNextSlide;
  82. {$IFDEF VER110}
  83.     property Anchors;
  84.     property Constraints;
  85. {$ENDIF}
  86.     property Align;
  87.     property DragCursor;
  88. {$IFDEF VER110}
  89.     property DragKind;
  90. {$ENDIF}
  91.     property DragMode;
  92.     property Hint;
  93.     property ParentShowHint;
  94.     property PopupMenu;
  95.     property ShowHint;
  96.     property Visible;
  97.     property OnClick;
  98. {$IFDEF VER130}
  99.     property OnContextPopup;
  100. {$ENDIF}
  101.     property OnDblClick;
  102.     property OnDragDrop;
  103.     property OnDragOver;
  104. {$IFDEF VER110}
  105.     property OnEndDock;
  106. {$ENDIF}
  107.     property OnEndDrag;
  108.     property OnMouseDown;
  109.     property OnMouseMove;
  110.     property OnMouseUp;
  111. {$IFDEF VER110}
  112.     property OnStartDock;
  113. {$ENDIF}
  114.     property OnStartDrag;
  115.   end;
  116. implementation
  117. constructor TVrSlideShow.Create(AOwner: TComponent);
  118. begin
  119.   inherited Create(AOwner);
  120.   ControlStyle := ControlStyle + [csOpaque];
  121.   Width := 110;
  122.   Height := 110;
  123.   FActive := false;
  124.   FSteps := 10;
  125.   FLoop := True;
  126.   FNewImage := True;
  127.   FImageIndex1 := -1;
  128.   FImageIndex2 := -1;
  129.   FSlideCount := 0;
  130.   FBitmapListLink := TVrChangeLink.Create;
  131.   FBitmapListLink.OnChange := BitmapListChanged;
  132.   FThreaded := True;
  133.   FTimer := TVrTimer.Create(self);
  134.   FTimer.Enabled := False;
  135.   FTimer.OnTimer := TimerEvent;
  136.   FTimer.Interval := 100;
  137. end;
  138. destructor TVrSlideShow.Destroy;
  139. begin
  140.   FTimer.Free;
  141.   FBitmapListLink.Free;
  142.   inherited Destroy;
  143. end;
  144. procedure TVrSlideShow.Notification(AComponent: TComponent;
  145.   Operation: TOperation);
  146. begin
  147.   inherited Notification(AComponent, Operation);
  148.   if (Operation = opRemove) then
  149.     if AComponent = BitmapList then BitmapList := nil;
  150. end;
  151. function TVrSlideShow.GetBitmap(Index: Integer): TBitmap;
  152. begin
  153.   Result := nil;
  154.   if Assigned(FBitmapList) then
  155.     Result := FBitmapList.GetBitmap(Index);
  156. end;
  157. procedure TVrSlideShow.BitmapListChanged(Sender: TObject);
  158. begin
  159.   UpdateControlCanvas;
  160. end;
  161. procedure TVrSlideShow.CalcViewParams;
  162. begin
  163.   sglGrowX := Width / FSteps;
  164.   sglGrowY := Height / FSteps;
  165.   FCurrentStep := 0;
  166.   FImage1 := GetBitmap(FImageIndex1);
  167.   FImage2 := GetBitmap(FImageIndex2);
  168. end;
  169. procedure TVrSlideShow.Paint;
  170. begin
  171.   CalcViewParams;
  172.   if FImage1 = nil then
  173.     ClearBitmapCanvas;
  174.   if (FImage1 <> nil) then
  175.   begin
  176.     BitmapCanvas.Brush.Style := bsSolid;
  177.     BitmapCanvas.CopyRect(ClientRect, FImage1.Canvas,
  178.       BitmapRect(FImage1));
  179.   end;
  180.   ShowDesignFrame(BitmapCanvas);
  181.   inherited Paint;
  182. end;
  183. procedure TVrSlideShow.Stop;
  184. begin
  185.   if not Designing then
  186.     Active := false;
  187. end;
  188. procedure TVrSlideShow.Next;
  189. begin
  190.   if FImageIndex1 < BitmapList.Bitmaps.Count - 1 then
  191.     Inc(FImageIndex1) else FImageIndex1 := 0;
  192.   if FImageIndex2 < BitmapList.Bitmaps.Count - 1 then
  193.    Inc(FImageIndex2) else FImageIndex2 := 0;
  194.   if Assigned(OnNextSlide) then
  195.   try
  196.     OnNextSlide(Self);
  197.   except
  198.     Application.HandleException(Self);
  199.   end;
  200. end;
  201. procedure TVrSlideShow.Step;
  202. var
  203.   IntLeft, IntRight, IntTop, IntBottom: Integer;
  204. begin
  205.   if (FImage1 = nil) or (FImage2 = nil) then
  206.   begin
  207.     Active := false;
  208.     raise EVrException.Create('Transition bitmap(s) not assigned.');
  209.   end;
  210.   IntRight := Width;
  211.   IntTop := 0;
  212.   IntBottom := Height;
  213.   case FTransition of
  214.    SlideFromLeft,
  215.    SlideFromTopLeft,
  216.    PushFromLeft: IntLeft := Trunc((sglGrowX * FCurrentStep) - Width);
  217.    StretchFromBottomRight,
  218.    StretchFromRight,
  219.    SlideFromRight,
  220.    SlideFromBottomRight,
  221.    PushFromRight: IntLeft := Trunc(Width - (sglGrowX * FCurrentStep));
  222.    Zoom,
  223.    StretchFromXcenter: IntLeft := Trunc((Width - (sglGrowX * FCurrentStep)) / 2);
  224.    else
  225.      IntLeft:=0;
  226.   end;
  227.   case FTransition of
  228.     SlideFromRight,
  229.     SlideFromBottomRight,
  230.     PushFromRight: IntRight := Trunc((Width * 2) - (sglGrowX * FCurrentStep));
  231.     StretchFromLeft,
  232.     StretchFromTopLeft,
  233.     SlideFromLeft,
  234.     SlideFromTopLeft,
  235.     PushFromLeft: IntRight := Trunc(sglGrowX * FCurrentStep);
  236.     Zoom,
  237.     StretchFromXcenter: IntRight := IntLeft + Trunc(sglGrowX * FCurrentStep);
  238.   end;
  239.   case FTransition of
  240.     SlideFromTop,
  241.     SlideFromTopLeft,
  242.     PushFromTop: IntTop := Trunc((sglGrowY * FCurrentStep) - Height);
  243.     StretchFromBottom,
  244.     StretchFromBottomRight,
  245.     SlideFromBottom,
  246.     SlideFromBottomRight,
  247.     PushFromBottom: IntTop := Trunc(Height - (sglGrowY * FCurrentStep));
  248.     Zoom,
  249.     StretchFromYcenter: IntTop := Trunc((Height - (sglGrowY * FCurrentStep)) / 2);
  250.   end;
  251.   case FTransition of
  252.     SlideFromBottom,
  253.     SlideFromBottomRight,
  254.     PushFromBottom: IntBottom := Trunc((Height * 2) - (sglGrowY * FCurrentStep));
  255.     StretchFromTop,
  256.     StretchFromTopLeft,
  257.     SlideFromTop,
  258.     SlideFromTopLeft,
  259.     PushFromTop: IntBottom := Trunc(sglGrowY * FCurrentStep);
  260.     Zoom,
  261.     StretchFromYcenter: IntBottom := IntTop + Trunc(sglGrowY * FCurrentStep);
  262.   end;
  263.   BitmapCanvas.CopyRect(Rect(IntLeft, IntTop, IntRight, IntBottom),
  264.     FImage2.Canvas, Rect(0, 0, FImage2.Width, FImage2.Height));
  265.   case FTransition of
  266.     PushFromBottom:
  267.       BitmapCanvas.CopyRect(Rect(0, IntTop - Height, Width, IntTop),
  268.         FImage1.Canvas, BitmapRect(FImage1));
  269.     PushFromLeft:
  270.       BitmapCanvas.CopyRect(Rect(IntRight, 0, IntRight + Width, Height),
  271.         FImage1.Canvas, BitmapRect(FImage1));
  272.     PushFromRight:
  273.       BitmapCanvas.CopyRect(Rect(IntLeft - Width, 0, IntLeft, Height),
  274.         FImage1.Canvas, BitmapRect(FImage1));
  275.     PushFromTop:
  276.       BitmapCanvas.CopyRect(Rect(0, IntBottom, Width, IntBottom + Height),
  277.         FImage1.Canvas, BitmapRect(FImage1));
  278.   end;
  279.   inherited Paint;
  280.   Inc(FCurrentStep);
  281.   if FCurrentStep > FSteps then
  282.   begin
  283.     FAnimateInit := True;
  284.     if Loop then Next
  285.     else
  286.     begin
  287.       if FSlideCount < BitmapList.Bitmaps.Count - 2 then
  288.       begin
  289.         Next;
  290.         Inc(FSlideCount);
  291.       end
  292.       else
  293.       begin
  294.         Next;
  295.         Active := false;
  296.         if Assigned(OnNotify) then OnNotify(Self);
  297.       end;
  298.     end;
  299.   end;
  300. end;
  301. procedure TVrSlideShow.TimerEvent(Sender: TObject);
  302. begin
  303.   if FAnimateInit then
  304.   begin
  305.     CalcViewParams;
  306.     FAnimateInit := false;
  307.   end else Step;
  308. end;
  309. procedure TVrSlideShow.SetActive(Value: Boolean);
  310. begin
  311.   if FActive <> Value then
  312.   begin
  313.     FActive := Value;
  314.     FSlideCount := 0;
  315.     if Designing then Exit;
  316.     FTimer.Enabled := Value;
  317.     if Value then FAnimateInit := True
  318.     else UpdateControlCanvas;
  319.   end;
  320. end;
  321. procedure TVrSlideShow.SetImageIndex1(Value: Integer);
  322. begin
  323.   if FImageIndex1 <> Value then
  324.   begin
  325.     FImageIndex1 := Value;
  326.     if not Active then
  327.       UpdateControlCanvas;
  328.   end;
  329. end;
  330. procedure TVrSlideShow.SetImageIndex2(Value: Integer);
  331. begin
  332.   if FImageIndex2 <> Value then
  333.   begin
  334.     FImageIndex2 := Value;
  335.     if not Active then
  336.       UpdateControlCanvas;
  337.   end;
  338. end;
  339. procedure TVrSlideShow.SetBitmapList(Value: TVrBitmapList);
  340. begin
  341.   if FBitmapList <> nil then
  342.     FBitmapList.RemoveLink(FBitmapListLink);
  343.   FBitmapList := Value;
  344.   if FBitmapList <> nil then
  345.     FBitmapList.InsertLink(FBitmapListLink);
  346.   if not Loading then Stop;
  347.   UpdateControlCanvas;
  348. end;
  349. procedure TVrSlideShow.SetSteps(Value: Integer);
  350. begin
  351.   if (Value > 0) and (Value < Height) and (Value < Width) then
  352.   begin
  353.     if not Loading then Stop;
  354.     FSteps := Value
  355.   end;
  356. end;
  357. function TVrSlideShow.GetInterval: Integer;
  358. begin
  359.   Result := FTimer.Interval;
  360. end;
  361. procedure TVrSlideShow.SetInterval(Value: Integer);
  362. begin
  363.   FTimer.Interval := Value;
  364. end;
  365. procedure TVrSlideShow.SetThreaded(Value: Boolean);
  366. begin
  367.   if FThreaded <> Value then
  368.   begin
  369.     FThreaded := Value;
  370.     if Value then FTimer.TimerType := ttThread
  371.     else FTimer.TimerType := ttSystem;
  372.   end;
  373. end;
  374. end.