cxtrackbar.pas

来自「胜天进销存源码,国产优秀的进销存」· PAS 代码 · 共 1,699 行 · 第 1/5 页

PAS
1,699
字号
            ACanvas.MoveTo(X, ThumbRect.Top - (BetweenTrackAndTick + FCalcTickSize));
            ACanvas.LineTo(X, ThumbRect.Top - BetweenTrackAndTick);
          end;
        end;
      end;
    end;
  finally
    if AEditProperties.TextOrientation = tbtoVertical then RemoveIndirectFont;
  end;
end;

procedure TcxCustomTrackBarViewInfo.DrawThumb(ACanvas: TcxCanvas);
const
  ATrackBarStates2BtnStates: array[1..5] of TcxButtonState =
    (cxbsNormal, cxbsHot, cxbsPressed, cxbsHot, cxbsDisabled);
  ATrackBarTicks2TicksAlign: array[TcxTrackBarTickMarks] of TcxTrackBarTicksAlign =
    (tbtaBoth, tbtaUp, tbtaDown);

 function GetThumbRealColor: TColor;
  begin
    case TrackBarState of
      TUS_DISABLED:
        Result := clBtnShadow;
      TUS_PRESSED, TUS_HOT:
        if LookAndFeel.Kind in [lfUltraFlat, lfOffice11] then
          Result := GetEditButtonHighlightColor(
            TrackBarState = TUS_PRESSED, LookAndFeel.Kind = lfOffice11)
        else
          Result :=  TcxCustomTrackBarProperties(EditProperties).ThumbHighLightColor;
      else
        Result := TcxCustomTrackBarProperties(EditProperties).ThumbColor;
    end;
  end;

var
  AEditProperties: TcxCustomTrackBarProperties;
  FDrawThumbRect: TRect;
  FTheme: TdxTheme;
  FThumbRealColor: TColor;
begin
  AEditProperties := TcxCustomTrackBarProperties(EditProperties);
  if Painter <> nil then
  begin
    Painter.DrawTrackBarThumb(ACanvas, ThumbRect,
      ATrackBarStates2BtnStates[TrackBarState],
      AEditProperties.Orientation = tboHorizontal,
      ATrackBarTicks2TicksAlign[AEditProperties.TickMarks]);
  end
  else
    if AreVisualStylesMustBeUsed(LookAndFeel.NativeStyle, totTrackBar) then
    begin
      FTheme := OpenTheme(totTrackBar);
      DrawThemeBackground(FTheme, ACanvas.Handle, GetThumbThemeType,
        FTrackBarState, ThumbRect);
    end
    else
    begin
      FThumbRealColor := GetThumbRealColor;
      if NeedPointer then
      begin
        if (AEditProperties.TickMarks = cxtmBottomRight) then
          DrawBottomRightThumb(ACanvas, ThumbRect, AEditProperties.Orientation, ThumbSize, LookAndFeel.Kind, FThumbRealColor)
         else
          DrawTopLeftThumb(ACanvas, ThumbRect, AEditProperties.Orientation, ThumbSize, LookAndFeel.Kind, FThumbRealColor);
      end
      else
      begin
        case LookAndFeel.Kind of
          lfStandard:
          begin
            FDrawThumbRect := DrawBounds(ACanvas, ThumbRect, clWindow, clWindowFrame);
            FDrawThumbRect := DrawBounds(ACanvas, FDrawThumbRect, clBtnFace, clBtnShadow);
          end;
          lfFlat:
            FDrawThumbRect := DrawBounds(ACanvas, ThumbRect, clWindow, clBtnShadow);
          lfUltraFlat, lfOffice11:
            FDrawThumbRect := DrawBounds(ACanvas, ThumbRect, clWindowFrame, clWindowFrame);
        end;

        Inc(FDrawThumbRect.Right);
        Inc(FDrawThumbRect.Bottom);
        ACanvas.Brush.Color := FThumbRealColor;
        ACanvas.FillRect(FDrawThumbRect);
      end;
    end;
end;

function TcxCustomTrackBarViewInfo.DrawingThumbRectToRealThumbRect(
  ACanvas: TcxCanvas): TRect;
