我正在製作從TScrollBox派生的自定義組件,該自定義組件顯示存儲在字符串屬性中的圖像文件的縮略圖。它運行良好,直到Scrollbox在運行時調整大小。我嘗試覆蓋Risize方法,但是當我在運行時將圖像添加到組件後,添加兩個到三個圖像後,它出錯(無效的指針操作)。見下面重寫自定義組件中的TScrollBox OnResize事件
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;
我修復了這一行 - TPanel(Components [I])。 (控制[I])。 – JakeSays 2013-03-10 13:53:23