3

嗨,有谁知道是否可以将图片显示为字符串网格的背景,或者是否有人知道可以执行此操作的任何免费 Grid 组件。

谢谢

科林

4

3 回答 3

12

您可以使用支持所有者绘图的 a TDrawGrid(或 a TStringGrid),然后执行

procedure TForm1.FormCreate(Sender: TObject);
begin
  FBg := TBitmap.Create;
  FBg.LoadFromFile('C:\Users\Andreas Rejbrand\Pictures\Sample.bmp');
end;

FBga在哪里TBitmap(例如,在表单类中),然后执行

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  r: TRect;
begin
  if not (Sender is TStringGrid) then Exit;
  BitBlt(TStringGrid(Sender).Canvas.Handle,
         Rect.Left,
         Rect.Top,
         Rect.Right - Rect.Left,
         Rect.Bottom - Rect.Top,
         FBg.Canvas.Handle,
         Rect.Left,
         Rect.Top,
         SRCCOPY);
  if gdSelected in State then
    InvertRect(TStringGrid(Sender).Canvas.Handle, Rect);
  r := Rect;
  TStringGrid(Sender).Canvas.Brush.Style := bsClear;
  DrawText(TStringGrid(Sender).Canvas.Handle,
           TStringGrid(Sender).Cells[ACol, ARow],
           length(TStringGrid(Sender).Cells[ACol, ARow]),
           r,
           DT_SINGLELINE or DT_VCENTER or DT_END_ELLIPSIS);
end;

示例截图 示例截图 示例截图

于 2011-03-12T21:45:30.637 回答
4

虽然在他对 Andreas Rejbrand 的代码的评论中实际回答了 rossmcm 的明确问题,但它也补充了他对原始问题的回答。

绘制超出网格边界但仍在 StringGrid 控件范围内的图像可以实现如下:

type
  TStringGrid = class(Grids.TStringGrid)
  private
    FGraphic: TGraphic;
    FStretched: Boolean;
    function BackgroundVisible(var ClipRect: TRect): Boolean;
    procedure PaintBackground;
  protected
    procedure Paint; override;
    procedure Resize; override;
    procedure TopLeftChanged; override;
  public
    property BackgroundGraphic: TGraphic read FGraphic write FGraphic;
    property BackgroundStretched: Boolean read FStretched write FStretched;
  end;

  TForm1 = class(TForm)
    StringGrid: TStringGrid;
    Image: TImage;
    procedure FormCreate(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TStringGrid }

function TStringGrid.BackgroundVisible(var ClipRect: TRect): Boolean;
var
  Info: TGridDrawInfo;
  R: TRect;
begin
  CalcDrawInfo(Info);
  SetRect(ClipRect, 0, 0, Info.Horz.GridBoundary, Info.Vert.GridBoundary);
  R := ClientRect;
  Result := (ClipRect.Right < R.Right) or (ClipRect.Bottom < R.Bottom);
end;

procedure TStringGrid.Paint;
begin
  inherited Paint;
  PaintBackground;
end;

procedure TStringGrid.PaintBackground;
var
  R: TRect;
begin
  if (FGraphic <> nil) and BackgroundVisible(R) then
  begin
    with R do
      ExcludeClipRect(Canvas.Handle, Left, Top, Right, Bottom);
    if FStretched then
      Canvas.StretchDraw(ClientRect, FGraphic)
    else
      Canvas.Draw(0, 0, FGraphic);
  end;
end;

procedure TStringGrid.Resize;
begin
  inherited Resize;
  PaintBackground;
end;

procedure TStringGrid.TopLeftChanged;
begin
  inherited TopLeftChanged;
  PaintBackground;
end;

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin
  // Usage: 
  StringGrid.BackgroundGraphic := Image.Picture.Graphic;
  StringGrid.BackgroundStretched := True;
end;

如果您还想在单元格中绘制图像,请结合使用这两种技术。他们不遵循相同的方法,因为 Andreas 使用我声明后代的事件,不应导致合并有很大困难。

于 2011-05-25T18:06:03.273 回答
1

是的,有可能。TStringGrid 继承自 TDrawGrid 并自行完成所有绘图。您可以使用 OnDrawCell 事件进行自定义绘图。

于 2011-03-12T21:46:31.180 回答