var
  AThumbSize: TSize;
begin
  Result := ThumbRect;
  if AreVisualStylesMustBeUsed(NativeStyle, totTrackBar) then
  begin
    GetThemePartSize(OpenTheme(totTrackBar), ACanvas.Handle,
      GetThumbThemeType, TrackBarState, Result, TS_DRAW, AThumbSize);
    Result.Left := Result.Left +
      (RectWidth(Result) - AThumbSize.cx) div 2;
    Result.Top := Result.Top +
      (RectHeight(Result) - AThumbSize.cy) div 2;
    Result.Right := Result.Left + AThumbSize.cx;
    Result.Bottom := Result.Top + AThumbSize.cy;
  end
  else
    ExtendRect(Result, Rect(0, 0, -1, -1));
  OffsetRect(Result, RealTrackBarRect.Left, RealTrackBarRect.Top);
end;

function TcxCustomTrackBarViewInfo.GetThumbThemeType: Byte;
const
  AThumbThemeParts: array[TcxTrackBarTickMarks, Boolean] of Byte = (
    (TKP_THUMB, TKP_THUMBVERT),
    (TKP_THUMBTOP, TKP_THUMBLEFT),
    (TKP_THUMBBOTTOM, TKP_THUMBRIGHT)
  );
begin
  Result := AThumbThemeParts[TcxCustomTrackBarProperties(EditProperties).TickMarks,
    TcxCustomTrackBarProperties(EditProperties).Orientation = tboVertical];
end;

function TcxCustomTrackBarViewInfo.GetEdit: TcxCustomTrackBar;
begin
  Result := TcxCustomTrackBar(FEdit);
end;

{ TcxCustomTrackBarViewData }

procedure TcxCustomTrackBarViewData.CalculateTBViewInfoProps(AViewInfo: TcxCustomEditViewInfo);
begin
  with TcxCustomTrackBarViewInfo(AViewInfo) do
  begin
    TrackBarBorderWidth := Properties.BorderWidth;
    TrackSize := Properties.TrackSize;
    SelectionStart := Properties.SelectionStart;
    SelectionEnd := Properties.SelectionEnd;
    if not Enabled then
      TickColor := clBtnShadow
    else
      TickColor := Properties.TickColor;
    ThumbHeight := Properties.ThumbHeight;
    ThumbWidth := Properties.ThumbWidth;
  end;
end;

function TcxCustomTrackBarViewData.GetTopLeftTickSize(
  ACanvas: TcxCanvas; AViewInfo: TcxCustomTrackBarViewInfo;
  ALeftTop: Boolean): Integer;
var
  ACalcNumValue: string;
begin
  Result := 0;
  if ((Properties.TickMarks <> cxtmBottomRight) and (ALeftTop = True)) or
    (Properties.TickMarks <> cxtmTopLeft) and (ALeftTop = False) then
    case Properties.TickType of
      tbttTicks: Result := Properties.TickSize + BetweenTrackAndTick;
      tbttNumbers, tbttValueNumber:
      begin
        if ((Properties.Orientation = tboHorizontal) and
          (Properties.TextOrientation = tbtoHorizontal)) or
          ((Properties.Orientation = tboVertical) and
          (Properties.TextOrientation = tbtoVertical)) then
          Result := ACanvas.TextHeight(IntToStr(Properties.Min))
        else
        begin
          if Length(IntToStr(Properties.Min)) > Length(IntToStr(Properties.Max)) then
            ACalcNumValue := IntToStr(Properties.Min)
          else
            ACalcNumValue := IntToStr(Properties.Max);
          Result := ACanvas.TextWidth(ACalcNumValue);
        end;
      end;
    end;
  if AViewInfo.TrackBarBorderWidth = 0 then
    Inc(Result, 1);
end;

procedure TcxCustomTrackBarViewData.CalculateTrackBarRect(
  AViewInfo: TcxCustomTrackBarViewInfo);
