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;procedure TForm1.FormCreate(Sender: TObject); var i, k: Integer; begin with stringgrid1 do begin cells[1, 1] := 'A rather long line which will span cells'; for i:= 1 to colcount-1 do for k:= 2 to rowcount -1 do cells[i,k] := Format( 'Cell[%d,%d]',[i,k]); end; end;
to robinhunter 你的方法只是重新画了一下,并没有合并,且合并的高度你没有计算出?有别的方法吗?
那是一个通用的例子,仔细研究,你就知道怎么画了, 他能把右边的线去掉,你不能把它的底线去掉吗,想想吧, 所以, 要做你那样的表头,不用合并, 给你个文字居中的例子, Text :=Cells[ACol, ARow]; if ARow < 2 then begin Canvas.Brush.Color:=clBtnFace; if (ARow=0) and (ACol in[0,1,2,3]) then begin Canvas.Font.Color := clBlack; Canvas.TextOut(Rect.Left+(Rect.Right-Rect.Left-Canvas.TextWidth(Text))div 2,Rect.Top+1+RowHeights[ARow] div 2 ,Text); end; if (ARow=1) and (ACol in[0,1,2,3]) then begin Canvas.Font.Color := clBlack; Canvas.TextOut(Rect.Left+(Rect.Right-Rect.Left-Canvas.TextWidth(Text)) div 2, Rect.Top+1-RowHeights[ARow]div 2 ,Text); end;1,2行Fixed,吧Cells[0,0],Cells[1,0],Cells[2,0],Cells[3,0], Cells[0,1],Cells[1,1],Cells[2,1],Cells[3,1] 合并
to pekiee 要合并的不是表头,而是表内容,且两行数据中有些数据是不相同 要求合并的格式如上面的一样,同时请教如何在robinhunter的基础上合并 底线去掉呢,请说明原理。在这里先感谢robinhunter与你。
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;procedure TForm1.FormCreate(Sender: TObject);
var
i, k: Integer;
begin
with stringgrid1 do begin
cells[1, 1] := 'A rather long line which will span cells';
for i:= 1 to colcount-1 do
for k:= 2 to rowcount -1 do
cells[i,k] := Format( 'Cell[%d,%d]',[i,k]);
end;
end;
你的方法只是重新画了一下,并没有合并,且合并的高度你没有计算出?有别的方法吗?
他能把右边的线去掉,你不能把它的底线去掉吗,想想吧,
所以,
要做你那样的表头,不用合并,
给你个文字居中的例子,
Text :=Cells[ACol, ARow];
if ARow < 2 then
begin
Canvas.Brush.Color:=clBtnFace;
if (ARow=0) and (ACol in[0,1,2,3]) then
begin
Canvas.Font.Color := clBlack;
Canvas.TextOut(Rect.Left+(Rect.Right-Rect.Left-Canvas.TextWidth(Text))div 2,Rect.Top+1+RowHeights[ARow] div 2 ,Text);
end;
if (ARow=1) and (ACol in[0,1,2,3]) then
begin
Canvas.Font.Color := clBlack;
Canvas.TextOut(Rect.Left+(Rect.Right-Rect.Left-Canvas.TextWidth(Text)) div 2, Rect.Top+1-RowHeights[ARow]div 2 ,Text);
end;1,2行Fixed,吧Cells[0,0],Cells[1,0],Cells[2,0],Cells[3,0],
Cells[0,1],Cells[1,1],Cells[2,1],Cells[3,1]
合并
要合并的不是表头,而是表内容,且两行数据中有些数据是不相同
要求合并的格式如上面的一样,同时请教如何在robinhunter的基础上合并
底线去掉呢,请说明原理。在这里先感谢robinhunter与你。