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

Delphi控件源码

开发平台:

Delphi

  1. {*****************************************************}
  2. {                                                     }
  3. {     Varian Component Workshop                       }
  4. {                                                     }
  5. {     Varian Software NL (c) 1996-2000                }
  6. {     All Rights Reserved                             }
  7. {                                                     }
  8. {*****************************************************}
  9. unit VrBanner;
  10. {$I VRLIB.INC}
  11. interface
  12. uses
  13.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  14.   VrClasses, VrControls, VrSysUtils, VrThreads;
  15. type
  16.   TVrBannerPixelMode = (pmAuto, pmCustom);
  17.   TVrBannerDirection = (bdRightToLeft, bdLeftToRight,
  18.     bdTopToBottom, bdBottomToTop);
  19.   TVrBanner = class(TVrGraphicImageControl)
  20.   private
  21.     FBitmap: TBitmap;
  22.     FRaster: TBitmap;
  23.     FPixelSize: Integer;
  24.     FPixelColor: TColor;
  25.     FPixelMode: TVrBannerPixelMode;
  26.     FSpacing: Integer;
  27.     FIncrement: Integer;
  28.     FAutoScroll: Boolean;
  29.     FBevel: TVrBevel;
  30.     FDirection: TVrBannerDirection;
  31.     FThreaded: Boolean;
  32.     FOnScrollDone: TNotifyEvent;
  33.     FDstX, FDstY: Integer;
  34.     FScrollInit: Boolean;
  35.     Initialized: Boolean;
  36.     FTimer: TVrTimer;
  37.     function GetTimeInterval: Integer;
  38.     function GetPixelColor: TColor;
  39.     procedure SetBitmap(Value: TBitmap);
  40.     procedure SetPixelSize(Value: Integer);
  41.     procedure SetPixelColor(Value: TColor);
  42.     procedure SetSpacing(Value: Integer);
  43.     procedure SetTimeInterval(Value: Integer);
  44.     procedure SetAutoScroll(Value: Boolean);
  45.     procedure SetPixelMode(Value: TVrBannerPixelMode);
  46.     procedure SetBevel(Value: TVrBevel);
  47.     procedure SetThreaded(Value: Boolean);
  48.     procedure CreateRasterImage;
  49.     procedure TimerEvent(Sender: TObject);
  50.     procedure BevelChanged(Sender: TObject);
  51.     procedure CMColorChanged(var Message: TMessage); message CM_COLORCHANGED;
  52.   protected
  53.     procedure Reset;
  54.     procedure Paint; override;
  55.     procedure Loaded; override;
  56.     procedure Notify;
  57.     procedure BitmapChanged(Sender: TObject);
  58.     function StepSize: Integer;
  59.   public
  60.     constructor Create(AOwner: TComponent); override;
  61.     destructor Destroy; override;
  62.     procedure SetBounds(ALeft, ATop, AWidth, AHeight: Integer); override;
  63.   published
  64.     property Threaded: Boolean read FThreaded write SetThreaded default True;
  65.     property Bitmap: TBitmap read FBitmap write SetBitmap;
  66.     property PixelSize: Integer read FPixelSize write SetPixelSize default 2;
  67.     property PixelColor: TColor read FPixelColor write SetPixelColor default clGray;
  68.     property PixelMode: TVrBannerPixelMode read FPixelMode write SetPixelMode default pmAuto;
  69.     property Spacing: Integer read FSpacing write SetSpacing default 1;
  70.     property TimeInterval: Integer read GetTimeInterval write SetTimeInterval default 50;
  71.     property AutoScroll: Boolean read FAutoScroll write SetAutoScroll default false;
  72.     property Bevel: TVrBevel read FBevel write SetBevel;
  73.     property Direction: TVrBannerDirection read FDirection write FDirection default bdRightToLeft;
  74.     property OnScrollDone: TNotifyEvent read FOnScrollDone write FOnScrollDone;
  75. {$IFDEF VER110}
  76.     property Anchors;
  77.     property Constraints;
  78. {$ENDIF}
  79.     property Color default clBlack;
  80.     property DragCursor;
  81. {$IFDEF VER110}
  82.     property DragKind;
  83. {$ENDIF}
  84.     property DragMode;
  85.     property Hint;
  86.     property ParentColor default false;
  87.     property ParentShowHint;
  88.     property PopupMenu;
  89.     property ShowHint;
  90.     property Visible;
  91.     property OnClick;
  92. {$IFDEF VER130}
  93.     property OnContextPopup;
  94. {$ENDIF}    
  95.     property OnDblClick;
  96.     property OnDragDrop;
  97.     property OnDragOver;
  98. {$IFDEF VER110}
  99.     property OnEndDock;
  100. {$ENDIF}
  101.     property OnEndDrag;
  102.     property OnMouseDown;
  103.     property OnMouseMove;
  104.     property OnMouseUp;
  105. {$IFDEF VER110}
  106.     property OnStartDock;
  107. {$ENDIF}
  108.     property OnStartDrag;
  109.   end;
  110. implementation
  111. { TVrBanner }
  112. constructor TVrBanner.Create(AOwner: TComponent);
  113. begin
  114.   inherited Create(AOwner);
  115.   ControlStyle := ControlStyle + [csOpaque];
  116.   Width := 320;
  117.   Height := 40;
  118.   Color := clBlack;
  119.   ParentColor := false;
  120.   FPixelSize := 2;
  121.   FPixelColor := clGray;
  122.   FSpacing := 1;
  123.   FIncrement := 0;
  124.   FAutoScroll := false;
  125.   FPixelMode := pmAuto;
  126.   FDirection := bdRightToLeft;
  127.   FScrollInit := True;
  128.   FDstX := 0;
  129.   FDstY := 0;
  130.   FBitmap := TBitmap.Create;
  131.   FBitmap.OnChange := BitmapChanged;
  132.   FRaster := TBitmap.Create;
  133.   FBevel := TVrBevel.Create;
  134.   with FBevel do
  135.   begin
  136.     InnerStyle := bsLowered;
  137.     InnerWidth := 2;
  138.     InnerSpace := 0;
  139.     InnerColor := clBlack;
  140.     OnChange := BevelChanged;
  141.   end;
  142.   FThreaded := True;
  143.   FTimer := TVrTimer.Create(Self);
  144.   FTimer.Enabled := false;
  145.   FTimer.Interval := 50;
  146.   FTimer.OnTimer := TimerEvent;
  147. end;
  148. destructor TVrBanner.Destroy;
  149. begin
  150.   FBitmap.Free;
  151.   FRaster.Free;
  152.   FTimer.Free;
  153.   FBevel.Free;
  154.   inherited Destroy;
  155. end;
  156. procedure TVrBanner.Loaded;
  157. begin
  158.   inherited Loaded;
  159.   CreateRasterImage;
  160.   BoundsRect := Bounds(Left, Top, Width, Height);
  161. end;
  162. procedure TVrBanner.Reset;
  163. var
  164.   W, H, I: Integer;
  165. begin
  166.   I := StepSize;
  167.   W := (Width - FRaster.Width) div 2;
  168.   FDstX := MaxIntVal(0, (W div I) * I);
  169.   H := (Height - FRaster.Height) div 2;
  170.   FDstY := MaxIntVal(0, (H div I) * I);
  171. end;
  172. procedure TVrBanner.BevelChanged(Sender: TObject);
  173. begin
  174.   UpdateControlCanvas;
  175. end;
  176. procedure TVrBanner.CreateRasterImage;
  177. begin
  178.   if not FBitmap.Empty then
  179.     BitmapToLCD(FRaster, FBitmap, FPixelColor, Self.Color, FPixelSize, FSpacing)
  180.   else FRaster.Assign(nil);
  181. end;
  182. procedure TVrBanner.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
  183. var
  184.   Ticks: Integer;
  185. begin
  186.   if not Loading then
  187.   begin
  188.     Ticks := (AWidth + FSpacing) div StepSize;
  189.     AWidth := (Ticks * StepSize) - FSpacing;
  190.     Ticks := (AHeight + FSpacing) div StepSize;
  191.     AHeight := (Ticks * StepSize) - FSpacing;
  192.   end;
  193.   inherited SetBounds(ALeft, ATop, AWidth, AHeight);
  194.   if FRaster <> nil then Reset;
  195. end;
  196. procedure TVrBanner.Paint;
  197. var
  198.   R: TRect;
  199. begin
  200.   DrawRasterPattern(BitmapCanvas, ClientRect, GetPixelColor,
  201.     Self.Color, FPixelSize, FSpacing);
  202.   if not FRaster.Empty then
  203.     BitmapCanvas.Draw(FDstX, FDstY, FRaster);
  204.   R := ClientRect;
  205.   FBevel.Paint(BitmapCanvas, R);
  206.   inherited Paint;
  207.   //Make sure we first display the control
  208.   if (not Initialized) and (AutoScroll) then
  209.   begin
  210.     Initialized := True;
  211.     FScrollInit := True;
  212.     FTimer.Enabled := True;
  213.   end;
  214. end;
  215. procedure TVrBanner.SetBitmap(Value: TBitmap);
  216. begin
  217.   FBitmap.Assign(Value);
  218. end;
  219. procedure TVrBanner.SetBevel(Value: TVrBevel);
  220. begin
  221.   FBevel.Assign(Value);
  222. end;
  223. procedure TVrBanner.SetPixelSize(Value: Integer);
  224. begin
  225.   if (FPixelSize <> Value) and (Value > 0) then
  226.   begin
  227.     FPixelSize := Value;
  228.     BitmapChanged(Self);
  229.   end;
  230. end;
  231. procedure TVrBanner.SetPixelColor(Value: TColor);
  232. begin
  233.   if FPixelColor <> Value then
  234.   begin
  235.     FPixelColor := Value;
  236.     UpdateControlCanvas;
  237.   end;
  238. end;
  239. procedure TVrBanner.SetSpacing(Value: Integer);
  240. begin
  241.   if (FSpacing <> Value) and (Value > 0) then
  242.   begin
  243.     FSpacing := Value;
  244.     BitmapChanged(Self);
  245.   end;
  246. end;
  247. procedure TVrBanner.SetAutoScroll(Value: Boolean);
  248. begin
  249.   if FAutoScroll <> Value then
  250.   begin
  251.     FAutoScroll := Value;
  252.     Reset;
  253.     UpdateControlCanvas;
  254.     if not (Designing or Loading) then
  255.     begin
  256.       FScrollInit := True;
  257.       FTimer.Enabled := Value;
  258.     end;
  259.   end;
  260. end;
  261. function TVrBanner.GetTimeInterval: Integer;
  262. begin
  263.   Result := FTimer.Interval;
  264. end;
  265. procedure TVrBanner.SetTimeInterval(Value: Integer);
  266. begin
  267.   FTimer.Interval := Value;
  268. end;
  269. procedure TVrBanner.SetThreaded(Value: Boolean);
  270. begin
  271.   if FThreaded <> Value then
  272.   begin
  273.     FThreaded := Value;
  274.     if Value then FTimer.TimerType := ttThread
  275.     else FTimer.TimerType := ttSystem;
  276.   end;
  277. end;
  278. function TVrBanner.GetPixelColor: TColor;
  279. begin
  280.   Result := FPixelColor;
  281.   if FPixelMode = pmAuto then
  282.     if not FBitmap.Empty then Result := FRaster.Canvas.Pixels[0, 0];
  283. end;
  284. procedure TVrBanner.SetPixelMode(Value: TVrBannerPixelMode);
  285. begin
  286.   if FPixelMode <> Value then
  287.   begin
  288.     FPixelMode := Value;
  289.     UpdateControlCanvas;
  290.   end;
  291. end;
  292. procedure TVrBanner.CMColorChanged(var Message: TMessage);
  293. begin
  294.   inherited;
  295.   if not (csLoading in ComponentState) then
  296.     if FBitmap <> nil then CreateRasterImage;
  297. end;
  298. procedure TVrBanner.BitmapChanged(Sender: TObject);
  299. begin
  300.   if not (csLoading in ComponentState) then
  301.   begin
  302.     CreateRasterImage;
  303.     BoundsRect := Bounds(Left, Top, Width, Height);
  304.     if not AutoScroll then UpdateControlCanvas;
  305.   end;
  306. end;
  307. function TVrBanner.StepSize: Integer;
  308. begin
  309.   Result := FPixelSize + FSpacing;
  310.   if Result = 0 then Result := 1;
  311. end;
  312. procedure TVrBanner.Notify;
  313. begin
  314.   if Assigned(FOnScrollDone) then
  315.     FOnScrollDone(Self);
  316. end;
  317. procedure TVrBanner.TimerEvent(Sender: TObject);
  318. begin
  319.   if FScrollInit then
  320.   begin
  321.     Reset;
  322.     FScrollInit := false;
  323.   end;
  324.   case Direction of
  325.     bdRightToLeft:
  326.       begin
  327.         if FDstX + FRaster.Width > 0 then
  328.           Dec(FDstX, StepSize)
  329.         else
  330.         begin
  331.           Notify;
  332.           FDstX := ClientWidth - FPixelSize;
  333.         end;
  334.       end;
  335.     bdLeftToRight:
  336.       begin
  337.         if (FDstX < Width) then
  338.           Inc(FDstX, StepSize)
  339.         else
  340.         begin
  341.           Notify;
  342.           FDstX := -FRaster.Width;
  343.         end;
  344.       end;
  345.     bdTopToBottom:
  346.       begin
  347.         if (FDstY < Height) then
  348.           Inc(FDstY, StepSize)
  349.         else
  350.         begin
  351.           Notify;
  352.           FDstY := -FRaster.Height;
  353.         end;
  354.       end;
  355.     bdBottomToTop:
  356.       begin
  357.         if FDstY + FRaster.Height > 0 then
  358.           Dec(FDstY, StepSize)
  359.         else
  360.         begin
  361.           Notify;
  362.           FDstY := ClientHeight - FPixelSize;
  363.         end;
  364.       end;
  365.   end; //case
  366.   UpdateControlCanvas;
  367. end;
  368. end.