MMSlider.pas
上传用户:hylc_2004
上传日期:2014-01-23
资源大小:46800k
文件大小:42k
- {========================================================================}
- {= (c) 1995-98 SwiftSoft Ronald Dittrich =}
- {========================================================================}
- {= All Rights Reserved =}
- {========================================================================}
- {= D 01099 Dresden = Fax.: +49 (0)351-8037944 =}
- {= Loewenstr.7a = info@swiftsoft.de =}
- {========================================================================}
- {= Actual versions on http://www.swiftsoft.de/mmtools.html =}
- {========================================================================}
- {= This code is for reference purposes only and may not be copied or =}
- {= distributed in any format electronic or otherwise except one copy =}
- {= for backup purposes. =}
- {= =}
- {= No Delphi Component Kit or Component individually or in a collection=}
- {= subclassed or otherwise from the code in this unit, or associated =}
- {= .pas, .dfm, .dcu, .asm or .obj files may be sold or distributed =}
- {= without express permission from SwiftSoft. =}
- {= =}
- {= For more licence informations please refer to the associated =}
- {= HelpFile. =}
- {========================================================================}
- {= $Date: 30.03.98 - 16:35:32 $ =}
- {========================================================================}
- unit MMSlider;
- {$I COMPILER.INC}
- interface
- uses
- {$IFDEF WIN32}
- Windows,
- {$ELSE}
- WinTypes,
- WinProcs,
- {$ENDIF}
- Forms,
- SysUtils,
- Messages,
- Classes,
- Graphics,
- Controls,
- Dialogs,
- ExtCtrls,
- Menus,
- MMObj,
- MMUtils,
- MMBCache,
- MMMath,
- MMString,
- MMScale;
- type
- TMMOrientation = (orVertical,orHorizontal);
- TMMThumbStyle = (tsRect,tsOwnerDraw);
- TMMGrooveStyle = (gsRect,gsOwnerDraw);
- TMMFocusAction = (faNone, faFocusThumb, faFocusRect, faFocusColor, faAll);
- TMMScalePos = (spAboveOrLeft, spBelowOrRight, spBoth);
- TMMThumbDrawEvent = procedure(Sender: TObject; aCanvas: TCanvas; aRect: TRect;
- Dragged,Focused: Boolean) of object;
- TMMGrooveDrawEvent= procedure(Sender: TObject; aCanvas: TCanvas; var aRect: TRect) of object;
- {-- TMMCustomSlider ---------------------------------------------------}
- TMMCustomSlider = class(TMMCustomControl)
- private
- FDragging : Boolean;
- FGroove : TMMBevel;
- FHandCursor : Boolean;
- FThumbCursor : TCursor;
- FMax,FMin,FPosition : Longint;
- FLineSize : Integer;
- FPageSize : Integer;
- FOrientation : TMMOrientation;
- FFocusAction : TMMFocusAction;
- FThumbWidth, FThumbHeight: Byte;
- FThumbColor : TColor;
- FThumbBorder : Boolean;
- FGrooveColor : TColor;
- FFocusColor : TColor;
- FDisabledColor : TColor;
- FFocusTime : Boolean;
- FScaleDistance : Integer;
- FScalePos : TMMScalePos;
- FScale : TMMScale;
- FGrooveSize : Byte;
- FThumbStyle : TMMThumbStyle;
- FGrooveStyle : TMMGrooveStyle;
- FBitmap : TBitmap;
- FForceChange : Boolean;
- FDragOffset : integer;
- FDragVal : Longint;
- HalfTW,HalfTH : Integer;
- FThumbRect : TRect;
- FPicLeft : TBitmap;
- FPicRight : TBitmap;
- FSensitivity : integer;
- FLogMode : Boolean;
- FNeedTrackEnd : Boolean;
- FOnChange : TNotifyEvent;
- FOnTrack : TNotifyEvent;
- FOnTrackEnd : TNotifyEvent;
- FOnDrawThumb : TMMThumbDrawEvent;
- FOnDrawGroove : TMMGrooveDrawEvent;
- FOnGetFocus : TNotifyEvent; { Added January, 30 2000 }
- FOnLostFocus : TNotifYEvent; { Added January, 30 2000 }
- FOnMouseEnter : TNotifyEvent; { Added January, 30 2000 }
- FOnMouseLeave : TNotifyEvent; { Added January, 30 2000 }
- procedure SetColors(index: integer; aValue: TColor);
- procedure SetMax(aValue: Longint);
- procedure SetMin(aValue: Longint);
- procedure SetOrientation(aValue: TMMOrientation);
- procedure SetPosition(aValue: Longint);
- function GetPosition: Longint;
- function UpdatePosition(aValue: Longint): Boolean;
- procedure UpdateFocusTimer(Enable: Boolean);
- procedure SetScaleDist(aValue: Integer);
- procedure SetScalePos(aValue: TMMScalePos);
- procedure SetScale(Value: TMMScale);
- procedure SetFocusAction(aValue: TMMFocusAction);
- procedure SetGrooveSize(aValue: Byte);
- procedure SetThumbSize(index: integer; aValue: Byte);
- procedure SetThumbBorder(aValue: Boolean);
- procedure SetThumbStyle(aValue: TMMThumbStyle);
- procedure SetGrooveStyle(aValue: TMMGrooveStyle);
- procedure SetSensitivity(aValue: integer);
- procedure SetLogMode(aValue: Boolean);
- procedure SetGroove(aValue: TMMBevel);
- procedure GrooveChanged(Sender: TObject);
- procedure ScaleChanged(Sender: TObject);
- procedure TimerAction(Sender: TObject);
- procedure UpdateBitmap(aWidth,aHeight: integer);
- function NewPosition(WhereX,WhereY: Integer): integer;
- function IsVert: Boolean;
- procedure WhereIsThumb(const ClientRect: TRect; var aRect: TRect);
- procedure DrawTrench(Canvas: TCanvas; aRect: TRect);
- procedure DrawScale(Canvas: TCanvas; aRect: TRect);
- procedure DrawThumb(Canvas: TCanvas; aRect: TRect);
- function DrawGroove(Canvas: TCanvas; aRect: TRect): TRect;
- function DrawPics(Canvas: TCanvas; aRect: TRect): TRect;
- procedure SetPicLeft(Value: TBitmap);
- procedure SetPicRight(Value: TBitmap);
- procedure PicChanged(Sender: TObject);
- function CalcClientRect: TRect;
- procedure WMSetFocus(var Message: TWMSetFocus); message WM_SetFocus;
- procedure WMKillFocus(var Message: TWMKillFocus); message WM_KillFocus;
- procedure WMGetDlgCode(var Message: TWMGetDlgCode); message WM_GETDLGCODE;
- procedure WMSize(var Message: TWMSize); message WM_SIZE;
- procedure CMEnabledChanged(var Message: TMessage); message CM_ENABLEDCHANGED;
- procedure CMMouseEnter(var msg: TMessage); message CM_MOUSEENTER; { Added January, 30 2000 }
- procedure CMMouseLeave(var msg: TMessage); message CM_MOUSELEAVE; { Added January, 30 2000 }
- protected
- procedure Change; dynamic;
- procedure Track; dynamic;
- procedure TrackEnd; dynamic;
- procedure OwnerDrawThumb(aCanvas: TCanvas; aRect: TRect; Dragged,Focused: Boolean); dynamic;
- procedure OwnerDrawGroove(aCanvas: TCanvas; var aRect: TRect); dynamic;
- procedure Paint; override;
- procedure Loaded; override;
- procedure SetBounds(aLeft, aTop, aWidth, aHeight: integer); override;
- procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
- procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
- procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
- procedure KeyDown(var Key: Word; Shift: TShiftState); override;
- procedure KeyUp(var Key: Word; Shift: TShiftState); override;
- procedure SetThumbCursor(AtThumb: Boolean);
- public
- constructor Create(AOwner: TComponent); override;
- destructor Destroy; override;
- procedure SetMinMax(aMin,aMax: Longint);
- protected
- property OnChange: TNotifyEvent read FOnChange write FOnChange;
- property OnTrack: TNotifyEvent read FOnTrack write FOnTrack;
- property OnTrackEnd: TNotifyEvent read FOnTrackEnd write FOnTrackEnd;
- property OnDrawThumb: TMMThumbDrawEvent read FOnDrawThumb write FOnDrawThumb;
- property OnDrawGroove: TMMGrooveDrawEvent read FOnDrawGroove write FOnDrawGroove;
- property OnMouseEnter: TNotifyEvent read FOnMouseEnter write FOnMouseEnter;
- property OnMouseLeave: TNotifyEvent read FOnMouseLeave write FOnMouseLeave;
- property OnGetFocus: TNotifyEvent read FOnGetFOcus write FOnGetFocus;
- property OnLostFocus: TNotifyEvent read FOnLostFocus write FOnLostFocus;
- property TabStop default True;
- property Width default 200;
- property Height default 40;
- property Groove: TMMBevel read FGroove write SetGroove;
- property FocusAction: TMMFocusAction read FFocusAction write SetFocusAction default faFocusRect;
- property FocusColor: TColor index 0 read FFocusColor write SetColors default clActiveCaption;
- property GrooveColor: TColor index 1 read FGrooveColor write SetColors default clBtnFace;
- property ThumbColor: TColor index 2 read FThumbColor write SetColors default clBtnFace;
- property DisabledColor: TColor index 3 read FDisabledColor write SetColors default clWhite;
- property HandCursor: Boolean read FHandCursor write FHandCursor default False;
- property ThumbCursor: TCursor read FThumbCursor write FThumbCursor default crDefault;
- property MinValue: Longint read FMin write SetMin default 0;
- property MaxValue: Longint read FMax write SetMax default 10;
- property LineSize: Integer read FLineSize write FLineSize default 1;
- property PageSize: Integer read FPageSize write FPageSize default 5;
- property Orientation: TMMOrientation read FOrientation write SetOrientation default orHorizontal;
- property Position: Longint read GetPosition write SetPosition default 0;
- property GrooveSize: Byte read FGrooveSize write SetGrooveSize default 3;
- property GrooveStyle: TMMGrooveStyle read FGrooveStyle write SetGrooveStyle default gsRect;
- property ThumbWidth: Byte index 0 read FThumbWidth write SetThumbSize default 11;
- property ThumbHeight: Byte index 1 read FThumbHeight write SetThumbSize default 23;
- property ThumbStyle: TMMThumbStyle read FThumbStyle write SetThumbStyle default tsRect;
- property ThumbBorder: Boolean read FThumbBorder write SetThumbBorder default True;
- property ScaleDistance: Integer read FScaleDistance write SetScaleDist default 10;
- property ScalePosition: TMMScalePos read FScalePos write SetScalePos default spBelowOrRight;
- property Scale: TMMScale read FScale write SetScale;
- property PicLeft: TBitmap read FPicLeft write SetPicLeft;
- property PicRight: TBitmap read FPicRight write SetPicRight;
- property Logarithmic: Boolean read FLogMode write SetLogMode default False;
- property Sensitivity: Integer read FSensitivity write SetSensitivity default -24;
- end;
- {-- TMMSlider ---------------------------------------------------------}
- TMMSlider = class(TMMCustomSlider)
- published
- property OnEnter;
- property OnExit;
- property OnKeyDown;
- property OnKeyPress;
- property OnKeyUp;
- property OnChange;
- property OnTrack;
- property OnTrackEnd;
- property OnDrawThumb;
- property OnDrawGroove;
- property OnMouseEnter; { Added January, 30 2000 }
- property OnMouseLeave; { Added January, 30 2000 }
- property OnGetFocus; { Added January, 30 2000 }
- property OnLostFocus; { Added January, 30 2000 }
- property Align;
- property Visible;
- property Color;
- property Enabled;
- property ParentShowHint;
- property PopupMenu;
- property ShowHint;
- property TabStop;
- property TabOrder;
- property Width;
- property Height;
- property Bevel;
- property Groove;
- property FocusAction;
- property FocusColor;
- property GrooveColor;
- property ThumbColor;
- property ThumbBorder;
- property DisabledColor;
- property HandCursor;
- property ThumbCursor;
- property MinValue;
- property MaxValue;
- property LineSize;
- property PageSize;
- property Orientation;
- property Position;
- property GrooveSize;
- property ThumbWidth;
- property ThumbHeight;
- property ThumbStyle;
- property GrooveStyle;
- property ScaleDistance;
- property ScalePosition;
- property Scale;
- property PicLeft;
- property PicRight;
- property Logarithmic;
- property Sensitivity;
- end;
- implementation
- const
- FocusTimer: TTimer = nil;
-
- {== TMMCustomSlider =====================================================}
- constructor TMMCustomSlider.Create(AOwner: TComponent);
- begin
- inherited Create(AOwner);
- FBitmap := nil;
- FGroove := TMMBevel.Create;
- FGroove.OnChange := GrooveChanged;
- Bevel.BorderWidth := 5;
- Height := 40;
- Width := 200;
- FFocusAction:= faFocusRect;
- FHandCursor := False;
- FThumbCursor := crDefault;
- FMin := 0;
- FMax := 10;
- FForceChange := False;
- FLineSize := 1;
- FPageSize := 5;
- FOrientation := orHorizontal;
- FPosition := 0;
- FGrooveSize := 3;
- FDragging := False;
- FDragVal := 0;
- FDragOffset := 0;
- ThumbStyle := tsRect;
- GrooveStyle := gsRect;
- FFocusColor := clActiveCaption;
- FGrooveColor := clBtnFace;
- FThumbColor := clBtnFace;
- FThumbBorder:= True;
- FDisabledColor := clWhite;
- FScalePos := spBelowOrRight;
- FScale := TMMScale.Create;
- FScale.OnChange := ScaleChanged;
- FScale.Visible := False;
- FScale.Origin := soOuter;
- FScale.Connect := True;
- FScaleDistance := 10;
- FFocusTime := False;
- FSensitivity := -24;
- FLogMode := False;
- FNeedTrackEnd := False;
- TabStop := True;
- FThumbWidth := 11;
- ThumbHeight := 23;
- FPicLeft := TBitmap.Create;
- FPicRight := TBitmap.Create;
- FPicLeft.OnChange := PicChanged;
- FPicRight.OnChange := PicChanged;
- ErrorCode := ComponentRegistered(InitCode, Self, ClassName);
- if (ErrorCode <> 0) then RegisterFailed(InitCode, Self , ClassName);
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- destructor TMMCustomSlider.Destroy;
- begin
- FGroove.OnChange := Nil;
- FGroove.Free;
- RemoveCacheBitmap(FBitmap);
- UpdateFocusTimer(False);
- FScale.Free;
- FPicLeft.Free;
- FPicRight.Free;
- inherited Destroy;
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.Change;
- begin
- if (csLoading in ComponentState) or
- (csReading in ComponentState) then exit;
- if assigned(FOnChange) then FOnChange(Self);
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.Track;
- begin
- FNeedTrackEnd := True;
- if assigned(FOnTrack) then FOnTrack(Self);
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.TrackEnd;
- begin
- if FNeedTrackEnd then
- begin
- if assigned(FOnTrackEnd) then FOnTrackEnd(Self);
- FNeedTrackEnd := False;
- end;
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.OwnerDrawThumb(aCanvas: TCanvas; aRect: TRect; Dragged,Focused: Boolean);
- begin
- if assigned(FOnDrawThumb) then FOnDrawThumb(Self,aCanvas,aRect,Dragged,Focused);
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.OwnerDrawGroove(aCanvas: TCanvas; var aRect: TRect);
- begin
- if Assigned(FOnDrawGroove) then FOnDrawGroove(Self,aCanvas,aRect);
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- Procedure TMMCustomSlider.SetGroove(aValue: TMMBevel);
- begin
- FGroove.Assign(aValue);
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.GrooveChanged(Sender: TObject);
- begin
- Changed;
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.ScaleChanged(Sender: TObject);
- begin
- Changed;
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.PicChanged(Sender: TObject);
- begin
- Changed;
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- function TMMCustomSlider.CalcClientRect: TRect;
- begin
- Result := BeveledRect;
- if not FPicLeft.Empty then
- if Orientation = orHorizontal then
- Inc(Result.Left,FPicLeft.Width)
- else
- Inc(Result.Top,FPicLeft.Height);
- if not FPicRight.Empty then
- if Orientation = orHorizontal then
- Dec(Result.Right,FPicRight.Width)
- else
- Dec(Result.Bottom,FPicRight.Height);
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.WMSetFocus(var Message: TWMSetFocus);
- begin
- inherited;
- { Added January, 30 2000 }
- if Assigned(FOnGetFocus) then FOnGetFocus(Self);
- if (FFocusAction <> faNone) then
- begin
- UpdateFocusTimer(True);
- Refresh;
- end;
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.WMKillFocus(var Message: TWMKillFocus);
- begin
- inherited;
- { Added January, 30 2000 }
- if Assigned(FOnLostFocus) then FOnLostFocus(Self);
- if (FFocusAction <> faNone) then
- begin
- UpdateFocusTimer(False);
- Refresh;
- end;
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.WMGetDlgCode(var Message: TWMGetDlgCode);
- begin
- Message.Result := DLGC_WANTARROWS;
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.CMEnabledChanged(var Message: TMessage);
- begin
- inherited;
- Refresh;
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.WMSize(var Message: TWMSize);
- begin
- if Height > Width then
- Orientation := orVertical else Orientation := orHorizontal;
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.Loaded;
- begin
- inherited Loaded;
- UpdateBitmap(Width,Height);
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.UpdateBitmap(aWidth,aHeight: integer);
- begin
- if (csLoading in ComponentState) or
- (csReading in ComponentState) then exit;
- RemoveCacheBitmap(FBitmap);
- FBitmap := LoadCacheBitmap(Max(aWidth,0),Max(aHeight,0));
- Invalidate;
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.SetBounds(aLeft, aTop, aWidth, aHeight: integer);
- begin
- if (Width <> aWidth) or (Height <> aHeight) or (FBitmap = nil) then
- begin
- UpdateBitmap(aWidth,aHeight);
- end;
- inherited SetBounds(aLeft, aTop, aWidth, aHeight);
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.SetColors(index: integer; aValue: TColor);
- begin
- case index of
- 0: if aValue = FFocusColor then exit else FFocusColor := aValue;
- 1: if aValue = FGrooveColor then exit else FGrooveColor := aValue;
- 2: if aValue = FThumbColor then exit else FThumbColor := aValue;
- 3: if aValue = FDisabledColor then exit else FDisabledColor := aValue;
- end;
- Refresh;
- {$IFDEF WIN32}
- {$IFDEF TRIAL}
- {$DEFINE _HACK1}
- {$I MMHACK.INC}
- {$ENDIF}
- {$ENDIF}
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.SetThumbBorder(aValue: Boolean);
- begin
- if (aValue <> FThumbBorder) then
- begin
- FThumbBorder := aValue;
- Invalidate;
- end;
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.SetThumbSize(index: integer; aValue: Byte);
- begin
- case Index of
- 0: if (aValue = FThumbWidth) then exit else FThumbWidth := aValue;
- 1: if (aValue = FThumbHeight) then exit else FThumbHeight := aValue;
- end;
- HalfTH := FThumbHeight div 2;
- HalfTW := FThumbWidth div 2;
- Refresh;
- {$IFDEF WIN32}
- {$IFDEF TRIAL}
- {$DEFINE _HACK2}
- {$I MMHACK.INC}
- {$ENDIF}
- {$ENDIF}
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.SetThumbStyle(aValue: TMMThumbStyle);
- begin
- if (FThumbStyle <> aValue) then
- begin
- FThumbStyle := aValue;
- Refresh;
- end;
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.SetGrooveStyle(aValue: TMMGrooveStyle);
- begin
- if (FGrooveStyle <> aValue) then
- begin
- FGrooveStyle := aValue;
- Refresh;
- end;
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.SetSensitivity(aValue: integer);
- var
- oldVal: integer;
- begin
- aValue:= MinMax(aValue, -96, -20);
- if aValue <> FSensitivity then
- begin
- oldVal := Position;
- FSensitivity:= aValue;
- Position := oldVal;
- end;
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.SetLogMode;
- var
- oldVal: integer;
- begin
- if (aValue <> FlogMode) then
- begin
- oldVal := Position;
- FLogMode := aValue;
- Position := oldVal;
- end;
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.SetMin(aValue: Longint);
- begin
- SetMinMax(aValue,FMax);
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.SetMax(aValue: Longint);
- begin
- SetMinMax(FMin,aValue);
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.SetMinMax(aMin,aMax: Longint);
- begin
- if (Fmin <> aMin) or (FMax <> aMax) then
- begin
- FMin := aMin;
- FMax := aMax;
- if not (csLoading in ComponentState) then
- FMax := Max(FMax,FMin+1);
- FForceChange := True;
- Position := MinMax(Position,FMin,FMax);
- FForceChange := False;
- Refresh;
- end;
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.SetOrientation(aValue: TMMOrientation);
- var
- Temp: integer;
- begin
- if (aValue <> FOrientation) then
- begin
- FOrientation := aValue;
- if (csDesigning in ComponentState) and
- not (csLoading in ComponentState) and
- not (csReading in ComponentState) then
- begin
- { exchange Thumb sizes }
- Temp := ThumbWidth;
- ThumbWidth := ThumbHeight;
- ThumbHeight := Temp;
- if (isVert and (Width > Height)) or
- (not isVert and (Height > Width)) then
- SetBounds(Left,Top,Height,Width);
- end;
- Refresh;
- end;
- {$IFDEF WIN32}
- {$IFDEF TRIAL}
- {$DEFINE _HACK2}
- {$I MMHACK.INC}
- {$ENDIF}
- {$ENDIF}
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- function TMMCustomSlider.UpdatePosition(aValue: Longint): Boolean;
- var
- aRect: TRect;
- begin
- if (aValue <> FPosition) or FForceChange then
- begin
- Result := True;
- FPosition := MinMax(aValue,FMin,FMax);
- if not (csDesigning in ComponentState) and
- not (csLoading in ComponentState) then
- begin
- WhereIsThumb(CalcClientRect,aRect);
- if (aRect.Left <> FThumbRect.Left) or (aRect.Top <> FThumbRect.Top) or
- (aRect.Right <> FThumbRect.Right) or (aRect.Bottom <> FThumbRect.Bottom) then
- begin
- Refresh;
- end;
- Change;
- end
- else Refresh;
- end
- else Result := False;
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.SetPosition(aValue: Longint);
- var
- aPos: Float;
- begin
- if FLogMode and (aValue <> 0) then
- begin
- aPos:= Log10(aValue/(FMax-FMin))*20 + -FSensitivity;
- aPos:= MinMax(Round(aPos*(FMax-FMin)/-FSensitivity),FMin,FMax);
- aValue := Round(aPos);
- end;
- UpdatePosition(aValue);
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- function TMMCustomSlider.GetPosition: Longint;
- var
- aPos: Float;
- begin
- aPos := MinMax(FPosition,FMin,FMax);
- if FLogMode and (aPos <> 0) then
- begin
- aPos:= Pow(10,(aPos*(-FSensitivity)/(FMax-FMin)-(-FSensitivity))/20)*(FMax-FMin);
- end;
- Result := MinMax(Round(aPos),FMin,FMax);
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.SetGrooveSize(aValue: Byte);
- begin
- If (aValue >= 0) then
- begin
- FGrooveSize := aValue;
- Refresh;
- end;
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.SetScaleDist(aValue: Integer);
- begin
- if (aValue <> FScaleDistance) then
- begin
- FScaleDistance := aValue;
- Refresh;
- end;
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.SetScalePos(aValue: TMMScalePos);
- begin
- if (aValue <> FScalePos) then
- begin
- FScalePos := aValue;
- Refresh;
- end;
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.SetScale(Value: TMMScale);
- begin
- FScale.Assign(Value);
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.TimerAction(Sender: TObject);
- begin
- if not FDragging then
- begin
- FFocusTime := not FFocusTime;
- Refresh;
- end
- else FFocusTime := True;
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.UpdateFocusTimer(Enable: Boolean);
- begin
- if (FocusTimer <> nil) and (FocusTimer.Owner = Self) then
- begin
- FocusTimer.Enabled := False;
- FocusTimer.Free;
- FocusTimer := nil;
- FFocusTime := False;
- end;
- if not (csDesigning in ComponentState) then
- begin
- if Enable and Focused and (FocusAction in [faFocusThumb,faAll]) then
- begin
- if (FocusTimer = nil) then FocusTimer := TTimer.Create(Self);
- FocusTimer.OnTimer := TimerAction;
- FocusTimer.Interval := 500;
- FocusTimer.Enabled := True;
- end;
- end;
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.SetFocusAction(aValue: TMMFocusAction);
- begin
- if (FFocusAction <> aValue) then
- begin
- FFocusAction := aValue;
- UpdateFocusTimer(Enabled);
- Refresh;
- end;
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- function TMMCustomSlider.NewPosition(WhereX,WhereY: Integer): integer;
- var
- aHeight,aWidth: Integer;
- begin
- { Calculate the nearest position to where the mouse is located }
- with CalcClientRect do
- begin
- aHeight := Bottom - Top - FThumbHeight;
- aWidth := Right - Left -FThumbWidth;
- WhereY := WhereY - Top - HalfTH;
- WhereX := WhereX - Left - HalfTW;
- end;
- if IsVert then
- Result := Round(((aHeight-WhereY)/aHeight)*(FMax-FMin)+FMin)
- else
- Result := Round((WhereX/aWidth)*(FMax-FMin)+FMin);
- Result := Min(Max(Result,FMin),FMax);
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- function TMMCustomSlider.IsVert: Boolean;
- begin
- IsVert := (Orientation = orVertical);
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.KeyDown(var Key: Word; Shift: TShiftState);
- var
- Pos : Integer;
- begin
- Pos := FPosition; { To avoid compiler warning }
- try
- case Key of
- VK_NEXT : if (FPosition-FPageSize) > FMin then
- Pos := FPosition - FPageSize else Pos := FMin;
- VK_PRIOR: if (FPosition+FPageSize) < FMax then
- Pos := FPosition + FPageSize else Pos := FMax;
- VK_END : if IsVert then Pos := FMin else Pos := FMax;
- VK_HOME : if IsVert then Pos := FMax else Pos := FMin;
- VK_LEFT,
- VK_DOWN : if FPosition > FMin then Pos := FPosition - FLineSize;
- VK_UP,
- VK_RIGHT: if FPosition < FMax then Pos := FPosition + FLineSize;
- else exit;
- end;
- if UpdatePosition(Pos) then Track;
- finally
- inherited KeyDown(Key,Shift);
- end;
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.KeyUp(var Key: Word; Shift: TShiftState);
- begin
- TrackEnd;
- inherited KeyUp(Key,Shift);
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
- begin
- {$IFNDEF BUILD_ACTIVEX}
- SetFocus;
- {$ELSE}
- Windows.SetFocus(Handle);
- {$ENDIF}
- if PtInRect(FThumbRect,Point(X,Y)) then
- begin
- if (Button = mbLeft) then FDragging := True;
- SetThumbCursor(True);
- end;
- if (Button = mbLeft) then
- begin
- if IsVert then
- FDragOffset := Y
- else
- FDragOffset := X;
- FDragVal := FPosition;
- if not FDragging then
- begin
- if not UpdatePosition(NewPosition(X,Y)) then
- Invalidate;
- end
- else Invalidate;
- Track;
- end;
- inherited MouseDown(Button, Shift, X, Y);
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.MouseMove(Shift: TShiftState; X, Y: Integer);
- var
- aPos,aWidth,aHeight: integer;
- begin
- if not FDragging then
- begin
- {$IFDEF WIN32}
- SetThumbCursor(ptInRect(FThumbRect,Point(X,Y)));
- {$ELSE}
- SetThumbCursor(ptInRect(FThumbRect,Point(X,Y)) or FDragging);
- {$ENDIF}
- end;
- {Is the left mouse button down and dragging the thumb bar?}
- if (ssLeft in Shift) and FDragging then
- begin
- with CalcClientRect do
- begin
- aHeight := Bottom - Top - FThumbHeight;
- aWidth := Right - Left - FThumbWidth;
- end;
- if IsVert then
- aPos := MulDiv(FDragOffset-Y,FMax-FMin,aHeight)
- else
- aPos := MulDiv(X-FDragOffset,FMax-FMin,aWidth);
- aPos := Min(Max(FDragVal+aPos,FMin),FMax);
- if UpdatePosition(aPos) then Track;
- end;
- inherited MouseMove(Shift, X, Y);
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
- begin
- if (Button = mbLeft) then
- begin
- FDragging := False;
- Refresh;
- TrackEnd;
- end;
- inherited MouseUp(Button, Shift, X, Y);
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.WhereIsThumb(const ClientRect: TRect; var aRect: Trect);
- var
- Each : Real;
- ThumbX,ThumbY : Integer;
- AWidth, AHeight : Integer ;
- begin
- AWidth := ClientRect.Right - ClientRect.Left ;
- AHeight := ClientRect.Bottom - ClientRect.Top ;
- { Calculate where to paint the thumb bar - store in aRect }
- if IsVert then
- begin
- Each := (AHeight-FThumbHeight)/(FMax-FMin);
- ThumbY := AHeight-Round(Each*(FPosition-FMin))-FThumbHeight;
- ThumbY := ClientRect.Top + Max(0,Min(AHeight-FThumbHeight,ThumbY));
- if Scale.Visible and (FScalePos = spBelowOrRight) then
- ThumbX := ClientRect.Left
- else if Scale.Visible and (FScalePos = spAboveOrLeft) then
- ThumbX := ClientRect.Left + AWidth-ThumbWidth
- else
- ThumbX := ClientRect.Left + AWidth div 2 - HalfTW;
- end
- else
- begin
- Each := (AWidth-FThumbWidth)/(FMax-FMin);
- ThumbX := Round(Each*(FPosition-FMin));
- ThumbX := ClientRect.Left + Max(0,Min(AWidth-FThumbWidth,ThumbX));
- if Scale.Visible and (FScalePos = spBelowOrRight) then
- ThumbY := ClientRect.Top
- else if Scale.Visible and (FScalePos = spAboveOrLeft) then
- ThumbY := ClientRect.Top + AHeight-ThumbHeight
- else
- ThumbY := ClientRect.Top + AHeight div 2 - HalfTH;
- end;
- aRect := Rect(ThumbX,ThumbY,ThumbX+FThumbWidth,ThumbY+FThumbHeight);
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.DrawScale(Canvas: TCanvas; aRect: TRect);
- begin
- if Scale.Visible then
- with Scale do
- begin
- MinValue := Self.MinValue;
- MaxValue := Self.MaxValue;
- end
- else Exit;
- Scale.Canvas := Canvas;
- if isVert then
- begin
- Inc(aRect.Top, HalfTH);
- Dec(aRect.Bottom, HalfTH);
- if (FScalePos = spAboveOrLeft) or (FScalePos = spBoth) then
- Scale.DrawRect(Canvas,Rect(aRect.Left-Scale.ScaleHeight-FScaleDistance,
- aRect.Top,aRect.Left-FScaleDistance,aRect.Bottom),True);
- if (FScalePos = spBelowOrRight) or (FScalePos = spBoth) then
- Scale.DrawRect(Canvas,Rect(aRect.Right+FScaleDistance,
- aRect.Top,aRect.Right+Scale.ScaleHeight+FScaleDistance,
- aRect.Bottom),False);
- end
- else
- begin
- Inc(aRect.Left, HalfTW);
- Dec(aRect.Right, HalfTW);
- if (FScalePos = spAboveOrLeft) or (FScalePos = spBoth) then
- Scale.DrawRect(Canvas,Rect(aRect.Left,aRect.Top-Scale.ScaleHeight-FScaleDistance-1,
- aRect.Right,aRect.Top-FScaleDistance-1),True);
- if (FScalePos = spBelowOrRight) or (FScalePos = spBoth) then
- Scale.DrawRect(Canvas,Rect(aRect.Left,aRect.Bottom+FScaleDistance+1,
- aRect.Right,aRect.Bottom+Scale.ScaleHeight+FScaleDistance+1),False);
- end;
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.DrawTrench(Canvas: TCanvas; aRect: TRect);
- var
- aWidth,aHeight: integer;
- X1,Y1,X2,Y2 : integer;
- R1,R2,R3 : TRect;
- begin
- {This procedure simply draws the slot that the thumb bar will travel through}
- {including the tick-marks. The bar itself is not drawn.}
- {Calculate the corners of the trench dependant on orientation}
- aWidth := aRect.Right-aRect.Left;
- aHeight:= aRect.Bottom-aRect.Top;
- with Canvas do
- begin
- if IsVert then
- begin
- if Scale.Visible and (FScalePos = spBelowOrRight) then
- X1 := aRect.Left+HalfTW-FGroove.BevelExtend -(FGrooveSize div 2)
- else if Scale.Visible and (FScalePos = spAboveOrLeft) then
- X1 := aRect.Right-HalfTW-FGroove.BevelExtend-(FGrooveSize div 2)-1
- else
- X1 := aRect.Left+(aWidth div 2) - FGroove.BevelExtend -(FGrooveSize div 2);
- X2 := X1 + 2*FGroove.BevelExtend + FGrooveSize;
- Y1 := aRect.Top;
- Y2 := aRect.Bottom;
- end
- else
- begin
- if Scale.Visible and (FScalePos = spBelowOrRight) then
- Y1 := aRect.Top+HalfTH-FGroove.BevelExtend -(FGrooveSize div 2)
- else if Scale.Visible and (FScalePos = spAboveOrLeft) then
- Y1 := aRect.Bottom-HalfTH-FGroove.BevelExtend-(FGrooveSize div 2)-1
- else
- Y1 := aRect.Top+(aHeight div 2)-FGroove.BevelExtend-(FGrooveSize div 2);
- Y2 := Y1 + 2*FGroove.BevelExtend+ FGrooveSize;
- X1 := aRect.Left;
- X2 := aRect.Right;
- end;
- R1 := Rect(X1,Y1,X2,Y2);
- DrawScale(Canvas,R1);
- R2 := DrawGroove(Canvas,R1);
- {Now do a filled rectangle in the center if the control has focus}
- Brush.Color := FGrooveColor;
- if Focused then
- begin
- if (FFocusAction = faFocusRect) or (FFocusAction = faAll) then
- begin
- R3 := aRect;
- if ((Bevel.BorderWidth > 0) and (Bevel.BevelInner = bvNone)) or
- (Bevel.BorderSpace > 0) then
- InflateRect(R3,1,1);
- DrawFocusRect(R3);
- end;
- if (FFocusAction = faFocusColor) or (FFocusAction = faAll) then
- Brush.Color := FocusColor;
- end;
- FillRect(R2);
- end;
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.DrawThumb(Canvas: TCanvas; aRect: Trect);
- var
- X, Y: integer;
- Clr: TColor;
- begin
- with Canvas,aRect do
- begin
- case FThumbStyle of
- tsOwnerDraw: OwnerDrawThumb(Canvas, aRect, FDragging, FFocusTime);
- tsRect:
- begin
- if FThumbBorder then
- Frame3D(Canvas, aRect, clWindowFrame, clWindowFrame, 1)
- else
- begin
- Pen.Color := clWindowFrame;
- MoveTo(aRect.Left,aRect.Bottom-1);
- LineTo(aRect.Right-1,aRect.Bottom-1);
- LineTo(aRect.Right-1,aRect.Top-1);
- dec(aRect.Right);
- dec(aRect.Bottom);
- end;
- Frame3D(Canvas, aRect, clBtnHighlight, clBtnShadow, 1);
- Pixels[aRect.Right,aRect.Top-1] := clBtnHighLight;
- Pixels[aRect.Left-1,aRect.Bottom] := clBtnHighLight;
- Brush.Color := FThumbColor;
- FillRect(aRect);
- if not Enabled or FFocusTime then
- begin
- if not Enabled then
- Clr := FDisabledColor
- else
- Clr := clBlack;
- for Y := aRect.Top to aRect.Bottom-1 do
- for X := aRect.Left to aRect.Right-1 do
- if (Y mod 2) = (X mod 2) then
- Pixels[X, Y] := Clr;
- end;
- end;
- end;
- end;
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- function TMMCustomSlider.DrawPics(Canvas: TCanvas; aRect: TRect): TRect;
- var
- AWidth, AHeight: Integer;
- OrigX, OrigY : Integer;
- procedure DrawPic(Pic: TBitmap; R: TRect);
- var
- X, Y: Integer;
- begin
- X := R.Left + (R.Right - R.Left - Pic.Width) div 2;
- Y := R.Top + (R.Bottom - R.Top - Pic.Height) div 2;
- Canvas.BrushCopy(Bounds(X,Y,Pic.Width,Pic.Height),Pic,Bounds(0,0,Pic.Width,Pic.Height),Pic.TransparentColor);
- end;
- begin
- AWidth := aRect.Right - aRect.Left;
- AHeight:= aRect.Bottom- aRect.Top;
- OrigY := AHeight div 2 + aRect.Top;
- if Orientation = orHorizontal then
- if Scale.Visible then
- if ScalePosition = spAboveOrLeft then
- OrigY := aRect.Bottom - FThumbHeight div 2
- else if ScalePosition = spBelowOrRight then
- OrigY := aRect.Top + FThumbHeight div 2;
-
- OrigX := AWidth div 2 + aRect.Left;
- if Orientation = orVertical then
- if Scale.Visible then
- if ScalePosition = spAboveOrLeft then
- OrigX := aRect.Right - FThumbWidth div 2
- else if ScalePosition = spBelowOrRight then
- OrigX := aRect.Left + FThumbWidth div 2;
- if not FPicLeft.Empty then
- if Orientation = orHorizontal then
- begin
- DrawPic(FPicLeft,Bounds(aRect.Left,OrigY-FPicLeft.Height div 2,FPicLeft.Width,FPicLeft.Height));
- Inc(aRect.Left,FPicLeft.Width);
- end
- else
- begin
- DrawPic(FPicLeft,Bounds(OrigX-FPicLeft.Width div 2,aRect.Top,FPicLeft.Width,FPicLeft.Height));
- Inc(aRect.Top,FPicLeft.Height);
- end;
- if not FPicRight.Empty then
- if Orientation = orHorizontal then
- begin
- DrawPic(FPicRight,Bounds(aRect.Right-FPicRight.Width,OrigY-FPicRight.Height div 2,
- FPicRight.Width,FPicRight.Height));
- Dec(aRect.Right,FPicRight.Width);
- end
- else
- begin
- DrawPic(FPicRight,Bounds(OrigX-FPicRight.Width div 2,aRect.Bottom-FPicRight.Height,
- FPicRight.Width,FPicRight.Height));
- Dec(aRect.Bottom,FPicRight.Height);
- end;
- Result := aRect;
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.SetPicLeft(Value: TBitmap);
- begin
- FPicLeft.Assign(Value);
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.SetPicRight(Value: TBitmap);
- begin
- FPicRight.Assign(Value);
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.Paint;
- var
- aRect: TRect;
- begin
- if (FBitmap = nil) then exit;
- with FBitmap do
- begin
- { draw the Bevel and fill the area }
- aRect := Bevel.PaintBevel(Canvas, ClientRect,True);
- with FBitmap.Canvas do
- begin
- Brush.Color := Color;
- Brush.Style := bsSolid;
- FillRect(aRect);
- aRect := DrawPics(Canvas,aRect);
- WhereIsThumb(aRect,FThumbRect);
- DrawTrench(Canvas, aRect);
- DrawThumb(Canvas, FThumbRect);
-
- end;
- end;
- Canvas.Draw(0,0,FBitmap);
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- function TMMCustomSlider.DrawGroove(Canvas: TCanvas; aRect: TRect): TRect;
- begin
- if FGrooveStyle = gsOwnerDraw then
- begin
- InflateRect(aRect,0,-FGroove.BevelExtend);
- OwnerDrawGroove(Canvas,aRect);
- Result := aRect;
- end
- else
- Result := FGroove.PaintBevel(Canvas, aRect, True);
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.SetThumbCursor(AtThumb: Boolean);
- begin
- if AtThumb then
- if FHandCursor then
- SetCursor(Screen.Cursors[crsHand5])
- else
- SetCursor(Screen.Cursors[ThumbCursor])
- else
- SetCursor(Screen.Cursors[Cursor]);
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.CMMouseEnter(var msg: TMessage);
- begin
- if Assigned(FOnMouseEnter) then FOnMouseEnter(Self);
- end;
- {-- TMMCustomSlider -----------------------------------------------------}
- procedure TMMCustomSlider.CMMouseLeave(var msg: TMessage);
- begin
- if Assigned(FOnMouseLeave) then FOnMouseLeave(Self);
- end;
- end.