![26153942_gIYS.gif](http://static.oschina.net/uploads/img/201409/26153942_gIYS.gif)
一般修改 TStringGrid 的 Options 直接在设计时选一下 True 或 False 就行了; 代码中可以像下面操作:
StringGrid1.Options := [goFixedVertLine];
StringGrid1.Options := [goFixedVertLine, goVertLine, goColSizing];
做完这个例子发现不太初级了, 但代码很简单:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, Grids;
type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
GroupBox1: TGroupBox;
procedure FormCreate(Sender: TObject);
procedure GroupBox1Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses
TypInfo; {需要这个单元获取 StringGrid1.Options 选项名称}
var
cb: TCheckBox; {准备动态生成 15 个 TCheckBox}
GridOpt: TGridOptions; {StringGrid1.Options 是一个 TGridOptions 类型(集合)}
{同时也应知道: TGridOptions 集合是根据 TGridOption 枚举定义的}
{窗体建立时, 干了不少事}
procedure TForm1.FormCreate(Sender: TObject);
var
i,j: Integer;
gb: TGroupBox; {为简化 GroupBox1 名称用}
begin
StringGrid1.RowCount := 16; {设 StringGrid1 为 16 行}
{给每个单元赋值}
with StringGrid1 do
for i := 0 to ColCount - 1 do
for j := 0 to RowCount - 1 do
Cells[i,j] := Format('%.1x%.1x',[i,j]);
{下面只是动态生成 15 个 TCheckBox, 并赋予标题与 OnClick 功能}
gb := Self.GroupBox1; {用 gb 简化 GroupBox1 的称谓}
j := 16; {用来记录 TCheckBox 的纵向位置}
for i := 0 to 14 do
begin
if cb<>nil then Inc(j,cb.Height);
cb := TCheckBox.Create(Self);
cb.Parent := gb;
cb.Left := 6; cb.Top := j;
cb.Caption := GetEnumName(TypeInfo(TGridOption),i);
cb.OnClick := gb.OnClick;
{本来可以没有下面这句, 不然会隐藏部分名称, 可能是中文系统的支持问题}
cb.Width := Canvas.TextWidth(GetEnumName(TypeInfo(TGridOption),5))+ 20;
end;
end;
{这个主要是让 TCheckBox 调用的}
procedure TForm1.GroupBox1Click(Sender: TObject);
var
i: Integer;
begin
GridOpt := [];
for i := 0 to GroupBox1.ControlCount - 1 do
begin
if TCheckBox(GroupBox1.Controls[i]).Checked then
GridOpt := GridOpt + [TGridOption(i)];
end;
StringGrid1.Options := GridOpt;
end;
end.
附上窗体设计源码:
object Form1: TForm1
Left = 201
Top = 31
Caption = 'Form1'
ClientHeight = 277
ClientWidth = 468
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
Position = poDesigned
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object StringGrid1: TStringGrid
Left = 0
Top = 0
Width = 335
Height = 277
Align = alClient
TabOrder = 0
ExplicitWidth = 328
end
object GroupBox1: TGroupBox
Left = 335
Top = 0
Width = 133
Height = 277
Align = alRight
Caption = 'GroupBox1'
TabOrder = 1
OnClick = GroupBox1Click
ExplicitLeft = 328
end
end