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

Delphi控件源码

开发平台:

Delphi

  1. {*****************************************************}
  2. {                                                     }
  3. {     Varian Component Workshop                       }
  4. {                                                     }
  5. {     Varian Software NL (c) 1996-2000                }
  6. {     All Rights Reserved                             }
  7. {                                                     }
  8. {*****************************************************}
  9. unit VrGauge;
  10. {$I VRLIB.INC}
  11. interface
  12. uses
  13.   Windows, Forms, Messages, SysUtils, Classes, Graphics, Controls, Dialogs,
  14.   VrTypes, VrClasses, VrControls, VrSysUtils;
  15. type
  16.   TVrGaugeImages = array[0..1] of TBitmap;
  17.   TVrGauge = class(TVrGraphicImageControl)
  18.   private
  19.     FMaxValue: Integer;
  20.     FMinValue: Integer;
  21.     FPosition: Integer;
  22.     FOrientation: TVrOrientation;
  23.     FPalette: TVrPalette;
  24.     FTickHeight: Integer;
  25.     FSpacing: Integer;
  26.     FSolidFill: Boolean;
  27.     FBevel: TVrBevel;
  28.     FStyle: TVrProgressStyle;
  29.     FActiveClick: Boolean;
  30.     FStep: Integer;
  31.     FOnChange: TNotifyEvent;
  32.     FOnMinValue: TNotifyEvent;
  33.     FOnMaxValue: TNotifyEvent;
  34.     FViewPort: TRect;
  35.     TickStep, Ticks: Integer;
  36.     FImages: TVrGaugeImages;
  37.     OrgSize: TPoint;
  38.     function GetPercentDone: Longint;
  39.     procedure SetMaxValue(Value: Integer);
  40.     procedure SetMinValue(Value: Integer);
  41.     procedure SetPosition(Value: Integer);
  42.     procedure SetOrientation(Value: TVrOrientation);
  43.     procedure SetTickHeight(Value: Integer);
  44.     procedure SetSpacing(Value: Integer);
  45.     procedure SetSolidFill(Value: Boolean);
  46.     procedure SetStyle(Value: TVrProgressStyle);
  47.     procedure SetPalette(Value: TVrPalette);
  48.     procedure SetBevel(Value: TVrBevel);
  49.     procedure DrawHori;
  50.     procedure DrawVert;
  51.     procedure PaletteModified(Sender: TObject);
  52.     procedure BevelChanged(Sender: TObject);
  53.   protected
  54.     procedure CreateBitmaps;
  55.     procedure DestroyBitmaps;
  56.     procedure CalcPaintParams;
  57.     procedure Paint; override;
  58.     procedure Change; dynamic;
  59.     procedure MoveTo(X, Y: Integer);
  60.     procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
  61.   public
  62.     constructor Create(AOwner: TComponent); override;
  63.     destructor Destroy; override;
  64.     procedure StepIt;
  65.     procedure StepBy(Delta: Integer);
  66.     property PercentDone: Longint read GetPercentDone;
  67.   published
  68.     property MaxValue: Integer read FMaxValue write SetMaxValue default 100;
  69.     property MinValue: Integer read FMinValue write SetMinValue default 0;
  70.     property Position: Integer read FPosition write SetPosition default 0;
  71.     property Palette: TVrPalette read FPalette write SetPalette;
  72.     property Bevel: TVrBevel read FBevel write SetBevel;
  73.     property Orientation: TVrOrientation read FOrientation write SetOrientation default voVertical;
  74.     property TickHeight: Integer read FTickHeight write SetTickHeight default 1;
  75.     property Spacing: Integer read FSpacing write SetSpacing default 1;
  76.     property SolidFill: Boolean read FSolidFill write SetSolidFill default false;
  77.     property Style: TVrProgressStyle read FStyle write SetStyle default psBottomLeft;
  78.     property ActiveClick: Boolean read FActiveClick write FActiveClick default false;
  79.     property Step: Integer read FStep write FStep default 10;
  80.     property OnChange: TNotifyEvent read FOnChange write FOnChange;
  81.     property OnMinValue: TNotifyEvent read FOnMinValue write FOnMinValue;
  82.     property OnMaxValue: TNotifyEvent read FOnMaxValue write FOnMaxValue;
  83. {$IFDEF VER110}
  84.     property Anchors;
  85.     property Constraints;
  86. {$ENDIF}
  87.     property Color default clBlack;
  88.     property DragCursor;
  89. {$IFDEF VER110}
  90.     property DragKind;
  91. {$ENDIF}
  92.     property DragMode;
  93.     property ParentColor default false;
  94.     property ParentShowHint;
  95.     property PopupMenu;
  96.     property ShowHint;
  97.     property Visible;
  98.     property OnClick;
  99. {$IFDEF VER130}
  100.     property OnContextPopup;
  101. {$ENDIF}    
  102.     property OnDblClick;
  103.     property OnDragDrop;
  104.     property OnDragOver;
  105. {$IFDEF VER110}
  106.     property OnEndDock;
  107. {$ENDIF}
  108.     property OnEndDrag;
  109.     property OnMouseDown;
  110.     property OnMouseMove;
  111.     property OnMouseUp;
  112. {$IFDEF VER110}
  113.     property OnStartDock;
  114. {$ENDIF}
  115.     property OnStartDrag;
  116.   end;
  117. implementation
  118. { TVrGauge }
  119. constructor TVrGauge.Create(AOwner: TComponent);
  120. begin
  121.   inherited Create(AOwner);
  122.   ControlStyle := ControlStyle + [csOpaque, csReplicatable];
  123.   Width := 30;
  124.   Height := 170;
  125.   ParentColor := false;
  126.   Color := clBlack;
  127.   FMaxValue := 100;
  128.   FMinValue := 0;
  129.   FPosition := 0;
  130.   FOrientation := voVertical;
  131.   FTickHeight := 1;
  132.   FSpacing := 1;
  133.   FSolidFill := false;
  134.   FStyle := psBottomLeft;
  135.   FActiveClick := false;
  136.   FStep := 10;
  137.   FPalette := TVrPalette.Create;
  138.   FPalette.OnChange := PaletteModified;
  139.   FBevel := TVrBevel.Create;
  140.   with FBevel do
  141.   begin
  142.     InnerStyle := bsLowered;
  143.     InnerWidth := 2;
  144.     InnerColor := clBlack;
  145.     OnChange := BevelChanged;
  146.   end;
  147.   AllocateBitmaps(FImages);
  148. end;
  149. destructor TVrGauge.Destroy;
  150. begin
  151.   FBevel.Free;
  152.   DestroyBitmaps;
  153.   inherited Destroy;
  154. end;
  155. procedure TVrGauge.CreateBitmaps;
  156. var
  157.   R: TRect;
  158. begin
  159.   R := ClientRect;
  160.   FBevel.GetVisibleArea(R);
  161.   with FImages[0] do
  162.   begin
  163.     case FOrientation of
  164.       voVertical:
  165.         begin
  166.           Height := FTickHeight;
  167.           Width := WidthOf(R);
  168.         end;
  169.       voHorizontal:
  170.         begin
  171.           Height := HeightOf(R);
  172.           Width := FTickHeight;
  173.         end;
  174.     end; //case
  175.     if (FTickHeight > 1) and (not FSolidFill) then
  176.       Canvas.Brush.BitMap := CreateDitherPattern(FPalette.Low, clBlack)
  177.     else
  178.       Canvas.Brush.Color := FPalette.Low;
  179.     try
  180.       R := Bounds(0, 0, Width, Height);
  181.       Canvas.FillRect(R);
  182.     finally
  183.       if Canvas.Brush.BitMap <> nil then
  184.       begin
  185.         Canvas.Brush.BitMap.Free;
  186.         Canvas.Brush.BitMap := nil;
  187.       end;
  188.     end;
  189.   end;
  190.   with FImages[1] do
  191.   begin
  192.     Width := FImages[0].Width;
  193.     Height := FImages[0].Height;
  194.     Canvas.Brush.Color := FPalette.High;
  195.     Canvas.FillRect(R);
  196.   end;
  197.   OrgSize.X := ClientWidth;
  198.   OrgSize.Y := ClientHeight;
  199. end;
  200. procedure TVrGauge.DestroyBitmaps;
  201. begin
  202.   DeAllocateBitmaps(FImages);
  203. end;
  204. procedure TVrGauge.PaletteModified(Sender: TObject);
  205. begin
  206.   CreateBitmaps;
  207.   UpdateControlCanvas;
  208. end;
  209. procedure TVrGauge.BevelChanged(Sender: TObject);
  210. var
  211.   R: TRect;
  212. begin
  213.   if not Loading then
  214.   begin
  215.     R := ClientRect;
  216.     FBevel.GetVisibleArea(R);
  217.     InflateRect(FViewPort, R.Left, R.Top);
  218.     BoundsRect := Bounds(Left, Top, WidthOf(FViewPort),
  219.       HeightOf(FViewPort));
  220.   end;
  221.   CreateBitmaps;
  222.   UpdateControlCanvas;
  223. end;
  224. procedure TVrGauge.SetMaxValue(Value: Integer);
  225. begin
  226.   if (FMaxValue <> Value) and (Value > FMinValue) then
  227.   begin
  228.     FMaxValue := Value;
  229.     if FPosition > FMaxValue then
  230.       Position := FMaxValue else UpdateControlCanvas;
  231.   end;
  232. end;
  233. procedure TVrGauge.SetMinValue(Value: Integer);
  234. begin
  235.   if (FMinValue <> Value) and (Value < FMaxValue) then
  236.   begin
  237.     FMinValue := Value;
  238.     if FPosition < FMinValue then
  239.       Position := FMinValue else UpdateControlCanvas;
  240.   end;
  241. end;
  242. procedure TVrGauge.SetPosition(Value: Integer);
  243. begin
  244.   if Value < FMinValue then Value := FMinValue;
  245.   if Value > FMaxValue then Value := FMaxValue;
  246.   if FPosition <> Value then
  247.   begin
  248.     FPosition := Value;
  249.     UpdateControlCanvas;
  250.     Change;
  251.   end;
  252. end;
  253. procedure TVrGauge.SetOrientation(Value: TVrOrientation);
  254. begin
  255.   if FOrientation <> Value then
  256.   begin
  257.     FOrientation := Value;
  258.     if not Loading then
  259.       BoundsRect := Bounds(Left, Top, Height, Width);
  260.     UpdateControlCanvas;
  261.   end;
  262. end;
  263. procedure TVrGauge.SetTickHeight(Value: Integer);
  264. begin
  265.   if (FTickHeight <> Value) and (Value > 0) then
  266.   begin
  267.     FTickHeight := Value;
  268.     CreateBitmaps;
  269.     UpdateControlCanvas;
  270.   end;
  271. end;
  272. procedure TVrGauge.SetSpacing(Value: Integer);
  273. begin
  274.   if FSpacing <> Value then
  275.   begin
  276.     FSpacing := Value;
  277.     UpdateControlCanvas;
  278.   end;
  279. end;
  280. procedure TVrGauge.SetSolidFill(Value: Boolean);
  281. begin
  282.   if FSolidFill <> Value then
  283.   begin
  284.     FSolidFill := Value;
  285.     CreateBitmaps;
  286.     UpdateControlCanvas;
  287.   end;
  288. end;
  289. procedure TVrGauge.SetStyle(Value: TVrProgressStyle);
  290. begin
  291.   if FStyle <> Value then
  292.   begin
  293.     FStyle := Value;
  294.     UpdateControlCanvas;
  295.   end;
  296. end;
  297. procedure TVrGauge.SetPalette(Value: TVrPalette);
  298. begin
  299.   FPalette.Assign(Value);
  300. end;
  301. procedure TVrGauge.SetBevel(Value: TVrBevel);
  302. begin
  303.   FBevel.Assign(Value);
  304. end;
  305. procedure TVrGauge.Change;
  306. begin
  307.   if Assigned(FOnChange) then FOnChange(self);
  308.   if (Position = MinValue) and (Assigned(FOnMinValue)) then FOnMinValue(Self);
  309.   if (Position = MaxValue) and (Assigned(FOnMaxValue)) then FOnMaxValue(Self);
  310. end;
  311. procedure TVrGauge.StepIt;
  312. begin
  313.   Position := Position + FStep;
  314. end;
  315. procedure TVrGauge.StepBy(Delta: Integer);
  316. begin
  317.   Position := Position + Delta;
  318. end;
  319. function TVrGauge.GetPercentDone: Longint;
  320. begin
  321.   Result := SolveForY(FPosition - FMinValue, FMaxValue - FMinValue);
  322. end;
  323. procedure TVrGauge.DrawHori;
  324. var
  325.   X, Y, I, Offset: Integer;
  326.   TicksOn, TicksOff: Integer;
  327. begin
  328.   TicksOn := SolveForX(PercentDone, Ticks);
  329.   TicksOff := Ticks - TicksOn;
  330.   Y := FViewPort.Top;
  331.   if FStyle = psBottomLeft then
  332.   begin
  333.     X := FViewPort.Left;
  334.     Offset := TickStep;
  335.   end
  336.   else
  337.   begin
  338.     X := FViewPort.Right - FTickHeight;
  339.     Offset := -TickStep;
  340.   end;
  341.   for I := 1 to TicksOn do
  342.   begin
  343.     BitmapCanvas.Draw(X, Y, FImages[1]);
  344.     Inc(X, Offset);
  345.   end;
  346.   for I := 1 to TicksOff do
  347.   begin
  348.     BitmapCanvas.Draw(X, Y, FImages[0]);
  349.     Inc(X, Offset);
  350.   end;
  351. end;
  352. procedure TVrGauge.DrawVert;
  353. var
  354.   X, Y, I, Offset: Integer;
  355.   TicksOn, TicksOff: Integer;
  356. begin
  357.   TicksOn := SolveForX(PercentDone, Ticks);
  358.   TicksOff := Ticks - TicksOn;
  359.   X := FViewPort.Left;
  360.   if FStyle = psBottomLeft then
  361.   begin
  362.     Y := FViewPort.Top;
  363.     Offset := TickStep;
  364.   end
  365.   else
  366.   begin
  367.     Y := FViewPort.Bottom - FTickHeight;
  368.     Offset := -TickStep;
  369.   end;
  370.   for I := 1 to TicksOff do
  371.   begin
  372.     BitmapCanvas.Draw(X, Y, FImages[0]);
  373.     Inc(Y, Offset);
  374.   end;
  375.   for I := 1 to TicksOn do
  376.   begin
  377.     BitmapCanvas.Draw(X, Y, FImages[1]);
  378.     Inc(Y, Offset);
  379.   end;
  380. end;
  381. procedure TVrGauge.Paint;
  382. var
  383.   R: TRect;
  384. begin
  385.   CalcPaintParams;
  386.   if (OrgSize.X <> ClientWidth) or
  387.    (OrgSize.Y <> ClientHeight) then CreateBitmaps;
  388.   ClearBitmapCanvas;
  389.   R := ClientRect;
  390.   FBevel.Paint(BitmapCanvas, R);
  391.   case FOrientation of
  392.     voVertical: DrawVert;
  393.     voHorizontal: DrawHori;
  394.   end;
  395.   inherited Paint;
  396. end;
  397. procedure TVrGauge.CalcPaintParams;
  398. begin
  399.   TickStep := FTickHeight + FSpacing;
  400.   FViewPort := ClientRect;
  401.   FBevel.GetVisibleArea(FViewPort);
  402.   case Orientation of
  403.     voVertical:
  404.       begin
  405.         Ticks := (HeightOf(FViewPort) + FSpacing) div TickStep;
  406.         Height := (FViewPort.Top * 2) + (Ticks * TickStep) - FSpacing;
  407.       end;
  408.     voHorizontal:
  409.       begin
  410.         Ticks := (WidthOf(FViewPort) + FSpacing) div TickStep;
  411.         Width := (FViewPort.Left * 2) + (Ticks * TickStep) - FSpacing;
  412.       end;
  413.   end;
  414. end;
  415. procedure TVrGauge.MoveTo(X, Y: Integer);
  416. var
  417.   Range: Double;
  418. begin
  419.   Range := FMaxValue - FMinValue;
  420.   case FOrientation of
  421.     voVertical:
  422.       begin
  423.         if FStyle = psBottomLeft then
  424.           Y := ClientHeight - Y - FTickHeight
  425.         else Y := Y - FTickHeight;
  426.         Position := Round(Y * Range / HeightOf(FViewPort))-1;
  427.       end;
  428.     voHorizontal:
  429.       begin
  430.         if FStyle = psBottomLeft then
  431.           X := X - FTickHeight
  432.         else X := ClientWidth - X - FTickHeight;
  433.         Position := Round(X * Range / WidthOf(FViewPort))-1;
  434.       end;
  435.   end;
  436. end;
  437. procedure TVrGauge.MouseDown(Button: TMouseButton; Shift: TShiftState;
  438.   X, Y: Integer);
  439. begin
  440.   inherited;
  441.   if (Button = mbLeft) and (FActiveClick) then
  442.     if PtInRect(FViewPort, Point(X, Y)) then MoveTo(X, Y);
  443. end;
  444. end.