VrScope.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 VrScope;
  10. {$I VRLIB.INC}
  11. interface
  12. uses
  13.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  14.   VrClasses, VrControls, VrSysUtils;
  15. type
  16.   TVrBaseOffsetInt = 0..100;
  17.   TVrScopeStyle = (ssLines, ssBars);
  18.   TVrScopePen = class(TPen)
  19.   public
  20.     constructor Create;
  21.   published
  22.     property Color default clYellow;
  23.   end;
  24.   TVrScope = class(TVrGraphicImageControl)
  25.   private
  26.     FMin: Integer;
  27.     FMax: Integer;
  28.     FBevel: TVrBevel;
  29.     FPen: TVrScopePen;
  30.     FGridSize: Integer;
  31.     FBaseOffset: TVrBaseOffsetInt;
  32.     FLineColor: TColor;
  33.     FLineWidth: Integer;
  34.     FBaseLineColor: TColor;
  35.     FFrequency: Integer;
  36.     FBufferSize: Integer;
  37.     FStyle: TVrScopeStyle;
  38.     BaseLineInt: Integer;
  39.     hStep: Integer;
  40.     ViewPort: TRect;
  41.     LineImage: TBitmap;
  42.     LineData: TVrIntList;
  43.     procedure SetMin(Value: Integer);
  44.     procedure SetMax(Value: Integer);
  45.     procedure SetGridSize(Value: Integer);
  46.     procedure SetBaseOffset(Value: TVrBaseOffsetInt);
  47.     procedure SetLineColor(Value: TColor);
  48.     procedure SetBaseLineColor(Value: TColor);
  49.     procedure SetLineWidth(Value: Integer);
  50.     procedure SetFrequency(Value: Integer);
  51.     procedure SetBufferSize(Value: Integer);
  52.     procedure SetPen(Value: TVrScopePen);
  53.     procedure SetBevel(Value: TVrBevel);
  54.     procedure SetStyle(Value: TVrScopeStyle);
  55.     procedure BevelChanged(Sender: TObject);
  56.     procedure PenChanged(Sender: TObject);
  57.   protected
  58.     procedure StepIt;
  59.     procedure PaintGrid;
  60.     procedure PaintLineGraph;
  61.     procedure Paint; override;
  62.   public
  63.     constructor Create(AOwner: TComponent); override;
  64.     destructor Destroy; override;
  65.     procedure Clear;
  66.     procedure AddValue(Value: Integer);
  67.   published
  68.     property Max: Integer read FMax write SetMax default 100;
  69.     property Min: Integer read FMin write SetMin default 0;
  70.     property Bevel: TVrBevel read FBevel write SetBevel;
  71.     property Pen: TVrScopePen read FPen write SetPen;
  72.     property GridSize: Integer read FGridSize write SetGridSize default 16;
  73.     property BaseOffset: TVrBaseOffsetInt read FBaseOffset write SetBaseOffset default 50;
  74.     property LineColor: TColor read FLineColor write SetLineColor default clGreen;
  75.     property BaseLineColor: TColor read FBaseLineColor write SetBaseLineColor default clLime;
  76.     property LineWidth: Integer read FLineWidth write SetLineWidth default 1;
  77.     property Frequency: Integer read FFrequency write SetFrequency default 1;
  78.     property BufferSize: Integer read FBufferSize write SetBufferSize default 999;
  79.     property Style: TVrScopeStyle read FStyle write SetStyle default ssLines;
  80.     property Align;
  81. {$IFDEF VER110}
  82.     property Anchors;
  83.     property DragKind;
  84. {$ENDIF}
  85.     property DragCursor;
  86.     property DragMode;
  87.     property Color default clBlack;
  88. {$IFDEF VER110}
  89.     property Constraints;
  90. {$ENDIF}
  91.     property ParentColor default false;
  92.     property ParentShowHint;
  93.     property PopupMenu;
  94.     property ShowHint;
  95.     property Visible;
  96.     property OnClick;
  97. {$IFDEF VER130}
  98.     property OnContextPopup;
  99. {$ENDIF}    
  100.     property OnDblClick;
  101.     property OnDragDrop;
  102.     property OnDragOver;
  103. {$IFDEF VER110}
  104.     property OnEndDock;
  105. {$ENDIF}
  106.     property OnEndDrag;
  107.     property OnMouseDown;
  108.     property OnMouseMove;
  109.     property OnMouseUp;
  110. {$IFDEF VER110}
  111.     property OnStartDock;
  112. {$ENDIF}
  113.     property OnStartDrag;
  114.  end;
  115. implementation
  116. { TVrScopePen }
  117. constructor TVrScopePen.Create;
  118. begin
  119.   inherited Create;
  120.   Color := clYellow;
  121. end;
  122. { TVrScope }
  123. constructor TVrScope.Create(AOwner: TComponent);
  124. begin
  125.   inherited Create(AOwner);
  126.   ControlStyle := ControlStyle + [csOpaque, csReplicatable];
  127.   Width := 249;
  128.   Height := 137;
  129.   Color := clBlack;
  130.   ParentColor := false;
  131.   FGridSize := 16;
  132.   FBaseOffset := 50;
  133.   FBaseLineColor := clLime;
  134.   FLineColor := clGreen;
  135.   FLineWidth := 1;
  136.   FFrequency := 1;
  137.   FStyle := ssLines;
  138.   FMin := 0;
  139.   FMax := 100;
  140.   FBufferSize := 999;
  141.   LineData := TVrIntList.Create;
  142.   FBevel := TVrBevel.Create;
  143.   with FBevel do
  144.   begin
  145.     InnerStyle := bsLowered;
  146.     InnerWidth := 2;
  147.     InnerColor := clBlack;
  148.     OnChange := BevelChanged;
  149.   end;
  150.   FPen := TVrScopePen.Create;
  151.   FPen.OnChange := PenChanged;
  152.   LineImage := TBitmap.Create;
  153. end;
  154. destructor TVrScope.Destroy;
  155. begin
  156.   FPen.Free;
  157.   FBevel.Free;
  158.   LineData.Free;
  159.   LineImage.Free;
  160.   inherited Destroy;
  161. end;
  162. procedure TVrScope.BevelChanged(Sender: TObject);
  163. begin
  164.   UpdateControlCanvas;
  165. end;
  166. procedure TVrScope.PenChanged(Sender: TObject);
  167. begin
  168.   UpdateControlCanvas;
  169. end;
  170. procedure TVrScope.SetGridSize(Value: Integer);
  171. begin
  172.   if (FGridSize <> Value) and (Value > 0) then
  173.   begin
  174.     FGridSize := Value;
  175.     UpdateControlCanvas;
  176.   end;
  177. end;
  178. procedure TVrScope.SetBaseOffset(Value: TVrBaseOffsetInt);
  179. begin
  180.   if FBaseOffset <> Value then
  181.   begin
  182.     FBaseOffset := Value;
  183.     UpdateControlCanvas;
  184.   end;
  185. end;
  186. procedure TVrScope.SetLineColor(Value: TColor);
  187. begin
  188.   if FLineColor <> Value then
  189.   begin
  190.     FLineColor := Value;
  191.     UpdateControlCanvas;
  192.   end;
  193. end;
  194. procedure TVrScope.SetLineWidth(Value: Integer);
  195. begin
  196.   if FLineWidth <> Value then
  197.   begin
  198.     FLineWidth := Value;
  199.     UpdateControlCanvas;
  200.   end;
  201. end;
  202. procedure TVrScope.SetBaseLineColor(Value: TColor);
  203. begin
  204.   if FBaseLineColor <> Value then
  205.   begin
  206.     FBaseLineColor := Value;
  207.     UpdateControlCanvas;
  208.   end;
  209. end;
  210. procedure TVrScope.SetFrequency(Value: Integer);
  211. begin
  212.   if (FFrequency <> Value) and (Value < FGridSize) then
  213.   begin
  214.     FFrequency := Value;
  215.     UpdateControlCanvas;
  216.   end;
  217. end;
  218. procedure TVrScope.SetMin(Value: Integer);
  219. begin
  220.   if (FMin <> Value) and (Value < FMax) then
  221.   begin
  222.     FMin := Value;
  223.     LineData.Clear;
  224.     UpdateControlCanvas;
  225.   end;
  226. end;
  227. procedure TVrScope.SetMax(Value: Integer);
  228. begin
  229.   if (FMax <> Value) and (Value > FMin) then
  230.   begin
  231.     FMax := Value;
  232.     LineData.Clear;
  233.     UpdateControlCanvas;
  234.   end;
  235. end;
  236. procedure TVrScope.SetStyle(Value: TVrScopeStyle);
  237. begin
  238.   if FStyle <> Value then
  239.   begin
  240.     FStyle := Value;
  241.     UpdateControlCanvas;
  242.   end;
  243. end;
  244. procedure TVrScope.SetBufferSize(Value: Integer);
  245. begin
  246.   if (FBufferSize <> Value) and (Value > 0) then
  247.   begin
  248.     FBufferSize := Value;
  249.     LineData.Clear;
  250.     UpdateControlCanvas;
  251.   end;
  252. end;
  253. procedure TVrScope.SetPen(Value: TVrScopePen);
  254. begin
  255.   FPen.Assign(Value);
  256. end;
  257. procedure TVrScope.SetBevel(Value: TVrBevel);
  258. begin
  259.   FBevel.Assign(Value);
  260. end;
  261. procedure TVrScope.StepIt;
  262. begin
  263.   Inc(hStep, FFrequency);
  264.   if hStep >= FGridSize then hStep := hStep - FGridSize;
  265. end;
  266. procedure TVrScope.Clear;
  267. begin
  268.   hStep := 0;
  269.   LineData.Clear;
  270.   UpdateControlCanvas;
  271. end;
  272. procedure TVrScope.AddValue(Value: Integer);
  273. begin
  274.   AdjustRange(Value, FMin, FMax);
  275.   LineData.Add(Value);
  276.   if LineData.Count - 1 > FBufferSize then LineData.Delete(0);
  277.   UpdateControlCanvas;
  278.   StepIt;
  279. end;
  280. procedure TVrScope.PaintLineGraph;
  281. var
  282.   X, I, BaseV, StepV: Integer;
  283. begin
  284.   with LineImage, LineImage.Canvas do
  285.   begin
  286.     Pen.Assign(FPen);
  287.     X := ViewPort.Left + WidthOf(ViewPort) + FFrequency;
  288.     I := LineData.Count - 1;
  289.     while (X >= 0) and (I >= 0) do
  290.     begin
  291.       BaseV := LineData.Items[I] - LineData.Items[I] * (FBaseOffset div 100);
  292.       StepV := (Height * (BaseV - FMin)) div (FMax - FMin);
  293.       LineTo(X, StepV);
  294.       Dec(I);
  295.       Dec(X, FFrequency);
  296.       if FStyle = ssBars then LineTo(X, StepV);
  297.     end;
  298.     LineTo(X, BaseLineInt);
  299.   end;
  300. end;
  301. procedure TVrScope.PaintGrid;
  302. var
  303.   X, Y: Integer;
  304. begin
  305.   with LineImage, LineImage.Canvas do
  306.   begin
  307.     Width := WidthOf(ViewPort);
  308.     Height := HeightOf(ViewPort);
  309.     BaseLineInt := Height - Round(Height / 100 * FBaseOffset);
  310.     Brush.Color := Self.Color;
  311.     FillRect(Bounds(0, 0, Width, Height));
  312.     Pen.Width := FLineWidth;
  313.     Pen.Color := FLineColor;
  314.     Pen.Mode := pmCopy;
  315.     Pen.Style := psSolid;
  316.     X := Width - hStep;
  317.     while X >= 0 do
  318.     begin
  319.       MoveTo(X, 0);
  320.       LineTo(X, Height);
  321.       Dec(X, FGridSize);
  322.     end;
  323.     Y := BaseLineInt;
  324.     while Y >= 0 do
  325.     begin
  326.       Dec(Y, FGridSize);
  327.       MoveTo(0, Y);
  328.       LineTo(Width, Y);
  329.     end;
  330.     Y := BaseLineInt;
  331.     while Y <= Height do
  332.     begin
  333.       Inc(Y, FGridSize);
  334.       MoveTo(0, Y);
  335.       LineTo(Width, Y);
  336.      end;
  337.      Pen.Color := FBaseLineColor;
  338.      MoveTo(0, BaseLineInt);
  339.      LineTo(Width, BaseLineInt);
  340.      PaintLineGraph;
  341.   end;
  342.   BitmapCanvas.Draw(ViewPort.Left, ViewPort.Top, LineImage);
  343. end;
  344. procedure TVrScope.Paint;
  345. begin
  346.   ClearBitmapCanvas;
  347.   ViewPort := ClientRect;
  348.   FBevel.Paint(BitmapCanvas, ViewPort);
  349.   PaintGrid;
  350.   inherited Paint;
  351. end;
  352. end.