VrUpDown.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 VrUpDown;
  10. {$I VRLIB.INC}
  11. interface
  12. uses
  13.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  14.   VrTypes, VrControls, VrSysUtils, VrThreads;
  15. type
  16.   TVrUpDown = class(TVrGraphicImageControl)
  17.   private
  18.     FGlyphsUp: TBitmap;
  19.     FGlyphsDown: TBitmap;
  20.     FNumGlyphs: TVrNumGlyphs;
  21.     FSizeUp: TPoint;
  22.     FSizeDown: TPoint;
  23.     FOrientation: TVrOrientation;
  24.     FRepeatClick: Boolean;
  25.     FRepeatPause: TVrMaxInt;
  26.     FOnUpClick: TNotifyEvent;
  27.     FOnDownClick: TNotifyEvent;
  28.     FFocusIndex: Integer;
  29.     FDown: Boolean;
  30.     FDownIndex: Integer;
  31.     FPressed: Boolean;
  32.     FRepeatTimer: TVrTimer;
  33.     procedure SetGlyphsUp(Value: TBitmap);
  34.     procedure SetGlyphsDown(Value: TBitmap);
  35.     procedure SetNumGlyphs(Value: TVrNumGlyphs);
  36.     procedure SetOrientation(Value: TVrOrientation);
  37.     procedure GlyphsChanged(Sender: TObject);
  38.     procedure TimerExpired(Sender: TObject);
  39.     procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
  40.     procedure CMEnabledChanged(var Message: TMessage); message CM_ENABLEDCHANGED;
  41.   protected
  42.     procedure AdjustGlyphs;
  43.     procedure Paint; override;
  44.     procedure DrawGlyph(GlyphIndex: Integer; Glyphs: TBitmap; Size: TPoint);
  45.     procedure LoadBitmaps; virtual;
  46.     procedure Clicked;
  47.     function GetGlyphRect(Index: Integer): TRect;
  48.     function GetGlyphIndex(X, Y: Integer): Integer;
  49.     procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
  50.     procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
  51.     procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
  52.   public
  53.     constructor Create(AOwner: TComponent); override;
  54.     destructor Destroy; override;
  55.   published
  56.     property GlyphsUp: TBitmap read FGlyphsUp write SetGlyphsUp;
  57.     property GlyphsDown: TBitmap read FGlyphsDown write SetGlyphsDown;
  58.     property NumGlyphs: TVrNumGlyphs read FNumGlyphs write SetNumGlyphs default 4;
  59.     property Orientation: TVrOrientation read FOrientation write SetOrientation default voVertical;
  60.     property RepeatClick: Boolean read FRepeatClick write FRepeatClick default True;
  61.     property RepeatPause: TVrMaxInt read FRepeatPause write FRepeatPause default 100;
  62.     property OnUpClick: TNotifyEvent read FOnUpClick write FOnUpClick;
  63.     property OnDownClick: TNotifyEvent read FOnDownClick write FOnDownClick;
  64.     property Transparent default false;
  65. {$IFDEF VER110}
  66.     property Anchors;
  67.     property Constraints;
  68. {$ENDIF}
  69.     property Color default clBlack;
  70.     property DragCursor;
  71. {$IFDEF VER110}
  72.     property DragKind;
  73. {$ENDIF}
  74.     property DragMode;
  75.     property Enabled;
  76.     property ParentColor default false;
  77.     property ParentShowHint;
  78.     property PopupMenu;
  79.     property ShowHint;
  80.     property Visible;
  81. {$IFDEF VER130}
  82.     property OnContextPopup;
  83. {$ENDIF}    
  84.     property OnDragDrop;
  85.     property OnDragOver;
  86. {$IFDEF VER110}
  87.     property OnEndDock;
  88. {$ENDIF}
  89.     property OnEndDrag;
  90.     property OnMouseDown;
  91.     property OnMouseMove;
  92.     property OnMouseUp;
  93. {$IFDEF VER110}
  94.     property OnStartDock;
  95. {$ENDIF}
  96.     property OnStartDrag;
  97.   end;
  98. implementation
  99. {$R VRUPDOWN.D32}
  100. const
  101.   InitRepeatPause = 400;
  102.   ResId: array[0..1] of PChar = ('UPDOWN_UP', 'UPDOWN_DOWN');
  103. constructor TVrUpDown.Create(AOwner: TComponent);
  104. begin
  105.   inherited Create(AOwner);
  106.   ControlStyle := ControlStyle + [csOpaque];
  107.   Width := 20;
  108.   Height := 30;
  109.   Color := clBlack;
  110.   ParentColor := false;
  111.   Transparent := false;
  112.   FFocusIndex := -1;
  113.   FOrientation := voVertical;
  114.   FGlyphsUp := TBitmap.Create;
  115.   FGlyphsUp.OnChange := GlyphsChanged;
  116.   FGlyphsDown := TBitmap.Create;
  117.   FGlyphsDown.OnChange := GlyphsChanged;
  118.   FNumGlyphs := 4;
  119.   LoadBitmaps;
  120.   FRepeatClick := True;
  121.   FRepeatPause := 100;
  122.   FRepeatTimer := TVrTimer.Create(nil);
  123.   with FRepeatTimer do
  124.   begin
  125.     Enabled := false;
  126.     TimerType := ttSystem;
  127.     Interval := InitRepeatPause;
  128.     OnTimer := TimerExpired;
  129.   end;
  130. end;
  131. destructor TVrUpDown.Destroy;
  132. begin
  133.   FGlyphsUp.Free;
  134.   FGlyphsDown.Free;
  135.   FRepeatTimer.Free;
  136.   inherited Destroy;
  137. end;
  138. procedure TVrUpDown.LoadBitmaps;
  139. begin
  140.   FGlyphsUp.LoadFromResourceName(hInstance, ResId[0]);
  141.   FGlyphsDown.LoadFromResourceName(hInstance, ResId[1]);
  142. end;
  143. procedure TVrUpDown.AdjustGlyphs;
  144. var
  145.   GlyphWidth: Integer;
  146. begin
  147.   if not FGlyphsUp.Empty then
  148.   begin
  149.     GlyphWidth := FGlyphsUp.Width div FNumGlyphs;
  150.     FSizeUp := Point(GlyphWidth, FGlyphsUp.Height);
  151.   end else FSizeUp := Point(0, 0);
  152.   if not FGlyphsDown.Empty then
  153.   begin
  154.     GlyphWidth := FGlyphsDown.Width div FNumGlyphs;
  155.     FSizeDown := Point(GlyphWidth, FGlyphsDown.Height);
  156.   end else FSizeDown := Point(0, 0);
  157. end;
  158. procedure TVrUpDown.GlyphsChanged(Sender: TObject);
  159. begin
  160.   AdjustGlyphs;
  161.   UpdateControlCanvas;
  162. end;
  163. function TVrUpDown.GetGlyphIndex(X, Y: Integer): Integer;
  164. begin
  165.   for Result := 0 to 1 do
  166.     if PtInRect(GetGlyphRect(Result), Point(X, Y)) then Exit;
  167.   Result := -1;
  168. end;
  169. function TVrUpDown.GetGlyphRect(Index: Integer): TRect;
  170. var
  171.   Size: TPoint;
  172. begin
  173.   if Orientation = voVertical then
  174.   begin
  175.     Size.X := Width;
  176.     Size.Y := Height div 2;
  177.     Result := Bounds(0, Size.Y * Index, Size.X, Size.Y);
  178.   end
  179.   else
  180.   begin
  181.     Size.X := Width div 2;
  182.     Size.Y := Height;
  183.     Result := Bounds(Index * Size.X, 0, Size.X, Size.Y);
  184.   end;
  185. end;
  186. procedure TVrUpDown.DrawGlyph(GlyphIndex: Integer; Glyphs: TBitmap; Size: TPoint);
  187. var
  188.   Center: TPoint;
  189.   Index: Integer;
  190.   R, ImageRect, GlyphRect: TRect;
  191. begin
  192.   if Glyphs.Empty then
  193.     Exit;
  194.   Index := 0;
  195.   if FFocusIndex = GlyphIndex then Index := 1;
  196.   if (FDown) and (FFocusIndex = GlyphIndex) then Index := 2;
  197.   if not Enabled then Index := 3;
  198.   if Index > Pred(NumGlyphs) then Index := 0;
  199.   R := GetGlyphRect(GlyphIndex);
  200.   Center.X := R.Left + ((WidthOf(R) - Size.X) div 2);
  201.   Center.Y := R.Top + ((HeightOf(R) - Size.Y) div 2);
  202.   ImageRect := Bounds(Center.X, Center.Y, Size.X, Size.Y);
  203.   GlyphRect := Bounds(Index * Size.X, 0, Size.X, Size.Y);
  204.   BitmapCanvas.Brush.Style := bsClear;
  205.   BitmapCanvas.BrushCopy(ImageRect, Glyphs, GlyphRect, Glyphs.TransparentColor);
  206. end;
  207. procedure TVrUpDown.Paint;
  208. begin
  209.   ClearBitmapCanvas;
  210.   DrawGlyph(0, GlyphsUp, FSizeUp);
  211.   DrawGlyph(1, GlyphsDown, FSizeDown);
  212.   inherited Paint;
  213. end;
  214. procedure TVrUpDown.SetGlyphsUp(Value: TBitmap);
  215. begin
  216.   if Value = nil then
  217.     FGlyphsUp.LoadFromResourceName(hInstance, ResId[0])
  218.   else FGlyphsUp.Assign(Value);
  219. end;
  220. procedure TVrUpDown.SetGlyphsDown(Value: TBitmap);
  221. begin
  222.   if Value = nil then
  223.     FGlyphsDown.LoadFromResourceName(hInstance, ResId[1])
  224.   else FGlyphsDown.Assign(Value);
  225. end;
  226. procedure TVrUpDown.SetNumGlyphs(Value: TVrNumGlyphs);
  227. begin
  228.   if FNumGlyphs <> Value then
  229.   begin
  230.     FNumGlyphs := Value;
  231.     AdjustGlyphs;
  232.     UpdateControlCanvas;
  233.   end;
  234. end;
  235. procedure TVrUpDown.SetOrientation(Value: TVrOrientation);
  236. begin
  237.   if FOrientation <> Value then
  238.   begin
  239.     FOrientation := Value;
  240.     if not Loading then
  241.       BoundsRect := Bounds(Left, Top, Height, Width);
  242.     UpdateControlCanvas;
  243.   end;
  244. end;
  245. procedure TVrUpDown.Clicked;
  246. begin
  247.   case FDownIndex of
  248.     0: if Assigned(FOnUpClick) then FOnUpClick(Self);
  249.     1: if Assigned(FOnDownClick) then FOnDownClick(Self);
  250.   end;
  251. end;
  252. procedure TVrUpDown.MouseMove(Shift: TShiftState; X, Y: Integer);
  253. var
  254.   NewIndex: Integer;
  255. begin
  256.   inherited;
  257.   NewIndex := GetGlyphIndex(X, Y);
  258.   if FPressed then
  259.     FDown := NewIndex = FDownIndex;
  260.   if FFocusIndex <> NewIndex then
  261.   begin
  262.     FFocusIndex := NewIndex;
  263.     UpdateControlCanvas;
  264.   end;
  265. end;
  266. procedure TVrUpDown.MouseDown(Button: TMouseButton;
  267.   Shift: TShiftState; X, Y: Integer);
  268. begin
  269.   if (Button = mbLeft) and Enabled then
  270.   begin
  271.     FPressed := True;
  272.     FDown := True;
  273.     FDownIndex := GetGlyphIndex(X, Y);
  274.     FRepeatTimer.Interval := InitRepeatPause;
  275.     FRepeatTimer.Enabled := RepeatClick;
  276.     UpdateControlCanvas;
  277.   end;
  278.   inherited;
  279. end;
  280. procedure TVrUpDown.MouseUp(Button: TMouseButton;
  281.   Shift: TShiftState; X, Y: Integer);
  282. var
  283.   DoClick: Boolean;
  284. begin
  285.   FRepeatTimer.Enabled := false;
  286.   if FPressed then
  287.   begin
  288.     FPressed := false;
  289.     DoClick := FDown;
  290.     FDown := false;
  291.     UpdateControlCanvas;
  292.     if DoClick then Clicked;
  293.   end;
  294.   inherited;
  295. end;
  296. procedure TVrUpDown.CMMouseLeave(var Message: TMessage);
  297. begin
  298.   inherited;
  299.   FFocusIndex := -1;
  300.   UpdateControlCanvas;
  301. end;
  302. procedure TVrUpDown.CMEnabledChanged(var Message: TMessage);
  303. begin
  304.   inherited;
  305.   UpdateControlCanvas;
  306. end;
  307. procedure TVrUpDown.TimerExpired(Sender: TObject);
  308. begin
  309.   FRepeatTimer.Interval := RepeatPause;
  310.   if (FPressed and FDown) then
  311.   begin
  312.     try
  313.       Clicked;
  314.     except
  315.       FRepeatTimer.Enabled := False;
  316.       raise;
  317.     end;
  318.   end;
  319. end;
  320. end.