⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 suitrackbar.pas

📁 SUIPack是一款为Delphi和C++Builder开发的所见即所得的界面增强VCL组件
💻 PAS
📖 第 1 页 / 共 2 页
字号:
                m_Timer := nil;
            end;

        Exit;
    end;

    UpdatePositionValue(X, Y, false);
    PlaceControl(m_Slider, GetSliderPosFromPosition());

    if m_Position <> m_LastPos then
    begin
        m_LastChange := m_Position - m_LastPos;
        if Assigned(m_OnChange) then
            m_OnChange(self);
        m_LastPos := m_Position;
    end;
end;

procedure TsuiTrackBar.OnSliderMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
    if m_Orientation = suiHorizontal then
        m_MouseDownPos := X
    else
        m_MouseDownPos := Y;
    m_bSlidingFlag := true;
    if SUICanFocus() and CanFocus and Enabled then
        SetFocus();

    SetCapture(Handle);
    if Assigned(OnMouseDown) then
        OnMouseDown(self, Button, Shift, m_Slider.Left + X, m_Slider.Top + Y);
end;

procedure TsuiTrackBar.MouseDown(Button: TMouseButton; Shift: TShiftState;
  X, Y: Integer);
begin
    inherited;

    if Button <> mbLeft then
        Exit;

    if m_Orientation = suiHorizontal then
    begin
        if X > GetSliderPosFromPosition().X then
            Position := Math.Min(Position + m_PageSize, GetPositionFromFromSliderPos(X, Y))
        else if X < GetSliderPosFromPosition().X then
            Position := Math.Max(Position - m_PageSize, GetPositionFromFromSliderPos(X, Y))
    end
    else
    begin
        if Y > GetSliderPosFromPosition().Y then
            Position := Math.Min(Position + m_PageSize, GetPositionFromFromSliderPos(X, Y))
        else if Y < GetSliderPosFromPosition().Y then
            Position := Math.Max(Position - m_PageSize, GetPositionFromFromSliderPos(X, Y))
    end;

    if m_Timer = nil then
    begin
        m_Timer := TTimer.Create(nil);
        m_Timer.OnTimer := OnTimer;
        m_Timer.Interval := 100;
        m_Timer.Enabled := true;
    end;
    if SUICanFocus() and CanFocus() and Enabled then
        SetFocus();
end;

procedure TsuiTrackBar.MouseUp(Button: TMouseButton; Shift: TShiftState; X,
  Y: Integer);
begin
    inherited;

    if m_Timer <> nil then
    begin
        m_Timer.Free();
        m_Timer := nil;
    end;

    m_bSlidingFlag := false;
    ReleaseCapture();
end;

function TsuiTrackBar.GetPositionFromFromSliderPos(X, Y: Integer): Integer;
begin
    if m_Orientation = suiHorizontal then
    begin
        if Width = m_Slider.Width then
            Result := 0
        else
            Result := Round((X - m_MouseDownPos) * (m_Max - m_Min) / (Width - m_Slider.Width)) + m_Min
    end
    else
    begin
        if Height = m_Slider.Height then
            Result := 0
        else
            Result := Round((Y - m_MouseDownPos) * (m_Max - m_Min) / (Height - m_Slider.Height)) + m_Min;
    end;
end;

procedure TsuiTrackBar.SetSize();
begin
    if m_Orientation = suiHorizontal then
        Height := Math.Max(m_BarImage.Height, m_Slider.Height)
    else
        Width := Math.Max(m_BarImage.Height, m_Slider.Width);
    Repaint();
end;

procedure TsuiTrackBar.WMERASEBKGND(var Msg: TMessage);
begin
    // do nothing
end;

procedure TsuiTrackBar.UpdatePicture;
var
    R : TRect;
    bFileTheme : boolean;
    OutUIStyle : TsuiUIStyle;
    Tmp : TBitmap;
