gpt4 book ai didi

delphi - 在自定义组件中重写 TScrollBox OnResize 事件

转载 作者:行者123 更新时间:2023-12-02 13:45:04 26 4
gpt4 key购买 nike

我正在制作一个从 TScrollBox 派生的自定义组件,它显示存储在字符串属性中的图像文件的缩略图。它工作得很好,直到滚动框在运行时调整大小。我尝试重写 Risize 方法,但是当我在运行时向组件添加图像时,在添加两到三个图像后,出现错误(无效指针操作)。请参阅下面的代码

type
TViewer = class(TScrollBox)
private
{ Private declarations }
fColumns: Integer;
fImages : TStrings;
fThumbWidth: Integer;
fThumbHeight: Integer;
procedure SetColumns(Value: Integer);
procedure SetImages(Value: TStrings);
procedure SetThumbWidth(Value: Integer);
procedure SetThumbHeight(Value: Integer);
function GetColumns: Integer;
function GetThumbWidth: Integer;
function GetThumbHeight: Integer;
protected
{ Protected declarations }
procedure LinesChanged(Sender: TObject);
procedure UpdateViewer;
procedure ClearViewer;
**//procedure Resize;override;**
public
{ Public declarations }
constructor Create(AOwner: TComponent);override;
destructor Destroy;override;
published
{ Published declarations }
property Columns : Integer read GetColumns write SetColumns default 5;
property Images : TStrings read fImages write SetImages;
property ThumbHeight: Integer read GetThumbHeight write SetThumbHeight default 100;
property ThumbWidth: Integer read GetThumbWidth write SetThumbWidth default 100;
property OnImageClick: TNotifyEvent read fOnImageClick write fOnImageClick;
end;

procedure Register;

implementation

procedure TViewer.ClearViewer;
var
I: Integer;
begin
for I := ControlCount-1 downto 0 do
TPanel(Components[I]).Free;
end;

procedure TViewer.UpdateViewer;
var
I,X,Y,C, D: Integer;
Thumb, btnPnl: TPanel;
img: TImage;
btn1, btn2, btn3, btn4: TSpeedButton;
begin
self.
ClearViewer;
X:= 0;
Y:= 0;
C:= 1;
D:= 5;
Thumb:= nil;
for I := 0 to fimages.Count - 1 do
begin
if fileExists(fimages[I]) then
begin
Thumb:= TPanel.Create(self);
Thumb.Parent:= self;
thumb.Tag:= I;

Thumb.Caption:= '';
Thumb.Left:= X + D;
Thumb.Top:= Y + D;
Thumb.Width:= fThumbWidth;
Thumb.Height:= fThumbHeight;
img:= TImage.Create(Thumb);
img.Parent:= Thumb;
img.Tag:= I;
img.Align:= alClient;
img.Stretch:= true;
img.OnClick:= fOnImageClick;
img.Picture.LoadFromFile(fImages[I]);
end;
X:= X + Thumb.Width + D;
if C = fColumns then
begin
X:= 0;
Y:= Y + Thumb.Height + D;
C:= 0;
end
else
Inc(C);
end;
end;


procedure TViewer.SetImages(Value: TStrings);
begin
fImages.Assign(Value);
UpdateViewer;
end;

procedure TViewer.SetColumns(Value: Integer);
begin
if fColumns <> value then
fColumns:= Value;
end;

procedure TViewer.SetThumbWidth(Value: Integer);
begin
if fThumbWidth <> Value then
fThumbWidth:= Value;
end;
procedure TViewer.SetThumbHeight(Value: Integer);
begin
if fThumbHeight <> Value then
fThumbHeight:= Value;
end;

function TViewer.GetColumns: Integer;
begin
result:= fColumns;
end;

function TViewer.GetThumbWidth: Integer;
begin
result:= fThumbWidth;
end;

function TViewer.GetThumbHeight: Integer;
begin
result:= fThumbHeight;
end;

procedure TViewer.LinesChanged(Sender: TObject);
begin
UpdateViewer;
end;

**{procedure TViewer.Resize;
begin
inherited;
UpdateViewer;
end;}**

constructor TViewer.Create(AOwner: TComponent);
begin
inherited Create(aOwner);
DoubleBuffered:= True;
fImages := TStringList.Create;
TStringList(fImages).OnChange := LinesChanged;
fThumbHeight:= 100;
fThumbWidth:= 100;
fColumns:= 5;
end;

destructor TViewer.Destroy;
begin
fImages.Free;
inherited;
end;

最佳答案

代码

for I := ControlCount-1 downto 0 do
TPanel(Components[I]).Free;

有问题。事实上,ControlCount 之于 Controls 就像 ComponentCount 之于 Components 一样。你不能混合它们!

你应该这样做:

for I := ControlCount-1 downto 0 do
Controls[I].Free;

但是,我认为这不是问题所在。

关于delphi - 在自定义组件中重写 TScrollBox OnResize 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15322794/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com