Autor Beitrag
Bronstein
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 578
Erhaltene Danke: 1

WIN XP
Delphi 6 / Delphi 2006 / Delphi XE
BeitragVerfasst: Sa 21.06.08 09:16 
Hallo,
ich möchte zwei Zellen in einem Stringgrid verbinden, bisher habe ich es immer nur hinbekommen, dass ich eine ganze Zeile verbinden kann. Meine Tabelle ist folgendermaßen aufgebaut

ausblenden Quelltext
1:
2:
Linie|   Früh  |   Spät  |  Nacht
Linie|Pass|Fail|Pass|Fail|Pass|Fail


Jetzt soll in meinem StringGrid die Zellen Früh, Spät, Nacht so verbunden werden, dass unter diesen 3 immer Pass und Fail steht.

Hier mal der Code für das verbinden einer ganzen Zeile:
ausblenden volle Höhe Delphi-Quelltext
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:
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  i, x, y: Integer;
begin
  If gdFixed In State Then Exit;
  If ARow > 1 Then Exit;

  // draw row 1 with text from cell 1,1 spanning all cells in the row
  with sender as tstringgrid do begin
    {extend rect to include grid line on right, if not last cell
     in row}

    If aCol < Pred(ColCount) Then
      Rect.Right := Rect.Right + GridlineWidth;
     
    {figure out where the text of the first cell would start relative
     to the current cells rect. }
 
    y:= Rect.Top + 2;
    x:= Rect.Left + 2;
    for i:= 1 to aCol-1 do
      x:= x - ColWidths[i] - GridlineWidth;
     
    { Paint cell pale yellow} 
    Canvas.Brush.Color := $7FFFFF;
    Canvas.Brush.Style := bsSolid;
    Canvas.FillRect( Rect );
   
    { Paint text of cell 1,1 clipped to current cell. }
    Canvas.TextRect( Rect, x, y, Cells[1,1] );
  end;
end;

_________________
Es gibt keine dummen Fragen nur dumme Antworten!!!
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Sa 21.06.08 11:02 
user profile iconBronstein hat folgendes geschrieben:
bisher habe ich es immer nur hinbekommen (...)

Du nimmst den Code einfach irgendwo vom Internet und gibst ihn als dein eigenes Werk aus.

Das StringGrid ist dafür überhaupt nicht geeignet. Nimm eine richtige Grid-Komponente.

ausblenden volle Höhe Delphi-Quelltext
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
    { Public declarations }
  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
        // Calculate rect of whole merged cell
        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);

          // Draw the merged cell, but only the part that is supposed to be drawn
          // by this particular event call.
          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.
Einloggen, um Attachments anzusehen!
Bronstein Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 578
Erhaltene Danke: 1

WIN XP
Delphi 6 / Delphi 2006 / Delphi XE
BeitragVerfasst: Sa 21.06.08 11:30 
Danke für die Info, ich habe aber nirgendwo geschrieben, dass der Code von mir ist.
Ich verwende diesen nur und habe ein paar Anpassungen gemacht!

_________________
Es gibt keine dummen Fragen nur dumme Antworten!!!