1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83:
| unit Unit1;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Grids;
type TForm1 = class(TForm) StringGrid1: TStringGrid; procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); procedure FormCreate(Sender: TObject); private FMergedCells: array of TRect; public end;
var Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject); begin SetLength(FMergedCells, 2); FMergedCells[0] := Rect(1,1,2,1); StringGrid1.Cells[1,1] := 'This text spans over 2 cells';
FMergedCells[1] := Rect(3,1,4,1); StringGrid1.Cells[3,1] := 'This text spans over 2 cells'; StringGrid1.Cells[1,2] := 'A regular cell'; end;
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); function IsCellMerged(Point: TPoint; Rect: TRect): Boolean; begin Inc(Rect.Right); Inc(Rect.Bottom); Result := PtInRect(Rect, Point); end; var Index, I: Integer; LargeRect: TRect; CellSpan: TRect; begin with Sender as TStringGrid do for I := 0 to Length(FMergedCells) - 1 do begin CellSpan := FMergedCells[I]; if IsCellMerged(Point(ACol, ARow), CellSpan) then begin LargeRect := Classes.Rect( CellRect(CellSpan.Left, CellSpan.Top).TopLeft, CellRect(CellSpan.Right, CellSpan.Bottom).BottomRight); Inc(Rect.Right, GridLineWidth); Inc(Rect.Bottom, GridLineWidth);
Index := SaveDC(Canvas.Handle); try Canvas.Refresh; IntersectClipRect(Canvas.Handle, Rect.Left, Rect.Top, Rect.Right, Rect.Bottom);
Canvas.FillRect(LargeRect); Canvas.TextRect(LargeRect, LargeRect.Left + 2, LargeRect.Top + 2, Cells[FMergedCells[I].Left, FMergedCells[I].Top]); finally RestoreDC(Canvas.Handle, Index); Canvas.Refresh; end; end; end; end;
end. |