RAD Studio How to reposition a TScrollBox view to show the areas: Bottom and Right, or anyother dont showed on the screen by Emailx45

emailx45

Свой
Регистрация
5 Май 2008
Сообщения
3,581
Реакции
2,450
Credits
706
How to reposition a TScrollBox view to show the areas: Bottom and Right, or anyother dont showed on the screen
by Emailx45

NOTE: The "ViewPortPosition" can help too! because, in it, you have the values of positions changed!

Scenary
  • RAD Studio 10.3.3 Arch
  • FireMonkey project
  • .. a little code for demo

[SHOWTOGROUPS=4,20,22]



[/SHOWTOGROUPS]
[SHOWTOGROUPS=4,20,22]

Код:
unit uFormMain;

interface

uses
  System.SysUtils,
  System.Types,
  System.UITypes,
  System.Classes,
  System.Variants,
  FMX.Types,
  FMX.Controls,
  FMX.Forms,
  FMX.Graphics,
  FMX.Dialogs,
  FMX.Objects,
  FMX.Layouts,
  FMX.Controls.Presentation,
  FMX.StdCtrls,
  FMX.ScrollBox,
  FMX.Memo,
  FMX.Edit;

type
  TfrmFormMain = class(TForm)
    ScrollBox1: TScrollBox;
    Image1: TImage;
    Rectangle1: TRectangle;
    btnScrollBox_on_Bottom: TButton;
    btnScrollBox_on_TopLeft: TButton;
    Memo1: TMemo;
    edtHScrollChange: TEdit;
    lbHScrollChange: TLabel;
    lbViewportPositionChange: TLabel;
    edtViewportPositionChange: TEdit;
    lbVScrollChange: TLabel;
    edtVScrollChange: TEdit;
    procedure btnScrollBox_on_BottomClick(Sender: TObject);
    procedure btnScrollBox_on_TopLeftClick(Sender: TObject);
    procedure ScrollBox1HScrollChange(Sender: TObject);
    procedure ScrollBox1VScrollChange(Sender: TObject);
    procedure ScrollBox1ViewportPositionChange(Sender: TObject; const OldViewportPosition, NewViewportPosition: TPointF; const ContentSizeChanged: Boolean);
  private
    procedure prcMyLog(lText: string = '');
  public
    { Public declarations }
  end;

var
  frmFormMain: TfrmFormMain;

implementation

{$R *.fmx}

//
// NOTE:
// when rolling the scrollbars (vert or hor) (manually or not) the "ViewportPosition" is changed !
//
var
  iHScrollChangeCounter         : Integer = 0;
  iViewportPositionChangeCounter: Integer = 0;
  iVScrollChangeCounter         : Integer = 0;

procedure TfrmFormMain.btnScrollBox_on_BottomClick(Sender: TObject);

var
  lRe_Scroll_me_to_DOWN : Single;
  lRe_Scroll_me_to_RIGHT: Single;
begin
  // my values on test! of course, is not real for all!
  // 160 and 168 is resulted from my test below to "lRe_Scroll_me_to_RIGHT" and "lRe_Scroll_me_to_DOWN" at ScrollBy()
  // for dont repeat this commands!
  if (ScrollBox1.ViewportPosition.X <> 160.0) or (ScrollBox1.ViewportPosition.Y <> 168.0) then
  begin
    lRe_Scroll_me_to_DOWN  := Abs(Image1.Height - ScrollBox1.ClientHeight) + Image1.Position.Y;
    lRe_Scroll_me_to_RIGHT := Abs(Image1.Width - ScrollBox1.ClientWidth) + Image1.Position.X;
    //
    Caption := Format('%f = %f = %f = %f', [ { }
      ScrollBox1.ClientHeight,               { }
      Image1.Height,                         { }
      lRe_Scroll_me_to_DOWN,                 { }
      lRe_Scroll_me_to_RIGHT                 { }
      ]);
    //
    ScrollBox1.ScrollBy(-lRe_Scroll_me_to_RIGHT, -lRe_Scroll_me_to_DOWN); // negative to DOWN or RIGHT direction!
    //
    prcMyLog(Format('ScrollBox1.ViewportPosition.X and .Y = %f, %f', [ { }
      ScrollBox1.ViewportPosition.X,                                   { }
      ScrollBox1.ViewportPosition.Y                                    { }
      ])                                                               { }
      );
    //
    prcMyLog('... changing position!');
  end;
end;

procedure TfrmFormMain.btnScrollBox_on_TopLeftClick(Sender: TObject);
var
  lRe_Scroll_me_to_TOP : Single;
  lRe_Scroll_me_to_LEFT: Single;
begin
  prcMyLog(Format('ScrollBox1.ViewportPosition.X and .Y = %f, %f', [ { }
    ScrollBox1.ViewportPosition.X,                                   { }
    ScrollBox1.ViewportPosition.Y                                    { }
    ])                                                               { }
    );
  //
  // ON FIRST, it would can be ignored, if you dont need re-config the position!
  // for example, if position was not changed... etc!!!
  //
  // the ScrollBars was changed?
  if (ScrollBox1.ViewportPosition.X > 0) or (ScrollBox1.ViewportPosition.Y > 0) then
  begin
    //
    lRe_Scroll_me_to_TOP  := Abs(Image1.Height - ScrollBox1.ClientHeight) + Image1.Position.Y;
    lRe_Scroll_me_to_LEFT := Abs(Image1.Width - ScrollBox1.ClientWidth) + Image1.Position.X;
    //
    Caption := Format('%f = %f = %f = %f', [ { }
      ScrollBox1.ClientHeight,               { }
      Image1.Height,                         { }
      lRe_Scroll_me_to_TOP,                  { }
      lRe_Scroll_me_to_LEFT                  { }
      ]);
    //
    ScrollBox1.ScrollBy(lRe_Scroll_me_to_LEFT, lRe_Scroll_me_to_TOP); // positive to TOP or LEFT direction!
    //
    prcMyLog('... changing position!');
  end;
end;

procedure TfrmFormMain.prcMyLog(lText: string);
begin
  if not(lText.Trim = '') then
    Memo1.Lines.Add(lText);
end;

procedure TfrmFormMain.ScrollBox1HScrollChange(Sender: TObject);
begin
  inc(iHScrollChangeCounter);
  edtHScrollChange.Text := Format('HScrollChange %dx', [iHScrollChangeCounter]);
end;

procedure TfrmFormMain.ScrollBox1ViewportPositionChange(Sender: TObject; const OldViewportPosition, NewViewportPosition: TPointF; const ContentSizeChanged: Boolean);
begin
  inc(iViewportPositionChangeCounter);
  edtViewportPositionChange.Text := Format('ViewportPositionChange %dx', [iViewportPositionChangeCounter]);
end;

procedure TfrmFormMain.ScrollBox1VScrollChange(Sender: TObject);
begin
  inc(iVScrollChangeCounter);
  edtVScrollChange.Text := Format('VScrollChange %dx', [iVScrollChangeCounter]);
end;

end.
[/SHOWTOGROUPS]