贴段代码TstringGrid 的行列合并研究 ========================== unit Unit1;//建立一工程, //粘贴本单元代码即可看 STringGrid 行列合并效果 //但发现非固定行非固定列的合并效果不好 interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Db, ADODB, DBTables, Grids;//注意这里要引用type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); procedure SGDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); procedure SGTopLeftChanged(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.DFM}// 以下 StringGrid 为固定行,固定列的合并所必须进行的处理 // 非固定行,非固定列的合并效果不好 var sg:TStringGrid; procedure TForm1.FormCreate(Sender: TObject); var i,j:integer ; begin Sg:=TStringGrid.Create(self);with SG do begin parent:=self; align:=alclient; DefaultDrawing:=false; FixedColor:=clYellow; RowCount:=30; ColCount:=20; FixedCols:=1; FixedRows:=1; GridLineWidth:=0; Options:=Options+[goEditing]-[goVertLine,goHorzLine,goRangeSelect]; OnDrawCell:=SGDrawCell; OnTopLeftChanged:=SGTopLeftChanged; Canvas.Font.name:='宋体'; Canvas.Font.Size:=10; for i:=0 to colCount-1 do for j:=0 to RowCount-1 do cells[i,j]:=Format('%d行%d列',[j,i]); for i:=0 to colCount-1 do cells[i,0]:=Format('第%d列',[i]); for i:=0 to RowCount-1 do cells[0,i]:=Format('第%d行',[i]); Cells[0,0]:=' 左上角'; Cells[1,0]:='AA这是列合并BB'; Cells[0,1]:='A这是行'#10'合并BB'; Cells[1,1]:='1111111'; Cells[1,2]:='1111222'; Cells[2,1]:='2222111'; Cells[2,2]:='2222222'; end; end;//重载 OnDrawCell 事件 procedure TForm1.SGDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); var r:TRect; d:TStringGrid; s:string; ts:TStrings; i,n:integer; fixed:Boolean; begin d:=TStringGrid(sender); if (Acol=2) and (ARow=0) then begin r.left:=Rect.left-1-d.colwidths[ACol-1]; r.top:=rect.top-1; r.right:=rect.right; r.bottom:=rect.bottom; s:=d.cells[ACol-1,ARow]; end else if (Acol=1) and (ARow=0) then begin r.left:=Rect.left-1; r.top:=rect.top-1; r.right:=rect.right+d.colwidths[ACol+1]; r.bottom:=rect.bottom; s:=d.cells[ACol,ARow]; end //////////以上列合并 else if (Acol=0) and (ARow=2) then begin r.left:=Rect.left-1; r.top:=rect.top-1-d.RowHeights[ARow-1]; r.right:=rect.right; r.bottom:=rect.bottom; s:=d.cells[ACol,ARow-1]; end else if (Acol=1) and (ARow=0) then begin r.left:=Rect.left-1; r.top:=rect.top-1; r.right:=rect.right; r.bottom:=rect.bottom+d.RowHeights[ARow+1]; s:=d.cells[ACol,ARow]; end ////////以上为行合并 else begin r.left:=Rect.left-1; r.top:=rect.top-1; r.right:=rect.right; r.bottom:=rect.bottom; s:=d.cells[ACol,ARow]; end;d.Canvas.brush.color:=d.color; d.canvas.Font.color:=$ff0000;Fixed:=false; if (Arow<d.FixedRows) or (ACol<d.Fixedcols) then begin d.Canvas.brush.color:=d.FixedColor; d.Canvas.Font.color:=$ff00ff; Fixed:=True; //d.Canvas.Font.style:=d.Canvas.Font.style+[fsBold]; end; if gdfocused in state then begin d.canvas.Brush.color:=$00ff00; end; if fixed then begin d.Canvas.Pen.color:=$0; d.canvas.Rectangle(r); d.Canvas.Pen.color:=$f0f0f0; d.Canvas.Pen.Width:=2; d.canvas.Moveto(r.left+1,r.top+2); d.canvas.Lineto(r.left+r.right,r.top+2); d.Canvas.Pen.color:=$808080; d.Canvas.Pen.Width:=1; d.canvas.Moveto(r.Left+1,r.bottom-1); d.canvas.Lineto(r.left+r.right,r.bottom-1);end else begin d.Canvas.Pen.color:=$0; d.Canvas.Pen.Width:=1; d.canvas.Rectangle(r); end; n:=r.top+4; ts:=TStringList.Create; ts.CommaText:=s; for i:=0 to ts.Count-1 do begin d.canvas.Textout(r.left+4,n,ts[i]); inc(n,d.RowHeights[ARow]); end; end;//重载 OnTopLeftChange事件,特别是行的合并 procedure TForm1.SGTopLeftChanged(Sender: TObject); var d:TStringGrid; begin d:=TStringGrid(Sender); d.Cells[0,1]:=d.Cells[0,1]; d.Cells[0,2]:=d.Cells[0,2]; end;end.
Tms 系列 的AdvMemo
Email : [email protected]
Email : [email protected]
==========================
unit Unit1;//建立一工程,
//粘贴本单元代码即可看 STringGrid 行列合并效果
//但发现非固定行非固定列的合并效果不好
interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Db, ADODB, DBTables, Grids;//注意这里要引用type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure SGDrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
procedure SGTopLeftChanged(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}// 以下 StringGrid 为固定行,固定列的合并所必须进行的处理
// 非固定行,非固定列的合并效果不好
var
sg:TStringGrid;
procedure TForm1.FormCreate(Sender: TObject);
var
i,j:integer ;
begin
Sg:=TStringGrid.Create(self);with SG do
begin
parent:=self;
align:=alclient;
DefaultDrawing:=false;
FixedColor:=clYellow;
RowCount:=30;
ColCount:=20;
FixedCols:=1;
FixedRows:=1;
GridLineWidth:=0;
Options:=Options+[goEditing]-[goVertLine,goHorzLine,goRangeSelect];
OnDrawCell:=SGDrawCell;
OnTopLeftChanged:=SGTopLeftChanged;
Canvas.Font.name:='宋体';
Canvas.Font.Size:=10; for i:=0 to colCount-1 do
for j:=0 to RowCount-1 do
cells[i,j]:=Format('%d行%d列',[j,i]); for i:=0 to colCount-1 do
cells[i,0]:=Format('第%d列',[i]);
for i:=0 to RowCount-1 do
cells[0,i]:=Format('第%d行',[i]); Cells[0,0]:=' 左上角';
Cells[1,0]:='AA这是列合并BB';
Cells[0,1]:='A这是行'#10'合并BB';
Cells[1,1]:='1111111';
Cells[1,2]:='1111222';
Cells[2,1]:='2222111';
Cells[2,2]:='2222222';
end;
end;//重载 OnDrawCell 事件
procedure TForm1.SGDrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
r:TRect;
d:TStringGrid;
s:string;
ts:TStrings;
i,n:integer;
fixed:Boolean;
begin
d:=TStringGrid(sender);
if (Acol=2) and (ARow=0) then
begin
r.left:=Rect.left-1-d.colwidths[ACol-1];
r.top:=rect.top-1;
r.right:=rect.right;
r.bottom:=rect.bottom;
s:=d.cells[ACol-1,ARow];
end else
if (Acol=1) and (ARow=0) then
begin
r.left:=Rect.left-1;
r.top:=rect.top-1;
r.right:=rect.right+d.colwidths[ACol+1];
r.bottom:=rect.bottom;
s:=d.cells[ACol,ARow];
end //////////以上列合并
else
if (Acol=0) and (ARow=2) then
begin
r.left:=Rect.left-1;
r.top:=rect.top-1-d.RowHeights[ARow-1];
r.right:=rect.right;
r.bottom:=rect.bottom;
s:=d.cells[ACol,ARow-1];
end else
if (Acol=1) and (ARow=0) then
begin
r.left:=Rect.left-1;
r.top:=rect.top-1;
r.right:=rect.right;
r.bottom:=rect.bottom+d.RowHeights[ARow+1];
s:=d.cells[ACol,ARow];
end ////////以上为行合并
else
begin
r.left:=Rect.left-1;
r.top:=rect.top-1;
r.right:=rect.right;
r.bottom:=rect.bottom;
s:=d.cells[ACol,ARow];
end;d.Canvas.brush.color:=d.color;
d.canvas.Font.color:=$ff0000;Fixed:=false;
if (Arow<d.FixedRows) or (ACol<d.Fixedcols) then
begin
d.Canvas.brush.color:=d.FixedColor;
d.Canvas.Font.color:=$ff00ff;
Fixed:=True;
//d.Canvas.Font.style:=d.Canvas.Font.style+[fsBold];
end;
if gdfocused in state then
begin
d.canvas.Brush.color:=$00ff00;
end;
if fixed then
begin
d.Canvas.Pen.color:=$0;
d.canvas.Rectangle(r); d.Canvas.Pen.color:=$f0f0f0;
d.Canvas.Pen.Width:=2;
d.canvas.Moveto(r.left+1,r.top+2);
d.canvas.Lineto(r.left+r.right,r.top+2); d.Canvas.Pen.color:=$808080;
d.Canvas.Pen.Width:=1;
d.canvas.Moveto(r.Left+1,r.bottom-1);
d.canvas.Lineto(r.left+r.right,r.bottom-1);end else
begin
d.Canvas.Pen.color:=$0;
d.Canvas.Pen.Width:=1;
d.canvas.Rectangle(r);
end;
n:=r.top+4;
ts:=TStringList.Create;
ts.CommaText:=s;
for i:=0 to ts.Count-1 do
begin
d.canvas.Textout(r.left+4,n,ts[i]);
inc(n,d.RowHeights[ARow]);
end;
end;//重载 OnTopLeftChange事件,特别是行的合并
procedure TForm1.SGTopLeftChanged(Sender: TObject);
var
d:TStringGrid;
begin
d:=TStringGrid(Sender);
d.Cells[0,1]:=d.Cells[0,1];
d.Cells[0,2]:=d.Cells[0,2];
end;end.