begin
    if CustomPicture() then
        Exit;
    bFileTheme := UsingFileTheme(m_FileTheme, m_UIStyle, OutUIStyle);
    if m_Orientation = suiHorizontal then
    begin
        if bFileTheme then
        begin
            m_BarImage.Bitmap.Assign(m_FileTheme.GetBitmap(SKIN2_TRACKBAR));
            Tmp := m_FileTheme.GetBitmap(SKIN2_TRACKBARSLIDER);
            ScanColor(Tmp, m_FileTheme.GetColor(SKIN2_TRANSCOLOR), m_FileTheme.GetColor(SKIN2_FORMCOLOR));
            SliderImage.Bitmap.Assign(Tmp);
        end
        else
        begin
            GetInsideThemeBitmap(OutUIStyle, SUI_THEME_TRACKBAR_BAR, m_BarImage.Bitmap);
            GetInsideThemeBitmap(OutUIStyle, SUI_THEME_TRACKBAR_SLIDER, SliderImage.Bitmap);
        end;

        m_BarImageBuf.Width := Width;
        m_BarImageBuf.Height := m_BarImage.Height;
        R := Rect(0, 0, m_BarImageBuf.Width, m_BarImageBuf.Height);
        m_BarImageBuf.Canvas.Brush.Color := Color;
        m_BarImageBuf.Canvas.FillRect(R);
        SpitDrawHorizontal(m_BarImage.Bitmap, m_BarImageBuf.Canvas, R, false, Color);
    end
    else
    begin
        if bFileTheme then
        begin
            m_BarImage.Bitmap.Assign(m_FileTheme.GetBitmap(SKIN2_TRACKBAR));
            Tmp := m_FileTheme.GetBitmap(SKIN2_TRACKBARVSLIDER);
            ScanColor(Tmp, m_FileTheme.GetColor(SKIN2_TRANSCOLOR), m_FileTheme.GetColor(SKIN2_FORMCOLOR));
            SliderImage.Bitmap.Assign(Tmp);
        end
        else
        begin
            GetInsideThemeBitmap(OutUIStyle, SUI_THEME_TRACKBAR_BAR, m_BarImage.Bitmap);
            GetInsideThemeBitmap(OutUIStyle, SUI_THEME_TRACKBAR_SLIDER_V, SliderImage.Bitmap);
        end;

        m_BarImageBuf.Width := Height;
        m_BarImageBuf.Height := m_BarImage.Height;
        R := Rect(0, 0, m_BarImageBuf.Width, m_BarImageBuf.Height);
        m_BarImageBuf.Canvas.Brush.Color := Color;
        m_BarImageBuf.Canvas.FillRect(R);
        SpitDrawHorizontal(m_BarImage.Bitmap, m_BarImageBuf.Canvas, R, false, Color);
        RoundPicture(m_BarImageBuf);
    end;
    m_Slider.Transparent := SliderTransparent();
    m_Slider.AutoSize := true;
end;

procedure TsuiTrackBar.UpdateControl;
begin
    UpdatePicture();
    SetSize();
    UpdateSlider();
    Repaint();
end;

procedure TsuiTrackBar.UpdatePositionValue(X, Y : Integer; Update : Boolean);
var
    nPos : Integer;
begin
    nPos := GetPositionFromFromSliderPos(X, Y);

    if nPos > m_Max then
        nPos := m_Max
    else if nPos < m_Min then
        nPos := m_Min;
    if Update then
        Position := nPos
    else
        m_Position := nPos;
end;

procedure TsuiTrackBar.OnTimer(Sender: TObject);
var
    P : TPoint;
begin
    GetCursorPos(P);
    P := ScreenToClient(P);
    if m_Orientation = suiHorizontal then
    begin
        if P.X > GetSliderPosFromPosition().X then
            Position := Math.Min(Position + 3 * m_PageSize, GetPositionFromFromSliderPos(P.X, P.Y))
        else if P.X < GetSliderPosFromPosition().X then
            Position := Math.Max(Position - 3 * m_PageSize, GetPositionFromFromSliderPos(P.X, P.Y))
    end
    else
    begin
        if P.Y > GetSliderPosFromPosition().Y then
            Position := Math.Min(Position + m_PageSize, GetPositionFromFromSliderPos(P.X, P.Y))
        else if P.Y < GetSliderPosFromPosition().Y then
            Position := Math.Max(Position - m_PageSize, GetPositionFromFromSliderPos(P.X, P.Y))
    end;
end;

procedure TsuiTrackBar.SetPageSize(const Value: Integer);
begin
    if (Value < 0) or (Value > m_Max) then
        Exit;
    m_PageSize := Value;
end;

procedure TsuiTrackBar.CMFocusChanged(var Msg: TCMFocusChanged);
begin
    inherited;

    Repaint();
end;

procedure TsuiTrackBar.SetFileTheme(const Value: TsuiFileTheme);
begin
    m_FileTheme := Value;
    SetUIStyle(m_UIStyle);
end;

procedure TsuiTrackBar.Notification(AComponent: TComponent;
  Operation: TOperation);
begin
    inherited;

    if (
        (Operation = opRemove) and
        (AComponent = m_FileTheme)
    )then
    begin
        m_FileTheme := nil;
        SetUIStyle(SUI_THEME_DEFAULT);
    end;