begin
  AViewInfo.RealTrackBarRect := AViewInfo.ClientRect;
  AViewInfo.TrackBarRect := AViewInfo.ClientRect;
  OffsetRect(AViewInfo.TrackBarRect, -Bounds.Left, -Bounds.Top);
  if (RectWidth(AViewInfo.TrackBarRect) div 2) < AViewInfo.TrackBarBorderWidth then
    AViewInfo.TrackBarBorderWidth := RectWidth(AViewInfo.TrackBarRect) div 2;
  if (RectHeight(AViewInfo.TrackBarRect) div 2) < AViewInfo.TrackBarBorderWidth then
    AViewInfo.TrackBarBorderWidth := RectHeight(AViewInfo.TrackBarRect) div 2;
end;

procedure TcxCustomTrackBarViewData.CalculateTrackZoneRect(ACanvas: TcxCanvas;
  AViewInfo: TcxCustomTrackBarViewInfo);
var
  FTopLeftIndent, FBottomRightIndent: Integer;
  FTrackZoneSize, FRealTrackZoneSize: Integer;
  FCustomRect: TRect;
begin
  FTopLeftIndent := GetTopLeftTickSize(ACanvas, AViewInfo, True);
  FBottomRightIndent := GetTopLeftTickSize(ACanvas, AViewInfo, False);
  if Properties.Orientation = tboHorizontal then
    FTrackZoneSize := RectHeight(AViewInfo.TrackBarRect) - FTopLeftIndent - FBottomRightIndent
  else
    FTrackZoneSize := RectWidth(AViewInfo.TrackBarRect) - FTopLeftIndent - FBottomRightIndent;
  FRealTrackZoneSize := FTrackZoneSize;
  if FTrackZoneSize < 10 then FRealTrackZoneSize := 10;
  if FTrackZoneSize > 21 then FRealTrackZoneSize := 21;

  if (Properties.ThumbType = cxttCustom) and
    IsOnGetThumbRectEventAssigned then
  begin
    DoOnGetThumbRect(FCustomRect);
//    Properties.OnGetThumbRect(Properties, FCustomRect);
    if (Properties.Orientation = tboHorizontal) and
      (RectHeight(FCustomRect) > FRealTrackZoneSize) then
      FRealTrackZoneSize := RectHeight(FCustomRect);
    if (Properties.Orientation = tboVertical) and
      (RectWidth(FCustomRect) > FRealTrackZoneSize) then
      FRealTrackZoneSize := RectWidth(FCustomRect);
  end;

  AViewInfo.FromBorderIndent := FromBorderIndent;
  if AViewInfo.Painter <> nil then
    AViewInfo.TrackSize := AViewInfo.Painter.TrackBarTrackSize
  else
    if Properties.AutoSize then
      AViewInfo.TrackSize := FRealTrackZoneSize div 2;

  if Properties.Orientation = tboHorizontal then
  begin
    AViewInfo.TrackZoneRect.Top := AViewInfo.TrackBarRect.Top +
      ((FTrackZoneSize - FRealTrackZoneSize) div 2) + FTopLeftIndent;
    AViewInfo.TrackZoneRect.Bottom := AViewInfo.TrackZoneRect.Top + FRealTrackZoneSize;
    AViewInfo.TrackZoneRect.Left := AViewInfo.TrackBarRect.Left +
      AViewInfo.TrackBarBorderWidth + AViewInfo.FromBorderIndent;
    AViewInfo.TrackZoneRect.Right := AViewInfo.TrackBarRect.Right -
      AViewInfo.TrackBarBorderWidth - AViewInfo.FromBorderIndent;
  end
  else
  begin
    AViewInfo.TrackZoneRect.Left := AViewInfo.TrackBarRect.Left +
      ((FTrackZoneSize - FRealTrackZoneSize) div 2) + FTopLeftIndent;
    AViewInfo.TrackZoneRect.Right := AViewInfo.TrackZoneRect.Left + FRealTrackZoneSize;
    AViewInfo.TrackZoneRect.Top := AViewInfo.TrackBarRect.Top +
      AViewInfo.TrackBarBorderWidth + AViewInfo.FromBorderIndent;
    AViewInfo.TrackZoneRect.Bottom := AViewInfo.TrackBarRect.Bottom -
      AViewInfo.TrackBarBorderWidth - AViewInfo.FromBorderIndent;
  end;
end;

