Delphi 10中如何捕获TScrollBox的水平和垂直滚动位置(解决继承报错)
Delphi 10中TScrollBox滚动条位置获取后重置的问题
我编写的代码意图在用户拖动滚动条时获取TScrollBox的水平和垂直滚动条位置,但拖动后滚动条会重置回初始位置。我认为这是无法继承控件相关方法导致的。此前看过基于Lazarus实现该功能的视频,但现在需要在Delphi 10中完成开发。
表单代码(DFM)
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
单元代码(PAS)
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.
遇到的问题
尝试调用inherited WMHScroll(Message)时,编译器报错:
Cannot access a private symbol TScrollingWinControl.WMHScroll
原因是Vcl.Forms中的TScrollingWinControl类将WMHScroll方法声明为Private,无法直接继承调用。
内容的提问来源于stack exchange,提问作者Yuppski
相关产品推荐
相关产品推荐

