编辑: 我想在TJvDBGrid(Project' TDBGrid的Jedi后代)上绘制一个垂直居中的TIcon图形和Text。 我试图禁用JvDBGrid的DefaultDrawing方法并覆盖它,但我只能用黑色填充单元格(我认为我的代码不完整以进行覆盖)。
现在我succeeded在单元格上绘制图标,文本与默认绘图保持一致。我如何将图标(垂直和水平)和文本(只是垂直)居中,如this?
这是我的代码:
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;
答案 0 :(得分: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