procedure TcxCustomTrackBarViewData.CalculateTrackRect(
  AViewInfo: TcxCustomTrackBarViewInfo);
begin
  if Properties.Orientation = tboHorizontal then
  begin
    AViewInfo.TrackRect.Left := AViewInfo.TrackZoneRect.Left;
    AViewInfo.TrackRect.Right := AViewInfo.TrackZoneRect.Right;
    AViewInfo.TrackRect.Top := AViewInfo.TrackZoneRect.Top +
      (RectHeight(AViewInfo.TrackZoneRect) - AViewInfo.TrackSize) div 2;
    AViewInfo.TrackRect.Bottom := AViewInfo.TrackRect.Top + AViewInfo.TrackSize;
  end
  else
  begin
    AViewInfo.TrackRect.Top := AViewInfo.TrackZoneRect.Top;
    AViewInfo.TrackRect.Bottom := AViewInfo.TrackZoneRect.Bottom;
    AViewInfo.TrackRect.Left := AViewInfo.TrackZoneRect.Left +
      (RectWidth(AViewInfo.TrackZoneRect) - AViewInfo.TrackSize) div 2;
    AViewInfo.TrackRect.Right := AViewInfo.TrackRect.Left + AViewInfo.TrackSize;
  end;
  Properties.FTrackRect := AViewInfo.TrackRect;
end;

procedure TcxCustomTrackBarViewData.CalculateThumbSize(
  AViewInfo: TcxCustomTrackBarViewInfo);
var
  FMinMaxDiff: Integer;
  FTickOffset: Double;
  FThumbSize, FThumbLargeSize: Integer;
  FCustomRect: TRect;
  AThumbSize: TSize;
begin
  FMinMaxDiff := Properties.Max - Properties.Min;
  if FMinMaxDiff = 0 then FMinMaxDiff := 1;
  AViewInfo.NeedPointer := (Properties.TickMarks <> cxtmBoth);
  if (Properties.ThumbType = cxttCustom) and IsOnGetThumbRectEventAssigned then
  begin
    DoOnGetThumbRect(FCustomRect);
    if Properties.Orientation = tboHorizontal then
    begin
      FThumbSize := RectWidth(FCustomRect);
      FThumbLargeSize := RectHeight(FCustomRect);
    end
    else
    begin
      FThumbSize := RectHeight(FCustomRect);
      FThumbLargeSize := RectWidth(FCustomRect);
    end;
    AViewInfo.ThumbWidth := FThumbSize;
    AViewInfo.ThumbHeight := FThumbLargeSize;
  end
  else
    if AViewInfo.Painter <> nil then
    begin
      AThumbSize := AViewInfo.Painter.TrackBarThumbSize(Properties.Orientation = tboHorizontal);
      if Properties.Orientation = tboHorizontal then
      begin
        FThumbSize := AThumbSize.cx;
        FThumbLargeSize := Min(AThumbSize.cy, RectHeight(AViewInfo.TrackZoneRect));
      end
      else
      begin
        FThumbSize := AThumbSize.cy;
        FThumbLargeSize := Min(AThumbSize.cx, RectWidth(AViewInfo.TrackZoneRect));
      end;
    end
    else 
    begin 
      if Properties.AutoSize then
      begin
        FThumbSize := AViewInfo.TrackSize;
        if Properties.Orientation = tboHorizontal then
        begin
          if (FThumbSize mod 2) = 1 then Inc(FThumbSize);
        end
        else
        begin
          if (FThumbSize mod 2) = 1 then Dec(FThumbSize);
        end;
        FThumbLargeSize := FThumbSize * 2 - 1;
      end
      else
      begin
        FThumbSize := AViewInfo.ThumbWidth;
        FThumbLargeSize := AViewInfo.ThumbHeight - 1;
      end;
    end;
    
  if Properties.Orientation = tboHorizontal then
    FTickOffset := (RectWidth(AViewInfo.TrackRect) - FThumbSize) / FMinMaxDiff
  else
    FTickOffset := (RectHeight(AViewInfo.TrackRect) - FThumbSize) / FMinMaxDiff;
  AViewInfo.ThumbSize := FThumbSize;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?