如何在 DBGrid 上绘制垂直居中的内容
How to Draw Vertical Centered Content on DBGrid
已编辑:
我想在 TJvDBGrid(项目的 TDBGrid 的 Jedi 后代)上绘制一个垂直居中的 TIcon 图形和文本。
我试图禁用 JvDBGrid 的 DefaultDrawing 方法并覆盖它,但我只能用黑色填充单元格(我认为我的代码不完整,无法进行覆盖)。
现在我succeeded draw the Icon on Cell and the Text remains the same with Default Drawing. How I can center the Icon (vertical and horizontal) and the Text (just vertical ), like 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;
经过多次测试,我找到了一种将网上不同教程合并的解决方案。在 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
已编辑: 我想在 TJvDBGrid(项目的 TDBGrid 的 Jedi 后代)上绘制一个垂直居中的 TIcon 图形和文本。 我试图禁用 JvDBGrid 的 DefaultDrawing 方法并覆盖它,但我只能用黑色填充单元格(我认为我的代码不完整,无法进行覆盖)。
现在我succeeded draw the Icon on Cell and the Text remains the same with Default Drawing. How I can center the Icon (vertical and horizontal) and the Text (just vertical ), like 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;
经过多次测试,我找到了一种将网上不同教程合并的解决方案。在 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