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

Delphi控件源码

开发平台:

Delphi

  1. {*****************************************************}
  2. {                                                     }
  3. {     Varian Component Workshop                       }
  4. {                                                     }
  5. {     Varian Software NL (c) 1996-2000                }
  6. {     All Rights Reserved                             }
  7. {                                                     }
  8. {*****************************************************}
  9. unit VrArrow;
  10. {$I VRLIB.INC}
  11. interface
  12. uses
  13.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Dialogs,
  14.   VrClasses, VrControls, VrSysUtils;
  15. type
  16.   TVrArrowDirection = (pdLeft, pdRight, pdUp, pdDown);
  17.   TVrArrow = class(TVrGraphicImageControl)
  18.   private
  19.     FDirection: TVrArrowDirection;
  20.     FPalette: TVrPalette;
  21.     FActive: Boolean;
  22.     FTrackMouse: Boolean;
  23.     Glyphs: TBitmap;
  24.     GlyphMask: TBitmap;
  25.     HasMouse: Boolean;
  26.     function ImageRect: TRect;
  27.     function InControl(X, Y: Integer): Boolean;
  28.     procedure SetActive(Value: Boolean);
  29.     procedure SetDirection(Value: TVrArrowDirection);
  30.     procedure SetPalette(Value: TVrPalette);
  31.     procedure PaletteModified(Sender: TObject);
  32.     procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
  33.   protected
  34.     procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
  35.     procedure LoadBitmaps; virtual;
  36.     procedure Paint; override;
  37.     function GetPalette: HPalette; override;
  38.   public
  39.     constructor Create(Aowner: TComponent); override;
  40.     destructor Destroy; override;
  41.   published
  42.     property Active: Boolean read FActive write SetActive default false;
  43.     property Direction: TVrArrowDirection read FDirection write SetDirection default pdUp;
  44.     property Palette: TVrPalette read FPalette write SetPalette;
  45.     property TrackMouse: Boolean read FTrackMouse write FTrackMouse default false;
  46.     property Transparent default false;
  47.     property Align;
  48. {$IFDEF VER110}
  49.     property Anchors;
  50.     property Constraints;
  51. {$ENDIF}
  52.     property Color default clBlack;
  53.     property DragCursor;
  54. {$IFDEF VER110}
  55.     property DragKind;
  56. {$ENDIF}
  57.     property DragMode;
  58.     property Hint;
  59.     property ParentColor default false;
  60.     property ParentShowHint;
  61.     property PopupMenu;
  62.     property ShowHint;
  63.     property Visible;
  64.     property OnClick;
  65. {$IFDEF VER130}
  66.     property OnContextPopup;
  67. {$ENDIF}
  68.     property OnDragDrop;
  69.     property OnDragOver;
  70. {$IFDEF VER110}
  71.     property OnEndDock;
  72. {$ENDIF}
  73.     property OnEndDrag;
  74.     property OnMouseDown;
  75.     property OnMouseMove;
  76.     property OnMouseUp;
  77. {$IFDEF VER110}
  78.     property OnStartDock;
  79. {$ENDIF}
  80.     property OnStartDrag;
  81.   end;
  82. implementation
  83. {$R VRARROW.D32}
  84. const
  85.   ResId: array[TVrArrowDirection] of PChar =
  86.     ('LEFTARROW', 'RIGHTARROW', 'UPARROW', 'DOWNARROW');
  87. {TVrArrow}
  88. constructor TVrArrow.Create(Aowner: TComponent);
  89. begin
  90.   inherited Create(Aowner);
  91.   ControlStyle := ControlStyle + [csOpaque] - [csDoubleClicks];
  92.   Width := 50;
  93.   Height := 45;
  94.   ParentColor := false;
  95.   Color := clBlack;
  96.   FActive := false;
  97.   FDirection := pdUp;
  98.   FPalette := TVrPalette.Create;
  99.   FPalette.OnChange := PaletteModified;
  100.   Glyphs := TBitmap.Create;
  101.   GlyphMask := TBitmap.Create;
  102.   HasMouse := false;
  103.   LoadBitmaps;
  104. end;
  105. destructor TVrArrow.Destroy;
  106. begin
  107.   FPalette.Free;
  108.   Glyphs.Free;
  109.   GlyphMask.Free;
  110.   inherited Destroy;
  111. end;
  112. procedure TVrArrow.LoadBitmaps;
  113. begin
  114.   Glyphs.Handle := LoadBitmap(hInstance, ResId[FDirection]);
  115.   GlyphMask.Assign(Glyphs);
  116.   GlyphMask.Mask(clWhite);
  117.   FPalette.ToBMP(Glyphs, clGreen, clLime);
  118. end;
  119. function TVrArrow.GetPalette: HPalette;
  120. begin
  121.   Result := Glyphs.Palette;
  122. end;
  123. procedure TVrArrow.SetActive(Value: Boolean);
  124. begin
  125.   if FActive <> Value then
  126.   begin
  127.     FActive := Value;
  128.     UpdateControlCanvas;
  129.   end;
  130. end;
  131. procedure TVrArrow.SetDirection(Value: TVrArrowDirection);
  132. begin
  133.   if FDirection <> Value then
  134.   begin
  135.     FDirection := Value;
  136.     LoadBitmaps;
  137.     UpdateControlCanvas;
  138.   end;
  139. end;
  140. procedure TVrArrow.PaletteModified(Sender: TObject);
  141. begin
  142.   LoadBitmaps;
  143.   UpdateControlCanvas;
  144. end;
  145. procedure TVrArrow.SetPalette(Value: TVrPalette);
  146. begin
  147.   FPalette.Assign(Value);
  148. end;
  149. procedure TVrArrow.Paint;
  150. var
  151.   GlyphRect: TRect;
  152.   Index, W, H: Integer;
  153. begin
  154.   ClearBitmapCanvas;
  155.   if FTrackMouse then Active := HasMouse;
  156.   Index := Ord(FActive);
  157.   with BitmapCanvas do
  158.   begin
  159.     Brush.Style := bsClear;
  160.     W := Glyphs.Width div 2;
  161.     H := Glyphs.Height;
  162.     GlyphRect := Bounds(Index * W, 0, W, H);
  163.     BrushCopy(ImageRect, Glyphs, GlyphRect, clWhite);
  164.   end;
  165.   ShowDesignFrame(BitmapCanvas);
  166.   inherited Paint;
  167. end;
  168. function TVrArrow.ImageRect: TRect;
  169. var
  170.   X, Y, W, H: Integer;
  171. begin
  172.   W := Glyphs.Width div 2;
  173.   H := Glyphs.Height;
  174.   X := (Width - W) div 2;
  175.   Y := (Height - W) div 2;
  176.   Result := Bounds(X, Y, W, H);
  177. end;
  178. function TVrArrow.InControl(X, Y: Integer): Boolean;
  179. var
  180.   R: TRect;
  181.   Px, Py: Integer;
  182. begin
  183.   R := ImageRect;
  184.   Px := R.Right - X - 1;
  185.   Py := R.Bottom - Y - 1;
  186.   Result := (PtInRect(R, Point(X, Y))) and
  187.             (GlyphMask.Canvas.Pixels[Px, Py] = clBlack);
  188. end;
  189. procedure TVrArrow.MouseMove(Shift: TShiftState; X, Y: Integer);
  190. begin
  191.   inherited;
  192.   if FTrackMouse then
  193.   begin
  194.     HasMouse := InControl(X, Y);
  195.     UpdateControlCanvas;
  196.   end;
  197. end;
  198. procedure TVrArrow.CMMouseLeave(var Message: TMessage);
  199. begin
  200.   inherited;
  201.   if TrackMouse then
  202.   begin
  203.     HasMouse := false;
  204.     UpdateControlCanvas;
  205.   end;
  206. end;
  207. end.