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

Delphi控件源码

开发平台:

Delphi

  1. {*****************************************************}
  2. {                                                     }
  3. {     Varian Component Workshop                       }
  4. {                                                     }
  5. {     Varian Software NL (c) 1996-2000                }
  6. {     All Rights Reserved                             }
  7. {                                                     }
  8. {*****************************************************}
  9. unit VrSlider;
  10. {$I VRLIB.INC}
  11. interface
  12. uses
  13.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  14.   VrConst, VrTypes, VrClasses, VrControls, VrSysUtils, VrSystem;
  15. type
  16.   TVrSliderOption = (soActiveClick, soMouseClip, soHandPoint, soThumbOpaque);
  17.   TVrSliderOptions = set of TVrSliderOption;
  18.   TVrSliderStyle = (ssBottomLeft, ssTopRight);
  19.   TVrSlider = class(TVrCustomImageControl)
  20.   private
  21.     FBevel: TVrBevel;
  22.     FPalette: TVrPalette;
  23.     FThumbRect: TRect;
  24.     FThumbWidth: Integer;
  25.     FThumbHeight: Integer;
  26.     FThumbImage: TBitmap;
  27.     FThumbImageIndex: Integer;
  28.     FThumbStates: TVrNumGlyphs;
  29.     FThumbIndent: Integer;
  30.     FBackImageOrg: TBitmap;
  31.     FBackImageNew: TBitmap;
  32.     FMinValue: Integer;
  33.     FMaxValue: Integer;
  34.     FPosition: Integer;
  35.     FOrientation: TVrOrientation;
  36.     FSpacing: Integer;
  37.     FTickWidth: Integer;
  38.     FSolidFill: Boolean;
  39.     FStyle: TVrSliderStyle;
  40.     FOptions: TVrSliderOptions;
  41.     FKeyIncrement: Integer;
  42.     FBorderColor: TColor;
  43.     FBorderWidth: Integer;
  44.     FFocusColor: TColor;
  45.     FHit: Integer;
  46.     FClipOn: Boolean;
  47.     FFocused: Boolean;
  48.     FThumbDown: Boolean;
  49.     FThumbHasMouse: Boolean;
  50.     FBitmapList: TVrBitmapList;
  51.     FBitmapListLink: TVrChangeLink;
  52.     FOnChange: TNotifyEvent;
  53.     procedure SetMinValue(Value: Integer);
  54.     procedure SetMaxValue(Value: Integer);
  55.     procedure SetPosition(Value: Integer);
  56.     procedure SetTickWidth(Value: Integer);
  57.     procedure SetSpacing(Value: Integer);
  58.     procedure SetSolidFill(Value: Boolean);
  59.     procedure SetOrientation(Value: TVrOrientation);
  60.     procedure SetStyle(Value: TVrSliderStyle);
  61.     procedure SetThumbImageIndex(Value: Integer);
  62.     procedure SetThumbStates(Value: TVrNumGlyphs);
  63.     procedure SetThumbIndent(Value: Integer);
  64.     procedure SetOptions(Value: TVrSliderOptions);
  65.     procedure SetBorderColor(Value: TColor);
  66.     procedure SetBorderWidth(Value: Integer);
  67.     procedure SetFocusColor(Value: TColor);
  68.     procedure SetBitmapList(Value: TVrBitmapList);
  69.     procedure SetPalette(Value: TVrPalette);
  70.     procedure SetBevel(Value: TVrBevel);
  71.     procedure BevelChanged(Sender: TObject);
  72.     procedure PaletteModified(Sender: TObject);
  73.     procedure BitmapListChanged(Sender: TObject);
  74.     procedure WMSize(var Message: TWMSize); message WM_SIZE;
  75.     procedure WMSetCursor(var Message: TWMSetCursor); message WM_SETCURSOR;
  76.     procedure CMFocusChanged(var Message: TCMFocusChanged); message CM_FOCUSCHANGED;
  77.     procedure CMEnabledChanged(var Message: TMessage); message CM_ENABLEDCHANGED;
  78.     procedure WMGetDlgCode(var Msg: TWMGetDlgCode); message WM_GETDLGCODE;
  79.     procedure CMColorChanged(var Message: TMessage); message CM_COLORCHANGED;
  80.   protected
  81.     procedure GetThumbImage;
  82.     procedure SetThumbTop(ATop: Integer);
  83.     procedure SetThumbLeft(ALeft: Integer);
  84.     procedure CenterThumb;
  85.     procedure CreateBackImages;
  86.     procedure Loaded; override;
  87.     procedure Paint; override;
  88.     procedure PaintThumb;
  89.     procedure Changed; dynamic;
  90.     procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  91.     procedure CreateParams(var Params: TCreateParams); override;
  92.     procedure DrawVert(Canvas: TCanvas; Rect: TRect; Color: TColor);
  93.     procedure DrawHorz(Canvas: TCanvas; Rect: TRect; Color: TColor);
  94.     function GetViewWidth: Integer;
  95.     function GetOffsetByValue(Value: Integer): Integer;
  96.     function GetValueByOffset(Offset: Integer): Integer;
  97.     function GetBitmap(Index: Integer): TBitmap;
  98.     function GetSliderRect: TRect;
  99.     function GetMinIndent(Rect: TRect): Integer;
  100.     procedure SetThumbOffset(Value: Integer);
  101.     procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
  102.     procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
  103.     procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
  104.     procedure KeyDown(var Key: Word; Shift: TShiftState); override;
  105.   public
  106.     constructor Create(AOwner: TComponent); override;
  107.     destructor Destroy; override;
  108.   published
  109.     property MaxValue: Integer read FMaxValue write SetMaxValue default 100;
  110.     property MinValue: Integer read FMinValue write SetMinValue default 0;
  111.     property Position: Integer read FPosition write SetPosition default 0;
  112.     property Spacing: Integer read FSpacing write SetSpacing default 1;
  113.     property TickWidth: Integer read FTickWidth write SetTickWidth default 1;
  114.     property Orientation: TVrOrientation read FOrientation write SetOrientation default voVertical;
  115.     property SolidFill: Boolean read FSolidFill write SetSolidFill default True;
  116.     property Style: TVrSliderStyle read FStyle write SetStyle default ssBottomLeft;
  117.     property ThumbStates: TVrNumGlyphs read FThumbStates write SetThumbStates default 1;
  118.     property Options: TVrSliderOptions read FOptions write SetOptions
  119.       default [soHandPoint, soThumbOpaque];
  120.     property KeyIncrement: Integer read FKeyIncrement write FKeyIncrement default 5;
  121.     property BorderColor: TColor read FBorderColor write SetBorderColor default clBtnFace;
  122.     property BorderWidth: Integer read FBorderWidth write SetBorderWidth default 1;
  123.     property FocusColor: TColor read FFocusColor write SetFocusColor default clBlue;
  124.     property Bevel: TVrBevel read FBevel write SetBevel;
  125.     property Palette: TVrPalette read FPalette write SetPalette;
  126.     property ThumbImageIndex: Integer read FThumbImageIndex write SetThumbImageIndex default -1;
  127.     property ThumbIndent: Integer read FThumbIndent write SetThumbIndent default 2;
  128.     property BitmapList: TVrBitmapList read FBitmapList write SetBitmapList;
  129.     property OnChange: TNotifyEvent read FOnChange write FOnChange;
  130. {$IFDEF VER110}
  131.     property Anchors;
  132.     property Constraints;
  133. {$ENDIF}
  134.     property Enabled;
  135.     property Color default clBlack;
  136.     property Cursor;
  137.     property DragMode;
  138. {$IFDEF VER110}
  139.     property DragKind;
  140. {$ENDIF}
  141.     property DragCursor;
  142.     property ParentColor default false;
  143.     property ParentShowHint;
  144.     property ShowHint;
  145.     property TabOrder;
  146.     property TabStop default false;
  147.     property Visible;
  148.     property OnClick;
  149. {$IFDEF VER130}
  150.     property OnContextPopup;
  151. {$ENDIF}
  152.     property OnDblClick;
  153.     property OnMouseMove;
  154.     property OnMouseDown;
  155.     property OnMouseUp;
  156.     property OnKeyDown;
  157.     property OnKeyUp;
  158.     property OnKeyPress;
  159.     property OnDragOver;
  160. {$IFDEF VER110}
  161.     property OnEndDock;
  162. {$ENDIF}
  163.     property OnDragDrop;
  164.     property OnEndDrag;
  165. {$IFDEF VER110}
  166.     property OnStartDock;
  167. {$ENDIF}
  168.     property OnStartDrag;
  169.   end;
  170. implementation
  171. {$R VRSLIDER.D32}
  172. const
  173.   ThumbNames: array[0..1] of PChar =
  174.     ('VRSLIDERTHUMB_VERT', 'VRSLIDERTHUMB_HORI');
  175. function CreateLCDImage(AWidth, AHeight: Integer;
  176.   ForeColor, BackColor: TColor): TBitmap;
  177. begin
  178.   Result := TBitmap.Create;
  179.   with TBitmap(Result) do
  180.   begin
  181.     Width := AWidth;
  182.     Height := AHeight;
  183.     Canvas.Brush.Bitmap := CreateDitherPattern(ForeColor, BackColor);
  184.     Canvas.FillRect(BitmapRect(Result));
  185.     Canvas.Brush.Bitmap.Free;
  186.     Canvas.Brush.Bitmap := nil;
  187.   end;
  188. end;
  189. constructor TVrSlider.Create(AOwner: TComponent);
  190. begin
  191.   inherited Create(AOwner);
  192.   ControlStyle := ControlStyle + [csOpaque];
  193.   Width := 36;
  194.   Height := 171;
  195.   Color := clBlack;
  196.   ParentColor := false;
  197.   Tabstop := false;
  198.   FMinValue := 0;
  199.   FMaxValue := 100;
  200.   FPosition := 0;
  201.   FSpacing := 1;
  202.   FTickWidth := 1;
  203.   FOrientation := voVertical;
  204.   FSolidFill := True;
  205.   FStyle := ssBottomLeft;
  206.   FOptions := [soHandPoint, soThumbOpaque];
  207.   FKeyIncrement := 5;
  208.   FBorderColor := clBtnFace;
  209.   FBorderWidth := 1;
  210.   FFocusColor := clBlue;
  211.   FThumbStates := 1;
  212.   FThumbIndent := 2;
  213.   FThumbImage := TBitmap.Create;
  214.   FBackImageOrg := TBitmap.Create;
  215.   FBackImageNew := TBitmap.Create;
  216.   FBevel := TVrBevel.Create;
  217.   with FBevel do
  218.   begin
  219.     InnerStyle := bsLowered;
  220.     InnerWidth := 2;
  221.     InnerColor := clBlack;
  222.     OnChange := BevelChanged;
  223.   end;
  224.   FPalette := TVrPalette.Create;
  225.   FPalette.OnChange := PaletteModified;
  226.   FThumbImageIndex := -1;
  227.   FBitmapListLink := TVrChangeLink.Create;
  228.   FBitmapListLink.OnChange := BitmapListChanged;
  229.   GetThumbImage;
  230. end;
  231. destructor TVrSlider.Destroy;
  232. begin
  233.   FBevel.Free;
  234.   FPalette.Free;
  235.   FThumbImage.Free;
  236.   FBackImageOrg.Free;
  237.   FBackImageNew.Free;
  238.   FBitmapListLink.Free;
  239.   inherited Destroy;
  240. end;
  241. procedure TVrSlider.CreateParams(var Params: TCreateParams);
  242. begin
  243.   inherited CreateParams(Params);
  244.   with Params do
  245.     WindowClass.style := WindowClass.style and not (CS_HREDRAW or CS_VREDRAW);
  246. end;
  247. procedure TVrSlider.Loaded;
  248. begin
  249.   inherited Loaded;
  250.   GetThumbImage;
  251. end;
  252. procedure TVrSlider.Notification(AComponent: TComponent;
  253.   Operation: TOperation);
  254. begin
  255.   inherited Notification(AComponent, Operation);
  256.   if (Operation = opRemove) then
  257.     if AComponent = BitmapList then BitmapList := nil;
  258. end;
  259. procedure TVrSlider.GetThumbImage;
  260. begin
  261.   FThumbImage.Assign(GetBitmap(FThumbImageIndex));
  262.   if FThumbImage.Empty then
  263.   begin
  264.     if Orientation = voVertical then
  265.     FThumbImage.LoadFromResourceName(hInstance, ThumbNames[1])
  266.     else FThumbImage.LoadFromResourceName(hInstance, ThumbNames[0]);
  267.   end;
  268.   FThumbHeight := FThumbImage.Height;
  269.   FThumbWidth := FThumbImage.Width div ThumbStates;
  270.   CenterThumb;
  271. end;
  272. function TVrSlider.GetBitmap(Index: Integer): TBitmap;
  273. begin
  274.   Result := nil;
  275.   if Assigned(FBitmapList) then
  276.     Result := FBitmapList.GetBitmap(Index);
  277. end;
  278. procedure TVrSlider.SetThumbLeft(ALeft: Integer);
  279. begin
  280.   FThumbRect := Bounds(ALeft, FThumbRect.Top, FThumbWidth, FThumbHeight);
  281. end;
  282. procedure TVrSlider.SetThumbTop(ATop: Integer);
  283. begin
  284.   FThumbRect := Bounds(FThumbRect.Left, ATop, FThumbWidth, FThumbHeight);
  285. end;
  286. procedure TVrSlider.CenterThumb;
  287. begin
  288.   if Orientation = voVertical then
  289.     SetThumbLeft((Width - FThumbWidth) div 2)
  290.   else SetThumbTop((Height - FThumbHeight) div 2);
  291. end;
  292. function TVrSlider.GetSliderRect: TRect;
  293. begin
  294.   Result := ClientRect;
  295.   InflateRect(Result, -BorderWidth - ThumbIndent, -BorderWidth - ThumbIndent);
  296. end;
  297. function TVrSlider.GetMinIndent(Rect: TRect): Integer;
  298. begin
  299.   if Orientation = voVertical then
  300.     Result := MaxIntVal(0, Rect.Top)
  301.   else
  302.     Result := MaxIntVal(0, Rect.Left);
  303. end;
  304. function TVrSlider.GetViewWidth: Integer;
  305. var
  306.   R: TRect;
  307. begin
  308.   R := GetSliderRect;
  309.   if Orientation = voVertical then
  310.   Result := HeightOf(R) - FThumbHeight
  311.   else Result := WidthOf(R) - FThumbWidth;
  312. end;
  313. function TVrSlider.GetOffsetByValue(Value: Integer): Integer;
  314. var
  315.   Range: Double;
  316.   R: TRect;
  317.   MinIndent: Integer;
  318. begin
  319.   R := GetSliderRect;
  320.   MinIndent := GetMinIndent(R);
  321.   Range := MaxValue - MinValue;
  322.   Result := Round((Value - MinValue) / Range * GetViewWidth) + MinIndent;
  323.   if (FOrientation = voVertical) and (FStyle = ssBottomLeft) then
  324.     Result := R.Top + R.Bottom - Result - FThumbHeight
  325.   else
  326.   if (FOrientation = voHorizontal) and (FStyle = ssTopRight) then
  327.     Result := R.Left + R.Right - Result - FThumbWidth;
  328. end;
  329. function TVrSlider.GetValueByOffset(Offset: Integer): Integer;
  330. var
  331.   R: TRect;
  332.   Range: Double;
  333.   MinIndent: Integer;
  334. begin
  335.   R := GetSliderRect;
  336.   MinIndent := GetMinIndent(R);
  337.   if Orientation = voVertical then
  338.     Offset := ClientHeight - Offset - FThumbHeight;
  339.   Range := FMaxValue - FMinValue;
  340.   Result := Round((Offset - MinIndent) * Range / GetViewWidth);
  341.   Result := MinIntVal(FMinValue + MaxIntVal(Result, 0), FMaxValue);
  342. end;
  343. procedure TVrSlider.SetThumbOffset(Value: Integer);
  344. var
  345.   R: TRect;
  346.   MinIndent: Integer;
  347. begin
  348.   R := GetSliderRect;
  349.   MinIndent := GetMinIndent(R);
  350.   Value := MinIntVal(MaxIntVal(Value, MinIndent),
  351.     MinIndent + GetViewWidth);
  352.   if FStyle = ssBottomLeft then Position := GetValueByOffset(Value)
  353.   else Position := FMaxValue - GetValueByOffset(Value) + FMinValue;
  354. end;
  355. procedure TVrSlider.Paint;
  356. var
  357.   Value: Integer;
  358.   BevelRect, SrcRect, DestRect: TRect;
  359.   CurrentColor: TColor;
  360. begin
  361.   ClearBitmapCanvas;
  362.   BevelRect := ClientRect;
  363.   if BorderWidth > 0 then
  364.   begin
  365.     if FFocused then CurrentColor := FFocusColor
  366.     else CurrentColor := FBorderColor;
  367.     DrawFrame3D(BitmapCanvas, BevelRect,
  368.       CurrentColor, CurrentColor, BorderWidth);
  369.   end;
  370.   Bevel.Paint(BitmapCanvas, BevelRect);
  371.   BitmapCanvas.CopyRect(BevelRect,
  372.     FBackImageOrg.Canvas, BitmapRect(FBackImageOrg));
  373.   Value := GetOffsetByValue(Position);
  374.   if Orientation = voVertical then
  375.     SetThumbTop(Value) else SetThumbLeft(Value);
  376.   DestRect := BevelRect;
  377.   SrcRect := BitmapRect(FBackImageNew);
  378.   if Orientation = voVertical then
  379.   begin
  380.     if Style = ssBottomLeft then
  381.     begin
  382.       DestRect.Top := FThumbRect.Top + FThumbHeight div 2;
  383.       SrcRect.Top := DestRect.Top - BevelRect.Top;
  384.     end else
  385.     begin
  386.       DestRect.Bottom := FThumbRect.Bottom - FThumbHeight div 2;
  387.       SrcRect.Bottom := HeightOf(DestRect);
  388.     end;
  389.   end else
  390.   begin
  391.     if Style = ssBottomLeft then
  392.     begin
  393.       DestRect.Right := FThumbRect.Left + FThumbWidth div 2;
  394.       SrcRect.Right := WidthOf(DestRect);
  395.     end else
  396.     begin
  397.       DestRect.Left := FThumbRect.Right - FThumbWidth div 2;
  398.       SrcRect.Left := DestRect.Left - BevelRect.Left;
  399.     end;
  400.   end;
  401.   BitmapCanvas.CopyRect(DestRect, FBackImageNew.Canvas, SrcRect);
  402.   PaintThumb;
  403.   inherited Paint;
  404. end;
  405. procedure TVrSlider.PaintThumb;
  406. var
  407.   Index: Integer;
  408.   SrcRect: TRect;
  409.   TransColor: TColor;
  410. begin
  411.   Index := 0;
  412.   if not Enabled then Index := 1;
  413.   if FThumbDown then Index := 2;
  414.   if (FThumbHasMouse) and (not FThumbDown) then Index := 3;
  415.   if Index > ThumbStates - 1 then Index := 0;
  416.   SrcRect := Bounds(Index * FThumbWidth, 0, FThumbWidth, FThumbHeight);
  417.   with BitmapCanvas do
  418.   begin
  419.     TransColor := FThumbImage.TransparentColor;
  420.     Brush.Color := TransColor;
  421.     if soThumbOpaque in Options then Brush.Style := bsSolid
  422.     else Brush.Style := bsClear;
  423.     BrushCopy(FThumbRect, FThumbImage, SrcRect, TransColor);
  424.   end;
  425. end;
  426. procedure TVrSlider.DrawVert(Canvas: TCanvas; Rect: TRect;
  427.   Color: TColor);
  428. var
  429.   R: TRect;
  430.   X, Y, W, I, Cnt: Integer;
  431.   OffsetValue: Integer;
  432.   Bm: TBitmap;
  433. begin
  434.   with Canvas do
  435.   begin
  436.     W := WidthOf(Rect);
  437.     Cnt := (HeightOf(Rect) div (TickWidth + Spacing)) + 1;
  438.     X := Rect.Left;
  439.     if Style = ssBottomLeft then
  440.     begin
  441.       Y := Rect.Bottom - TickWidth;
  442.       OffsetValue := -(TickWidth + Spacing);
  443.     end else
  444.     begin
  445.       Y := Rect.Top;
  446.       OffsetValue := TickWidth + Spacing;
  447.     end;
  448.     Brush.Color := Self.Color;
  449.     FillRect(Rect);
  450.     Bm := nil;
  451.     if not SolidFill then
  452.       Bm := CreateLCDImage(W, TickWidth, Color, Self.Color)
  453.     else Brush.Color := Color;
  454.     for I := 0 to Cnt do
  455.     begin
  456.       R := Bounds(X, Y, W, TickWidth);
  457.       if SolidFill then FillRect(R)
  458.       else CopyRect(R, Bm.Canvas, BitmapRect(Bm));
  459.       Inc(Y, OffsetValue);
  460.     end;
  461.     if Bm <> nil then Bm.Free;
  462.   end;
  463. end;
  464. procedure TVrSlider.DrawHorz(Canvas: TCanvas; Rect: TRect;
  465.   Color: TColor);
  466. var
  467.   R: TRect;
  468.   X, Y, I, H, Cnt: Integer;
  469.   OffsetValue: Integer;
  470.   Bm: TBitmap;
  471. begin
  472.   with Canvas do
  473.   begin
  474.     H := HeightOf(Rect);
  475.     Cnt := (WidthOf(Rect) div (TickWidth + Spacing)) + 1;
  476.     Y := Rect.Top;
  477.     if FStyle = ssBottomLeft then
  478.     begin
  479.       X := Rect.Left;
  480.       OffsetValue := (TickWidth + Spacing);
  481.     end
  482.     else
  483.     begin
  484.       X := Rect.Right - TickWidth;
  485.       OffsetValue := -(TickWidth + Spacing);
  486.     end;
  487.     Brush.Color := Self.Color;
  488.     FillRect(Rect);
  489.     Bm := nil;
  490.     if not SolidFill then
  491.       Bm := CreateLCDImage(TickWidth, H, Color, Self.Color)
  492.     else Brush.Color := Color;
  493.     for I := 0 to Cnt do
  494.     begin
  495.       R := Bounds(X, Y, TickWidth, H);
  496.       if SolidFill then FillRect(R)
  497.       else CopyRect(R, Bm.Canvas, BitmapRect(Bm));
  498.       Inc(X, OffsetValue);
  499.     end;
  500.     if Bm <> nil then Bm.Free;
  501.   end;
  502. end;
  503. procedure TVrSlider.CreateBackImages;
  504. var
  505.   W, H: Integer;
  506.   PaintRect: TRect;
  507. begin
  508.   PaintRect := GetSliderRect;
  509.   InflateRect(PaintRect, ThumbIndent, ThumbIndent);
  510.   Bevel.GetVisibleArea(PaintRect);
  511.   W := WidthOf(PaintRect);
  512.   H := HeightOf(PaintRect);
  513.   with FBackImageOrg do
  514.   begin
  515.     Width := W;
  516.     Height := H;
  517.   end;
  518.   with FBackImageNew do
  519.   begin
  520.     Width := W;
  521.     Height := H;
  522.   end;
  523.   if Orientation = voVertical then
  524.   begin
  525.     DrawVert(FBackImageOrg.Canvas, BitmapRect(FBackImageOrg), Palette[0]);
  526.     DrawVert(FBackImageNew.Canvas, BitmapRect(FBackImageNew), Palette[1]);
  527.   end else
  528.   begin
  529.     DrawHorz(FBackImageOrg.Canvas, BitmapRect(FBackImageOrg), Palette[0]);
  530.     DrawHorz(FBackImageNew.Canvas, BitmapRect(FBackImageNew), Palette[1]);
  531.   end;
  532. end;
  533. procedure TVrSlider.Changed;
  534. begin
  535.   if Assigned(FOnChange) then FOnChange(Self);
  536. end;
  537. procedure TVrSlider.WMSize(var Message: TWMSize);
  538. begin
  539.   inherited;
  540.   CreateBackImages;
  541.   CenterThumb;
  542.   UpdateControlCanvas;
  543. end;
  544. procedure TVrSlider.WMSetCursor(var Message: TWMSetCursor);
  545. var
  546.   P: TPoint;
  547. begin
  548.   GetCursorPos(P);
  549.   if (not Designing) and PtInRect(FThumbRect, ScreenToClient(P)) then
  550.   begin
  551.     if (soHandPoint in Options) then
  552.       Windows.SetCursor(Screen.Cursors[VrCursorHandPoint]);
  553.   end else inherited;
  554. end;
  555. procedure TVrSlider.CMFocusChanged(var Message: TCMFocusChanged);
  556. var
  557.   Active: Boolean;
  558. begin
  559.   with Message do Active := (Sender = Self);
  560.   if Active <> FFocused then
  561.   begin
  562.     FFocused := Active;
  563.     UpdateControlCanvas;
  564.   end;
  565.   inherited;
  566. end;
  567. procedure TVrSlider.WMGetDlgCode(var Msg: TWMGetDlgCode);
  568. begin
  569.   Msg.Result := DLGC_WANTARROWS;
  570. end;
  571. procedure TVrSlider.CMEnabledChanged(var Message: TMessage);
  572. begin
  573.   inherited;
  574.   UpdateControlCanvas;
  575. end;
  576. procedure TVrSlider.CMColorChanged(var Message: TMessage);
  577. begin
  578.   inherited;
  579.   if (HandleAllocated) then
  580.   begin
  581.     CreateBackImages;
  582.     UpdateControlCanvas;
  583.   end;
  584. end;
  585. procedure TVrSlider.BevelChanged(Sender: TObject);
  586. begin
  587.   if not Loading then
  588.     CreateBackImages;
  589.   UpdateControlCanvas;
  590. end;
  591. procedure TVrSlider.SetBevel(Value: TVrBevel);
  592. begin
  593.   FBevel.Assign(Value);
  594. end;
  595. procedure TVrSlider.PaletteModified(Sender: TObject);
  596. begin
  597.   if not Loading then CreateBackImages;
  598.   UpdateControlCanvas;
  599. end;
  600. procedure TVrSlider.SetPalette(Value: TVrPalette);
  601. begin
  602.   FPalette.Assign(Value);
  603. end;
  604. procedure TVrSlider.BitmapListChanged(Sender: TObject);
  605. begin
  606.   GetThumbImage;
  607.   UpdateControlCanvas;
  608. end;
  609. procedure TVrSlider.SetThumbImageIndex(Value: Integer);
  610. begin
  611.   if FThumbImageIndex <> Value then
  612.   begin
  613.     FThumbImageIndex := Value;
  614.     if not Loading then
  615.       GetThumbImage;
  616.     UpdateControlCanvas;
  617.   end;
  618. end;
  619. procedure TVrSlider.SetBitmapList(Value: TVrBitmapList);
  620. begin
  621.   if FBitmapList <> nil then
  622.     FBitmapList.RemoveLink(FBitmapListLink);
  623.   FBitmapList := Value;
  624.   if FBitmapList <> nil then
  625.     FBitmapList.InsertLink(FBitmapListLink);
  626.   if not Loading then
  627.   begin
  628.     GetThumbImage;
  629.     UpdateControlCanvas;
  630.   end;
  631. end;
  632. procedure TVrSlider.SetMinValue(Value: Integer);
  633. begin
  634.   if FMinValue <> Value then
  635.   begin
  636.     FMinValue := Value;
  637.     if Position < FMinValue then
  638.       Position := FMinValue
  639.     else UpdateControlCanvas;
  640.   end;
  641. end;
  642. procedure TVrSlider.SetMaxValue(Value: Integer);
  643. begin
  644.   if FMaxValue <> Value then
  645.   begin
  646.     FMaxValue := Value;
  647.     if Position > FMaxValue then
  648.       Position := FMaxValue
  649.     else UpdateControlCanvas;
  650.   end;
  651. end;
  652. procedure TVrSlider.SetPosition(Value: Integer);
  653. begin
  654.   if Value < FMinValue then Value := FMinValue;
  655.   if Value > FMaxValue then Value := FMaxValue;
  656.   if FPosition <> Value then
  657.   begin
  658.     FPosition := Value;
  659.     UpdateControlCanvas;
  660.     Changed;
  661.   end;
  662. end;
  663. procedure TVrSlider.SetSpacing(Value: Integer);
  664. begin
  665.   if (FSpacing <> Value) and (Value > -1) then
  666.   begin
  667.     FSpacing := Value;
  668.     if not Loading then
  669.       CreateBackImages;
  670.     UpdateControlCanvas;
  671.   end;
  672. end;
  673. procedure TVrSlider.SetTickWidth(Value: Integer);
  674. begin
  675.   if (FTickWidth <> Value) and (Value > 0) then
  676.   begin
  677.     FTickWidth := Value;
  678.     if not Loading then
  679.       CreateBackImages;
  680.     UpdateControlCanvas;
  681.   end;
  682. end;
  683. procedure TVrSlider.SetSolidFill(Value: Boolean);
  684. begin
  685.   if FSolidFill <> Value then
  686.   begin
  687.     FSolidFill := Value;
  688.     if not Loading then
  689.       CreateBackImages;
  690.     UpdateControlCanvas;
  691.   end;
  692. end;
  693. procedure TVrSlider.SetOrientation(Value: TVrOrientation);
  694. begin
  695.   if FOrientation <> Value then
  696.   begin
  697.     FOrientation := Value;
  698.     if not Loading then
  699.     begin
  700.       BoundsRect := Bounds(Left, Top, Height, Width);
  701.       if Height = Width then
  702.         CreateBackImages;
  703.       GetThumbImage;
  704.     end;
  705.     UpdateControlCanvas;
  706.   end;
  707. end;
  708. procedure TVrSlider.SetStyle(Value: TVrSliderStyle);
  709. begin
  710.   if FStyle <> Value then
  711.   begin
  712.     FStyle := Value;
  713.     if not Loading then
  714.       CreateBackImages;
  715.     UpdateControlCanvas;
  716.   end;
  717. end;
  718. procedure TVrSlider.SetOptions(Value: TVrSliderOptions);
  719. begin
  720.   if FOptions <> Value then
  721.   begin
  722.     FOptions := Value;
  723.     UpdateControlCanvas;
  724.   end;
  725. end;
  726. procedure TVrSlider.SetBorderColor(Value: TColor);
  727. begin
  728.   if FBorderColor <> Value then
  729.   begin
  730.     FBorderColor := Value;
  731.     UpdateControlCanvas;
  732.   end;
  733. end;
  734. procedure TVrSlider.SetBorderWidth(Value: Integer);
  735. begin
  736.   if (FBorderWidth <> Value) and (Value >= 0) then
  737.   begin
  738.     FBorderWidth := Value;
  739.     if not Loading then
  740.       CreateBackImages;
  741.     UpdateControlCanvas;
  742.   end;
  743. end;
  744. procedure TVrSlider.SetFocusColor(Value: TColor);
  745. begin
  746.   if FFocusColor <> Value then
  747.   begin
  748.     FFocusColor := Value;
  749.     UpdateControlCanvas;
  750.   end;
  751. end;
  752. procedure TVrSlider.SetThumbStates(Value: TVrNumGlyphs);
  753. begin
  754.   if FThumbStates <> Value then
  755.   begin
  756.     FThumbStates := Value;
  757.     if not Loading then
  758.       GetThumbImage;
  759.     UpdateControlCanvas;
  760.   end;
  761. end;
  762. procedure TVrSlider.SetThumbIndent(Value: Integer);
  763. begin
  764.   if (FThumbIndent <> Value) and (Value >= 0) then
  765.   begin
  766.     FThumbIndent := Value;
  767.     UpdateControlCanvas;
  768.   end;
  769. end;
  770. procedure TVrSlider.MouseDown(Button: TMouseButton; Shift: TShiftState;
  771.   X, Y: Integer);
  772. var
  773.   R: TRect;
  774.   P: TPoint;
  775. begin
  776.   inherited MouseDown(Button, Shift, X, Y);
  777.   if (Button = mbLeft) then
  778.   begin
  779.     if TabStop then SetFocus;
  780.     P := Point(X, Y);
  781.     if PtInRect(FThumbRect, P) then
  782.     begin
  783.       FThumbDown := True;
  784.       if Orientation = voHorizontal then FHit := X - FThumbRect.Left
  785.       else FHit := Y - FThumbRect.Top;
  786.       if (soMouseClip in Options) then
  787.       begin
  788.         R := Bounds(ClientOrigin.X, ClientOrigin.Y,
  789.           ClientWidth, ClientHeight);
  790.         ClipCursor(@R);
  791.         FClipOn := True;
  792.       end;
  793.       UpdateControlCanvas;
  794.     end
  795.     else
  796.     if (soActiveClick in Options) then
  797.     begin
  798.       if Orientation = voHorizontal then
  799.         FHit := X - FThumbWidth div 2
  800.       else FHit := Y - FThumbHeight div 2;
  801.       SetThumbOffset(FHit);
  802.     end;
  803.   end;
  804. end;
  805. procedure TVrSlider.MouseMove(Shift: TShiftState; X, Y: Integer);
  806. var
  807.   OldValue: Boolean;
  808. begin
  809.   if FThumbDown then
  810.   begin
  811.     if FOrientation = voVertical then
  812.       SetThumbOffset(Y - FHit)
  813.     else
  814.       SetThumbOffset(X - FHit);
  815.   end
  816.   else
  817.   begin
  818.     OldValue := FThumbHasMouse;
  819.     FThumbHasMouse := PtInRect(FThumbRect, Point(X, Y));
  820.     if OldValue <> FThumbHasMouse then UpdateControlCanvas;
  821.   end;
  822.   inherited MouseMove(Shift, X, Y);
  823. end;
  824. procedure TVrSlider.MouseUp(Button: TMouseButton; Shift: TShiftState;
  825.   X, Y: Integer);
  826. begin
  827.   if FThumbDown then
  828.   begin
  829.     FThumbDown := false;
  830.     UpdateControlCanvas;
  831.   end;
  832.   if FClipOn then
  833.   begin
  834.     ClipCursor(nil);
  835.     FClipOn := false;
  836.   end;
  837.   inherited MouseUp(Button, Shift, X, Y);
  838. end;
  839. procedure TVrSlider.KeyDown(var Key: Word; Shift: TShiftState);
  840.   function Adjust(Value: Integer): Integer;
  841.   begin
  842.     Result := Value;
  843.     if Style = ssTopRight then Result := -Result;
  844.   end;
  845. begin
  846.   if Shift = [] then
  847.   begin
  848.     if Key = VK_HOME then Position := MinValue
  849.     else if Key = VK_END then Position := MaxValue;
  850.     if Orientation = voHorizontal then
  851.     begin
  852.       if Key = VK_LEFT then Position := Position + Adjust(-FKeyIncrement)
  853.       else if Key = VK_RIGHT then Position := Position + Adjust(FKeyIncrement);
  854.     end
  855.     else
  856.     begin
  857.       if Key = VK_UP then Position := Position + Adjust(FKeyIncrement)
  858.       else if Key = VK_DOWN then Position := Position + Adjust(-FKeyIncrement);
  859.     end;
  860.   end;
  861.   inherited KeyDown(Key, Shift);
  862. end;
  863. end.