end;

procedure TsuiTrackBar.SetTransparent(const Value: Boolean);
begin
    m_Transparent := Value;
    Repaint();
end;

procedure TsuiTrackBar.KeyDown(var Key: word; Shift: TShiftState);
begin
    inherited;
    case Key of
        VK_PRIOR: Position := Position - PageSize;
        VK_NEXT: Position := Position + PageSize;
        VK_END: Position := Max;
        VK_HOME: Position := Min;
        VK_LEFT: Position := Position - LineSize;
        VK_RIGHT: Position := Position + LineSize;
        VK_UP: Position := Position - LineSize;
        VK_DOWN: Position := Position + LineSize;
    end;
end;

procedure TsuiTrackBar.WMGetDlgCode(var Msg: TWMGetDlgCode);
begin
    inherited;
    Msg.Result := DLGC_WANTARROWS;
end;

procedure TsuiTrackBar.SetLineSize(const Value: Integer);
begin
    if (Value < 0) or (Value > m_Max) then
        Exit;

    m_LineSize := Value;
end;

procedure TsuiTrackBar.PaintTick(const Buf : TBitmap);
var
    x : Integer;
    i : Integer;
    nLen : Integer;
    nStart : Integer;
    Freq : Integer;
begin
    if m_Max = m_Min then
        Exit;

    if m_Orientation = suiHorizontal then
    begin
        nLen := Width - m_Slider.Width;
        nStart := m_Slider.Width div 2;
        Freq := 0;
        for i := m_Min to m_Max do
        begin
            if i <> m_Max then
            begin
                if Freq <> m_Frequency then
                begin
                    Inc(Freq);
                    if Freq <> 1 then
                        Continue
                end
                else
                    Freq := 1;
            end;
            x := nStart + nLen * (i - m_Min) div (m_Max - m_Min);
            Buf.Canvas.MoveTo(x, Height - 3);
            Buf.Canvas.LineTo(x, Height);
        end;
    end
    else
    begin
        nLen := Height - m_Slider.Height;
        nStart := m_Slider.Height div 2;
        Freq := 0;
        for i := m_Min to m_Max do
        begin
            if i <> m_Max then
            begin
                if Freq <> m_Frequency then
                begin
                    Inc(Freq);
                    if Freq <> 1 then
                        Continue
                end
                else
                    Freq := 1;
            end;
            x := nStart + nLen * (i - m_Min) div (m_Max - m_Min);
            Buf.Canvas.MoveTo(0, x);
            Buf.Canvas.LineTo(3, x);
            Buf.Canvas.MoveTo(Width - 3, x);
            Buf.Canvas.LineTo(Width, x);
        end;
    end;
end;

procedure TsuiTrackBar.SetShowTick(const Value: Boolean);
begin
    m_ShowTick := Value;
    Repaint();
end;

function TsuiTrackBar.SliderTransparent: boolean;
begin
    if m_Orientation = suiHorizontal then
        Result := true
    else
        Result := false;
end;

function TsuiTrackBar.CustomPicture: Boolean;
begin
    Result := m_CustomPicture;
end;

procedure TsuiTrackBar.SetFrequency(const Value: Integer);
begin
    m_Frequency := Value;
    Repaint();
end;

procedure TsuiTrackBar.OnSliderMouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
    if Assigned(OnMouseUp) then
        OnMouseUp(self, Button, Shift, m_Slider.Left + X, m_Slider.Top + Y);
end;

function TsuiTrackBar.SUICanFocus: Boolean;
begin
    Result := true;
end;

{ TsuiScrollTrackBar }

constructor TsuiScrollTrackBar.Create(AOwner: TComponent);
begin
    inherited;

    TabStop := false;
    m_Slider.TabStop := false;
    m_Slider.ControlStyle := self.ControlStyle;
end;

function TsuiScrollTrackBar.CustomPicture: Boolean;
begin
    Result := true;
end;

function TsuiScrollTrackBar.GetSliderVisible: Boolean;
begin
    Result := m_Slider.Visible;
end;

procedure TsuiScrollTrackBar.SetSliderVisible(const Value: Boolean);
begin
    if m_Slider.Visible <> Value then
        m_Slider.Visible := Value;
end;

function TsuiScrollTrackBar.SliderTransparent: boolean;
begin
    Result := false;
end;

function TsuiScrollTrackBar.SUICanFocus: Boolean;
begin
    Result := false;
end;

procedure TsuiScrollTrackBar.UpdateSliderSize;
begin
    m_Slider.AutoSize := true;
end;

end.

⌨️ 快捷键说明

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