image - delphi:如何将图像放入DBGrid标题中?

标签 image delphi title lazarus dbgrid

如何将图像放入 TDBGrid 列标题中?

我尝试过,但当我将鼠标放在标题上时,图像不断显示并不断消失。

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
  if Column.FieldName = order then
  Begin
    Column.Title.Font.Color := clBlue;

    //if gdFixed in State then // didn't work..  I don't know why!!!

    if Rect.Top < 30 then 
      ImageList1.Draw(DBGrid1.Canvas, Rect.Right-18, Rect.Top-18, 0);

  end
  else 
    Column.Title.Font.Color := clWindowText;
end;

最佳答案

您可以使用 TDBGrid 的插入器类并覆盖 DrawCell 过程。

type
  TDBGrid = Class(DBGrids.TDBGrid)
  private
    FIcon:TIcon;
    FImageList: TImageList;
    procedure SetImageList(const Value: TImageList);
    Destructor Destroy;override;
  published
    procedure DrawCell(ACol, ARow: Longint; ARect: TRect; AState: TGridDrawState); override;
    Property Imagelist: TImageList read FImageList Write SetImageList;
  End;

  TForm2 = class(TForm)
    .......

implementation


{$R *.dfm}
{ TDBGrid }

destructor TDBGrid.Destroy;
begin
  if Assigned(FIcon) then FIcon.Free;
  inherited;
end;

procedure TDBGrid.SetImageList(const Value: TImageList);
begin
  FImageList := Value;
  FreeAndNil(FIcon);
  if Assigned(FImageList) then
    begin
    FIcon := TIcon.Create;
    FImageList.GetIcon(0, FIcon);
    end;
end;

procedure TDBGrid.DrawCell(ACol, ARow: Integer; ARect: TRect; AState: TGridDrawState);
var
  L_Col: Integer;
begin
  if dgIndicator in Options then
    L_Col := ACol - 1
  else
    L_Col := ACol;

  inherited;
  if Assigned(FIcon) and (L_Col > -1) and (ARow = 0) and (Columns[L_Col].FieldName = 'ID') and (gdFixed in AState) then
  begin
      Canvas.Draw(ARect.Right - 18, ARect.Bottom - 18, FIcon);
      //FImagelist.Draw(Canvas,ARect.Right - 18, ARect.Bottom - 18,0); // would cause more flickering
  end;
end;

procedure TForm2.FormCreate(Sender: TObject);
begin
  DBGrid1.DoubleBuffered := true;
  DBGrid1.Imagelist := ImageList1;
  ReportMemoryLeaksOnShutDown := true;
end;

关于image - delphi:如何将图像放入DBGrid标题中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16702992/

相关文章:

c# - 使用 .NET 程序集的 Delphi 出现 OLE 错误 8013150A(以管理员身份运行奇怪)

delphi - 在 Tchart 上绘制 stringgrid 值

Android SDK 示例应用问题 :

java - Java 中的平台独立图像

python - 如何解决网站上的 html5_webcam 问题?

delphi - 我可以使用 Delphi XE2 Subversion 与 Subversion 1.7 的集成吗

r - 如何更改排列Grob 上的标题(顶部)大小?

python - 为什么 Windows 上的 Python 无法以二进制模式读取图像?

java - 在 Java 中显示图像

php - 使用php提取关键字对进行SEO