绘制 DBGrid(从资源替换图像)



我需要你的帮助。绘制方法DBGrid。我有表DBGrid。

如何?到我的方法"DrawDBGrid",添加我的方法"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;

如果 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 类型的私有字段,并将其传递到您的独立过程中。

最新更新