Ignore:
Timestamp:
Jan 19, 2017, 8:40:22 PM (7 years ago)
Author:
chronos
Message:
  • Modified: Simplified PVSB.TPVScrollBar class and fixed wrong scrolling under Windows.
  • Modified: Enabled Windows XP manifest to have themed scrollbars.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/LocalPlayer/PVSB.pas

    r70 r89  
    1818  private
    1919    FOnUpdate: TNotifyEvent;
     20    ScrollBar: TScrollBar;
     21    function GetMax: Integer;
     22    function GetPageSize: Integer;
     23    function GetPosition: Integer;
    2024    procedure ScrollBarChanged(Sender: TObject);
     25    procedure SetMax(AValue: Integer);
     26    procedure SetPageSize(AValue: Integer);
     27    procedure SetPosition(AValue: Integer);
    2128  public
    22     ScrollBar: TScrollBar;
    23     si: TScrollInfo;
     29    constructor Create(Parent: TWinControl);
    2430    destructor Destroy; override;
    25     procedure Setup(TopSpacing, RightSpacing, BottomSpacing: integer; Parent: TWinControl);
    26     procedure Init(max, Page: integer);
     31    procedure Init(Max, Page: Integer);
    2732    procedure SetPos(Pos: Integer);
    2833    function Process(const m: TMessage): boolean;
    29     function ProcessMouseWheel(Delta: Integer) : boolean;
     34    function ProcessMouseWheel(Delta: Integer): Boolean;
    3035    procedure Show(Visible: boolean);
    3136    procedure EndSB;
    32     procedure UpdateScrollBar;
     37    procedure SetBorderSpacing(Top, Right, Bottom: Integer);
    3338    property OnUpdate: TNotifyEvent read FOnUpdate write FOnUpdate;
     39    property Position: Integer read GetPosition write SetPosition;
     40    property Max: Integer read GetMax write SetMax;
     41    property PageSize: Integer read GetPageSize write SetPageSize;
    3442  end;
    3543
     
    3846
    3947const
    40   Count: integer = 0;
     48  Count: Integer = 0;
    4149
    42 procedure TPVScrollBar.Setup(TopSpacing, RightSpacing, BottomSpacing: integer;
    43   Parent: TWinControl);
     50procedure TPVScrollBar.Init(Max, Page: Integer);
    4451begin
    45   inc(Count);
    46   //{$IFDEF LINUX}
    47 //  sb.Form := TForm.Create(nil);
    48 //  sb.Form.SetBounds(x1 - 100, y0, 100, y1 - y0);
    49 //  sb.Form.Name := 'PVSB' + IntToStr(Count);
    50   ScrollBar := TScrollBar.Create(Parent);
    51   ScrollBar.Kind := sbVertical;
    52   ScrollBar.Name := 'PVSB' + IntToStr(Count);
    53   ScrollBar.Parent := Parent;
    54   ScrollBar.BorderSpacing.Top := TopSpacing;
    55   ScrollBar.BorderSpacing.Right := RightSpacing;
    56   ScrollBar.BorderSpacing.Bottom := BottomSpacing;
    57   ScrollBar.Align := alRight;
    58   ScrollBar.OnChange := ScrollBarChanged;
    59   //sb.h := sb.ScrollBar.Handle;
    60   (*
    61   {$ENDIF}
    62   {$IFDEF WINDOWS}
    63   sb.h := CreateWindowEx(0, 'SCROLLBAR', pchar('PVSB' + IntToStr(Count)),
    64     SBS_VERT or WS_CHILD or SBS_RIGHTALIGN, x1 - 100, y0, 100, y1 - y0,
    65     Handle, 0, 0, nil);
    66   {$ENDIF}
    67   *)
    68   si.cbSize := 28;
    69 end;
    70 
    71 procedure TPVScrollBar.Init(max, Page: integer);
    72 begin
    73   with si do begin
    74     nMin := 0;
    75     nMax := max;
    76     npos := 0;
    77     nPage := Page;
    78     FMask := SIF_PAGE or SIF_POS or SIF_RANGE;
    79   end;
    80   UpdateScrollBar;
    81   //SetScrollInfo(sb.ScrollBar.Handle, SB_CTL, sb.si, true);
    82   if max < Page then ScrollBar.Visible := False
    83     else ScrollBar.Visible := True;
     52  ScrollBar.Min := 0;
     53  ScrollBar.Max := Max;
     54  ScrollBar.PageSize := Page;
     55  ScrollBar.Position := 0;
     56  ScrollBar.Visible := ScrollBar.Max >= ScrollBar.PageSize;
    8457end;
    8558
     
    8760begin
    8861  if Pos <> 0 then begin
    89     si.npos := Pos;
    90     si.FMask := SIF_POS;
    91     //SetScrollInfo(sb.ScrollBar.Handle, SB_CTL, sb.si, true);
    92     UpdateScrollBar;
     62    ScrollBar.Position := Pos;
    9363  end;
    9464end;
     
    9868  NewPos: integer;
    9969begin
    100   with si do
    101     if nMax < integer(nPage) then
     70    if ScrollBar.Max < ScrollBar.PageSize then
    10271      result := false
    10372    else
     
    10574      if (m.wParam and $ffff) in [SB_THUMBPOSITION, SB_THUMBTRACK] then
    10675      begin
    107         result := ((m.wParam shr 16) and $ffff) <> npos;
    108         npos := (m.wParam shr 16) and $ffff;
    109       end
    110       else
    111       begin
     76        result := ((m.wParam shr 16) and $ffff) <> ScrollBar.Position;
     77        ScrollBar.Position := (m.wParam shr 16) and $ffff;
     78      end else begin
    11279        case (m.wParam and $ffff) of
    11380          SB_LINEUP:
    114             NewPos := npos - 1;
     81            NewPos := ScrollBar.Position - 1;
    11582          SB_LINEDOWN:
    116             NewPos := npos + 1;
     83            NewPos := ScrollBar.Position + 1;
    11784          SB_PAGEUP:
    118             NewPos := npos - integer(nPage);
     85            NewPos := ScrollBar.Position - ScrollBar.PageSize;
    11986          SB_PAGEDOWN:
    120             NewPos := npos + integer(nPage);
     87            NewPos := ScrollBar.Position + ScrollBar.PageSize;
    12188        else
    122           NewPos := npos
     89          NewPos := ScrollBar.Position
    12390        end;
    12491        if NewPos < 0 then
    12592          NewPos := 0;
    126         if NewPos > nMax - integer(nPage) + 1 then
    127           NewPos := nMax - integer(nPage) + 1;
    128         result := NewPos <> npos;
    129         if (NewPos <> npos) or ((m.wParam and $ffff) = SB_ENDSCROLL) then
     93        if NewPos > ScrollBar.Max - ScrollBar.PageSize + 1 then
     94          NewPos := ScrollBar.Max - ScrollBar.PageSize + 1;
     95        result := NewPos <> ScrollBar.Position;
     96        if (NewPos <> ScrollBar.Position) or ((m.wParam and $ffff) = SB_ENDSCROLL) then
    13097        begin
    131           npos := NewPos;
    132           FMask := SIF_POS;
    133           UpdateScrollBar;
    134           //SetScrollInfo(sb.ScrollBar.Handle, SB_CTL, sb.si, true);
     98          ScrollBar.Position := NewPos;
    13599        end;
    136       end
    137     end
     100      end;
     101    end;
    138102end;
    139103
    140 function TPVScrollBar.ProcessMouseWheel(Delta: Integer
    141   ): boolean;
     104function TPVScrollBar.ProcessMouseWheel(Delta: Integer): Boolean;
    142105var
    143106  NewPos: integer;
    144107begin
    145   with si do
    146     if nMax < integer(nPage) then
    147       result := false
    148     else
    149     begin
    150       NewPos := npos - Delta div 300;
    151       if NewPos < 0 then
    152         NewPos := 0;
    153       if NewPos > nMax - integer(nPage) + 1 then
    154         NewPos := nMax - integer(nPage) + 1;
    155       result := NewPos <> npos;
    156       if NewPos <> npos then
    157       begin
    158         npos := NewPos;
    159         FMask := SIF_POS;
    160         UpdateScrollBar;
    161         //SetScrollInfo(sb.ScrollBar.Handle, SB_CTL, sb.si, true);
    162       end
    163     end
     108    if ScrollBar.Max < ScrollBar.PageSize then Result := False
     109    else begin
     110      NewPos := ScrollBar.Position - Delta div 300;
     111      if NewPos < 0 then NewPos := 0;
     112      if NewPos > ScrollBar.Max - ScrollBar.PageSize + 1 then
     113        NewPos := ScrollBar.Max - ScrollBar.PageSize + 1;
     114      Result := NewPos <> ScrollBar.Position;
     115      if NewPos <> ScrollBar.Position then begin
     116        ScrollBar.Position := NewPos;
     117      end;
     118    end;
    164119end;
    165120
    166121procedure TPVScrollBar.Show(Visible: boolean);
    167122begin
    168   if not Visible or (si.nMax < integer(si.nPage)) then
     123  if not Visible or (ScrollBar.Max < ScrollBar.PageSize) then
    169124    ScrollBar.Visible := False
    170125    else ScrollBar.Visible := True;
     
    173128procedure TPVScrollBar.EndSB;
    174129begin
    175   with si do begin
    176     if nMax < integer(nPage) then
    177       npos := 0 // hidden
    178     else begin
    179       si.npos := nMax - integer(nPage) + 1;
    180       si.FMask := SIF_POS;
    181       UpdateScrollBar;
    182       //SetScrollInfo(sb.ScrollBar.Handle, SB_CTL, sb.si, true);
    183     end
    184   end
     130  if ScrollBar.Max < ScrollBar.PageSize then
     131    ScrollBar.Position := 0 // hidden
     132  else begin
     133    ScrollBar.Position := ScrollBar.Max - ScrollBar.PageSize + 1;
     134  end;
    185135end;
    186136
    187 procedure TPVScrollBar.UpdateScrollBar;
     137procedure TPVScrollBar.SetBorderSpacing(Top, Right, Bottom: Integer);
    188138begin
    189   ScrollBar.Min := si.nMin;
    190   ScrollBar.Max := Max(si.nMax{$IFDEF LINUX} - si.nPage + 1{$ENDIF}, 0);
    191   ScrollBar.PageSize := si.nPage;
    192   ScrollBar.Position := si.nPos;
     139  ScrollBar.BorderSpacing.Top := Top;
     140  ScrollBar.BorderSpacing.Right := Right;
     141  ScrollBar.BorderSpacing.Bottom := Bottom;
    193142end;
    194143
     
    197146procedure TPVScrollBar.ScrollBarChanged(Sender: TObject);
    198147begin
    199   si.npos := ScrollBar.Position;
    200148  if Assigned(FOnUpdate) then FOnUpdate(Self);
     149end;
     150
     151procedure TPVScrollBar.SetMax(AValue: Integer);
     152begin
     153  ScrollBar.Max := AValue;
     154end;
     155
     156procedure TPVScrollBar.SetPageSize(AValue: Integer);
     157begin
     158  ScrollBar.PageSize := AValue;
     159end;
     160
     161function TPVScrollBar.GetPosition: Integer;
     162begin
     163  Result := ScrollBar.Position;
     164end;
     165
     166function TPVScrollBar.GetMax: Integer;
     167begin
     168  Result := ScrollBar.Max;
     169end;
     170
     171function TPVScrollBar.GetPageSize: Integer;
     172begin
     173  Result := ScrollBar.PageSize;
     174end;
     175
     176procedure TPVScrollBar.SetPosition(AValue: Integer);
     177begin
     178  ScrollBar.Position := AValue;
     179end;
     180
     181constructor TPVScrollBar.Create(Parent: TWinControl);
     182begin
     183  Inc(Count);
     184  ScrollBar := TScrollBar.Create(Parent);
     185  ScrollBar.Kind := sbVertical;
     186  ScrollBar.Name := 'PVSB' + IntToStr(Count);
     187  ScrollBar.Align := alRight;
     188  ScrollBar.OnChange := ScrollBarChanged;
     189  ScrollBar.Parent := Parent;
    201190end;
    202191
    203192destructor TPVScrollBar.Destroy;
    204193begin
    205   //h := 0;
    206   si.cbSize := 0;
    207194  FreeAndNil(ScrollBar);
    208195end;
Note: See TracChangeset for help on using the changeset viewer.