Strings プロパティに保存されている画像ファイルのサムネイル画像を表示する TScrollBox から派生したカスタム コンポーネントを作成しています。実行時にスクロールボックスのサイズが変更されるまで、うまく機能します。Risize メソッドをオーバーライドしようとしましたが、実行時にコンポーネントに画像を追加すると、2 つから 3 つの画像を追加した後にエラーが発生しました (無効なポインター操作)。以下の私のコードを参照してください
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;