0

已编辑: 我想在 TJvDBGrid(项目的绝地 TDBGrid 后裔)上绘制垂直居中的 TIcon 图形和文本。我试图禁用 JvDBGrid 的 DefaultDrawing 方法并覆盖它,但我只能用黑色填充单元格(我认为我的代码不完整,无法进行覆盖)。

现在我成功地在单元格上绘制了图标,并且文本与默认绘图保持不变。我怎样才能使图标(垂直和水平)和文本(只是垂直)居中,像这样

这是我的代码:

procedure TFrmXXX.JvDBGridXXXDrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
  Icon: TIcon;
  fixRect: TRect;
  imgWidth: Integer;
begin
  fixRect := Rect;

  if Column.Index = 0  then //always the first one
  begin
    Icon := GetIcon; //Returns TIcon object
    try
      imgWidth := (Rect.Bottom - Rect.Top);
      fixRect.Right := Rect.Left + imgWidth;
      (Sender as TJvDBGrid).Canvas.StretchDraw(fixRect, Icon);
    finally
      Icon.Free;
    end;
    fixRect := Rect;
    fixRect.Left := fixRect.Left + imgWidth;
  end;

  (Sender as TJvDBGrid).DefaultDrawColumnCell(fixRect, DataCol, Column, State);
end;
4

1 回答 1

0

经过多次测试,我找到了一个合并互联网上不同教程的解决方案。在 DrawColumnCell 事件中,我写了如下内容:

Canvas.FillRect(Rect); //Fill the cell using current brush. 

在每个特定的列案例中,我使用了以下方法之一:

Canvas.Draw((Rect.Right - Rect.Left - Icon.Width) div 2 + Rect.Left, (Rect.Bottom - Rect.Top - Icon.Height) div 2 + Rect.Top, Icon); //Draw the graphic centered on cell

Canvas.DrawText(Canvas.Handle, PChar(Column.Field.DisplayText), Length(Column.Field.DisplayText), Rect, DT_VCENTER or DT_CENTER or DT_SINGLELINE or DT_NOPREFIX); //Draw vertical and horizontal centered text

Canvas.DrawText(Canvas.Handle, PChar(Column.Field.DisplayText), Length(Column.Field.DisplayText), Rect, DT_VCENTER or DT_SINGLELINE or DT_NOPREFIX);  //Draw vertical centered and horizontal left justified text
于 2015-03-19T03:05:52.067 回答