2

I have code to get the Horizontal and Vertical Bar Positions of a TScrollBox when the user moves the bars but the scroll bar resets to its original position what I believe is due to not being able to inherit a control method.

I have seen this video of doing it using Lazarus

But I'd like to do it in Delphi 10.

This is the form:-

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 299
  ClientWidth = 635
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  Position = poScreenCenter
  PixelsPerInch = 96
  TextHeight = 13
  object ScrollBox1: TScrollBox
    Left = 8
    Top = 8
    Width = 434
    Height = 289
    TabOrder = 0
    object Panel1: TPanel
      Left = 48
      Top = 41
      Width = 443
      Height = 385
      Caption = 'Panel1'
      TabOrder = 0
    end
  end
  object lbScrollBox_Vertical_Pos: TLabeledEdit
    Left = 448
    Top = 24
    Width = 121
    Height = 21
    EditLabel.Width = 105
    EditLabel.Height = 13
    EditLabel.Caption = 'ScrollBox Vertical Pos:'
    TabOrder = 1
  end
  object lbScrollBox_Horizontal_Pos: TLabeledEdit
    Left = 448
    Top = 64
    Width = 121
    Height = 21
    EditLabel.Width = 118
    EditLabel.Height = 13
    EditLabel.Caption = 'ScrollBox Horizontal Pos:'
    TabOrder = 2
  end
end

This is the code:-

    unit uMain;
    
    interface
    
    uses
      Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
      Vcl.Controls, Vcl.Dialogs, Vcl.Imaging.pngimage, Vcl.ExtCtrls,
      Vcl.StdCtrls, Vcl.Forms;
    
    type
      TScrollBox = class(Vcl.Forms.TScrollBox)
        procedure WMHScroll(var Message: TWMHScroll); message WM_HSCROLL;
        procedure WMVScroll(var Message: TWMVScroll); message WM_VSCROLL;
      end;
    
      TForm1 = class(TForm)
        ScrollBox1: TScrollBox;
        lbScrollBox_Vertical_Pos: TLabeledEdit;
        lbScrollBox_Horizontal_Pos: TLabeledEdit;
        Panel1: TPanel;
        procedure WMHScroll(var Message: TWMHScroll);
        procedure WMVScroll(var Message: TWMVScroll);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    { TScrollBox }
    {$REGION 'TScrollBox'}
    procedure TScrollBox.WMHScroll(var Message: TWMHScroll);
    begin
      inherited WMHScroll(Message);
      Form1.WMHScroll(Message);
    end;
    
    procedure TScrollBox.WMVScroll(var Message: TWMVScroll);
    begin
      inherited WMVScroll(Message);
      Form1.WMVScroll(Message);
    end;
    {$ENDREGION 'TScrollBox'}
    
    { TForm1 }
    
    {$REGION 'TForm1'}
    procedure TForm1.WMHScroll(var Message: TWMHScroll);
    begin
      lbScrollBox_Horizontal_Pos.Text := Message.Pos.ToString;
    end;
    
    procedure TForm1.WMVScroll(var Message: TWMVScroll);
    begin
      lbScrollBox_Vertical_Pos.Text := Message.Pos.ToString;
    end;
    {$ENDREGION 'TForm1'}
    end.

I cant call:

inherited WMHScroll(Message)

As the complier says "Cannot access a private symbol TScrollingWinControl.WMHScroll" Which is correct as the Vcl.Forms TScrollingWinControl = class(TWinControl) has the method procedure WMHScroll declared as Private.

1 Answer 1

2

The compiler is right: WMHScroll is private and thus cannot be used in a derived class.

That doesn't mean that you can't call the inherited method bound to WM_HSCROLL. Just use a plain inherited;

    procedure TScrollBox.WMHScroll(var Message: TWMHScroll);
    begin
      inherited;
      Form1.WMHScroll(Message);
    end;
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply. I thought I did try that and the Scroll bar reset. Trying again and the scroll bar is ok. The position does register when scrolling, providing I keep my finger on the left mouse button down. As soon as I release the button the value is zero?
The Pos value is valid only when ScrollCode is SB_THUMBPOSITION or SB_THUMBTRACK.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.