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

Delphi控件源码

开发平台:

Delphi

  1. {*****************************************************}
  2. {                                                     }
  3. {     Varian Component Workshop                       }
  4. {                                                     }
  5. {     Varian Software NL (c) 1996-2000                }
  6. {     All Rights Reserved                             }
  7. {                                                     }
  8. {*****************************************************}
  9. unit VrCompass;
  10. {$I VRLIB.INC}
  11. interface
  12. uses
  13.   Classes, Controls, Windows, Graphics, SysUtils, Messages,
  14.   VrControls, VrSysUtils;
  15. type
  16.   TVrCompass = class(TVrGraphicImageControl)
  17.   private
  18.     FBackImage: TBitmap;
  19.     FAutoSize: Boolean;
  20.     FHeading: Integer;
  21.     FNeedleLength: Integer;
  22.     FNeedleWidth: Integer;
  23.     FNeedleColor: TColor;
  24.     FNeedleTransparent: Boolean;
  25.     FCircleColor: TColor;
  26.     FCircleWidth: Integer;
  27.     FCircleOutlineColor: TColor;
  28.     FCircleOutlineWidth: Integer;
  29.     FOnChange: TNotifyEvent;
  30.     procedure SetHeading(Value: Integer);
  31.     procedure SetNeedleColor(Value: TColor);
  32.     procedure SetCircleWidth(Value: Integer);
  33.     procedure SetCircleOutlineColor(Value: TColor);
  34.     procedure SetNeedleLength(Value: Integer);
  35.     procedure SetNeedleWidth(Value: Integer);
  36.     procedure SetCircleOutlineWidth (Value: Integer);
  37.     procedure SetCircleColor(Value: TColor);
  38.     procedure SetBackImage(Value: TBitmap);
  39.     procedure SetAutoSize(Value: Boolean);
  40.     procedure SetNeedleTransparent(Value: Boolean);
  41.     procedure BackImageChanged(Sender: TObject);
  42.   protected
  43.     procedure Paint; override;
  44.     procedure AdjustClientRect;
  45.     function GetPalette: HPALETTE; override;
  46.     procedure Changed; virtual;
  47.  public
  48.     constructor Create(AOwner : Tcomponent);override;
  49.     destructor Destroy; override;
  50.     procedure SetBounds(ALeft, ATop, AWidth, AHeight: Integer); override;
  51.   published
  52.     property Heading: Integer read fHeading write SetHeading;
  53.     property NeedleLength: Integer read fNeedleLength write SetNeedleLength default 40;
  54.     property NeedleWidth: Integer read FNeedleWidth write SetNeedleWidth;
  55.     property CircleOutlineWidth: Integer read FCircleOutlineWidth write SetCircleOutlineWidth;
  56.     property CircleWidth: Integer read FCircleWidth write SetCircleWidth default 8;
  57.     property CircleOutlineColor: TColor read FCircleOutlineColor write SetCircleOutlineColor default clBlue;
  58.     property NeedleColor: TColor read FNeedleColor write SetNeedleColor default clRed;
  59.     property CircleColor: TColor read FCircleColor write SetCircleColor default clNavy;
  60.     property BackImage: TBitmap read FBackImage write SetBackImage;
  61.     property AutoSize: Boolean read FAutoSize write SetAutoSize default True;
  62.     property NeedleTransparent: Boolean read FNeedleTransparent write SetNeedleTransparent default false;
  63.     property OnChange: TNotifyEvent read FOnChange write FOnChange;
  64.     property Transparent default false;
  65.     property Align;
  66. {$IFDEF VER110}
  67.     property Anchors;
  68.     property Constraints;
  69. {$ENDIF}
  70.     property Color default clBlack;
  71.     property DragCursor;
  72. {$IFDEF VER110}
  73.     property DragKind;
  74. {$ENDIF}
  75.     property DragMode;
  76.     property Hint;
  77.     property ParentColor default false;
  78.     property ParentShowHint;
  79.     property PopupMenu;
  80.     property ShowHint;
  81.     property Visible;
  82.     property OnClick;
  83. {$IFDEF VER130}
  84.     property OnContextPopup;
  85. {$ENDIF}
  86.     property OnDblClick;
  87.     property OnDragDrop;
  88.     property OnDragOver;
  89. {$IFDEF VER110}
  90.     property OnEndDock;
  91. {$ENDIF}
  92.     property OnEndDrag;
  93.     property OnMouseDown;
  94.     property OnMouseMove;
  95.     property OnMouseUp;
  96. {$IFDEF VER110}
  97.     property OnStartDock;
  98. {$ENDIF}
  99.     property OnStartDrag;
  100.   end;
  101. implementation
  102. function DegToRad(Degrees: Extended): Extended;
  103. begin
  104.   Result := Degrees * (PI / 180);
  105. end;
  106. { TVrCompass }
  107. constructor TVrCompass.Create(AOwner: TComponent);
  108. begin
  109.   inherited Create(AOwner);
  110.   ControlStyle := ControlStyle + [csOpaque, csReplicatable, csSetCaption];
  111.   Width := 100;
  112.   Height := 100;
  113.   Color := clBlack;
  114.   ParentColor := false;
  115.   Transparent := false;
  116.   FAutoSize := True;
  117.   FHeading := 90;
  118.   FNeedleLength := 40;
  119.   FNeedleWidth := 2;
  120.   FNeedleColor := clRed;
  121.   FNeedleTransparent := false;
  122.   FCircleWidth := 8;
  123.   FCircleOutlineWidth := 1;
  124.   FCircleOutlineColor := clBlue;
  125.   FCircleColor := clNavy;
  126.   FBackImage := TBitmap.Create;
  127.   FBackImage.OnChange := BackImageChanged;
  128. end;
  129. destructor TVrCompass.Destroy;
  130. begin
  131.   FBackImage.Free;
  132.   inherited Destroy;
  133. end;
  134. procedure TVrCompass.Changed;
  135. begin
  136.   if Assigned(FOnChange) then FOnChange(Self);
  137. end;
  138. procedure TVrCompass.Paint;
  139. const
  140.   PenMode: array[Boolean] of TPenMode = (pmCOPY, pmXOR);
  141. var
  142.   Center: TPoint;
  143.   R: TRect;
  144.   X, Y, Radius: Integer;
  145. begin
  146.   ClearBitmapCanvas;
  147.   Center.X := ClientWidth div 2;
  148.   Center.Y := ClientHeight div 2;
  149.   Radius := FCircleWidth;
  150.   DrawBitmap(BitmapCanvas, ClientRect, BackImage,
  151.     BitmapRect(BackImage),  Transparent, Self.Color);
  152.   with BitmapCanvas do
  153.   begin
  154.     R := Rect(Center.X - Radius, Center.Y - Radius,
  155.       Center.X + Radius, Center.Y + Radius);
  156.     Pen.Mode := pmCOPY;
  157.     Pen.Width := FCircleOutlineWidth;
  158.     Pen.Color := FCircleOutlineColor;
  159.     Brush.Color := FCircleColor;
  160.     Brush.Style := bsSolid;
  161.     Ellipse(R.Left, R.Top, R.Right, R.Bottom);
  162.     Pen.Mode := PenMode[NeedleTransparent];
  163.     Pen.Width:= FNeedleWidth;
  164.     Pen.Color := FNeedleColor;
  165.     X := Center.X + Trunc(Sin(DegToRad(FHeading)) * FNeedleLength);
  166.     Y := Center.Y + Trunc(Cos(DegToRad(FHeading)) * FNeedleLength);
  167.     MoveTo(Center.X, Center.Y);
  168.     LineTo(X, (Center.Y * 2) - Y);
  169.   end;
  170.   inherited Paint;
  171. end;
  172. procedure TVrCompass.AdjustClientRect;
  173. begin
  174.   if not FBackImage.Empty then
  175.   begin
  176.     Width := FBackImage.Width;
  177.     Height := FBackImage.Height;
  178.   end;
  179. end;
  180. procedure TVrCompass.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
  181. begin
  182.   if AutoSize then
  183.     if not FBackImage.Empty then
  184.     begin
  185.       AWidth := FBackImage.Width;
  186.       AHeight := FBackImage.Height;
  187.     end;
  188.   inherited SetBounds(ALeft, ATop, AWidth, AHeight);
  189. end;
  190. function TVrCompass.GetPalette: HPALETTE;
  191. begin
  192.   Result := BackImage.Palette;
  193. end;
  194. procedure TVrCompass.SetHeading (Value: Integer);
  195. begin
  196.    if FHeading <> value then
  197.    begin
  198.      FHeading := Value;
  199.      UpdateControlCanvas;
  200.      Changed;
  201.    end;
  202. end;
  203. procedure TVrCompass.SetAutoSize(Value: Boolean);
  204. begin
  205.   if FAutoSize <> Value then
  206.   begin
  207.     FAutoSize := Value;
  208.     AdjustClientRect;
  209.     UpdateControlCanvas;
  210.   end;
  211. end;
  212. procedure TVrCompass.SetNeedleLength (Value: Integer);
  213. begin
  214.   if FNeedleLength <> value then
  215.   begin
  216.     FNeedleLength := Value;
  217.     UpdateControlCanvas;
  218.   end;
  219. end;
  220. procedure TVrCompass.SetNeedleWidth(Value: Integer);
  221. begin
  222.   if NeedleWidth <> Value then
  223.   begin
  224.     FNeedleWidth := Value;
  225.     UpdateControlCanvas;
  226.   end;
  227. end;
  228. procedure TVrCompass.SetCircleOutlineWidth(Value: Integer);
  229. begin
  230.    if FCircleOutlineWidth <> value then
  231.    begin
  232.      FCircleOutlineWidth := Value;
  233.      UpdateControlCanvas;
  234.    end;
  235. end;
  236. procedure TVrCompass.SetCircleWidth(Value: Integer);
  237. begin
  238.   if FCircleWidth <> Value then
  239.   begin
  240.     FCircleWidth := Value;
  241.     UpdateControlCanvas;
  242.   end;
  243. end;
  244. procedure TVrCompass.SetNeedleColor(Value: TColor);
  245. begin
  246.   if FNeedleColor <> Value then
  247.   begin
  248.     FNeedleColor := Value;
  249.     UpdateControlCanvas;
  250.   end;
  251. end;
  252. procedure TVrCompass.SetCircleOutlineColor(Value: TColor);
  253. begin
  254.   if FCircleOutlineColor <> Value then
  255.   begin
  256.     FCircleOutlineColor := Value;
  257.     UpdateControlCanvas;
  258.   end;
  259. end;
  260. procedure TVrCompass.SetCircleColor(Value: TColor);
  261. begin
  262.   if FCircleColor <> Value then
  263.   begin
  264.     FCircleColor := Value;
  265.     UpdateControlCanvas;
  266.   end;
  267. end;
  268. procedure TVrCompass.SetNeedleTransparent(Value: Boolean);
  269. begin
  270.   if FNeedleTransparent <> Value then
  271.   begin
  272.     FNeedleTransparent := Value;
  273.     UpdateControlCanvas;
  274.   end;
  275. end;
  276. procedure TVrCompass.SetBackImage(Value: TBitmap);
  277. begin
  278.   FBackImage.Assign(Value);
  279. end;
  280. procedure TVrCompass.BackImageChanged(Sender: TObject);
  281. begin
  282.   if AutoSize then AdjustClientRect;
  283.   UpdateControlCanvas;
  284. end;
  285. end.