我需要你的帮助。绘制方法DBGrid。我有表DBGrid。
如何?到我的方法“DrawDBGrid”,添加我的方法“LoadDefaultImage” 如果没有文件路径(图像),将显示资源中的替换图像。
我的“LoadDefaultImage”
procedure LoadDefaultImage(AImage: TImage; aPngName: string);
var
lImage: TPngImage;
begin
lImage := TPngImage.Create();
try
lImage.LoadFromResourceName(hInstance, aPngName);
AImage.picture.Graphic := lImage;
finally
lImage.Free();
end;
end;
调用方法
LoadDefaultImage(AImage, 'No image');
我的“DrawDBGrid”
procedure DrawDBGrid(aDBGrid: TDBGrid; aGraphicFieldName: string; Column: TColumn; State: TGridDrawState; Sender: TObject;
const Rect: TRect; DataCol: Integer);
var
lRect: TRect;
lImage: TJPEGImage;
lPicturePath: string;
lDataset: TclientDataSet;
lIsFile: Boolean;
begin
lDataset := TclientDataSet(aDBGrid.DataSource.DataSet);
lPicturePath := extractfilepath(paramstr(0)) + lDataset.FieldByName(aGraphicFieldName).AsString;
lIsFile := fileexists(lPicturePath);
if (Column.Field.FieldName <> aGraphicFieldName) or (not lIsFile) then
begin
if (gdSelected in State) and (TDBGrid(Sender).Focused) then
TDBGrid(Sender).canvas.Brush.color := $E8E3A8
else
TDBGrid(Sender).canvas.Brush.color := clWhite;
aDBGrid.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end
else
begin
lRect.left := Rect.left + 1;
lRect.Top := Rect.Top + 1;
lRect.Right := Rect.Right - 1;
lRect.Bottom := Rect.Bottom - 1;
lImage := TJPEGImage.Create;
try
try
lImage.LoadFromFile(extractfilepath(paramstr(0)) + lDataset.FieldByName(aGraphicFieldName).AsString);
aDBGrid.canvas.StretchDraw(lRect, lImage);
except
on e: exception do
begin
raise exception.Create('Błąd wyświetlania pliku ' + lDataset.FieldByName(aGraphicFieldName)
.AsString + ''#13'' + ''#10'' + e.Message);
end;
end;
finally
lImage.Free;
end;
end;
end;
答案 0 :(得分:0)
如果DrawDBGrid
是从渲染事件调用的独立过程,您可以修改它以传递预加载的默认图像,如下所示:
procedure DrawDBGrid(aDBGrid: TDBGrid; aGraphicFieldName: string; Column: TColumn; State: TGridDrawState; Sender: TObject;
const Rect: TRect; DataCol: Integer; DefaultImage: TPngImage);
然后在里面你可以这样做:
...
lIsFile := FileExists(lPicturePath);
// if not the desired column
if (Column.Field.FieldName <> aGraphicFieldName) then
begin
if (gdSelected in State) and (TDBGrid(Sender).Focused) then
TDBGrid(Sender).canvas.Brush.color := $E8E3A8
else
TDBGrid(Sender).canvas.Brush.color := clWhite;
aDBGrid.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end
else // the right column
begin
// if the file was not found, draw the default image that was passed
if not lIsFile then
begin
aDBGrid.canvas.Draw(Rect, DefaultImage);
end
else
begin
lRect.left := Rect.left + 1;
lRect.Top := Rect.Top + 1;
lRect.Right := Rect.Right - 1;
lRect.Bottom := Rect.Bottom - 1;
lImage := TJPEGImage.Create;
try
try
lImage.LoadFromFile(extractfilepath(paramstr(0)) + lDataset.FieldByName(aGraphicFieldName).AsString);
aDBGrid.canvas.StretchDraw(lRect, lImage);
except
on e: exception do
begin
raise exception.Create('Błąd wyświetlania pliku ' + lDataset.FieldByName(aGraphicFieldName)
.AsString + ''#13'' + ''#10'' + e.Message);
end;
end;
finally
lImage.Free;
end;
end;
...
这里的最后一块乐高是你将预先加载默认图像,例如在创建表单时保存它,例如作为表单类中TPngImage
类型的私有字段,并将其传递给您的独立过程。