1、创建一个DrawGrid控件,在窗体的OnResize事件中
//---------------------------------------------------------------------------
//当窗体大小发生改变的时候触发OnResize事件
void __fastcall TForm1::FormResize(TObject *Sender)
{
//rows行,cols列,根据当前窗体的大小以及自定义单元个的大小,计算整个窗体所需填充单元格的行列个数
int rows =DrawGrid1->Height/DrawGrid1->DefaultRowHeight;
int cols =DrawGrid1->Width/DrawGrid1->DefaultColWidth;
DrawGrid1->RowCount = rows;
DrawGrid1->ColCount = cols;
}
//--------在OnDrawCell事件中-------------------------------------------------------------------
void __fastcall TForm1::DrawGrid1DrawCell(TObject *Sender, int ACol, int ARow, TRect &Rect,
TGridDrawState State)
{
TCanvas* Canvas = DrawGrid1->Canvas;
Canvas ->Rectangle(Rect);
//该控件的高度和宽度,在进行自动填充的时候需要考虑到自己设置的高度和宽度能否在显示的窗口中正好完美匹配出一个单元格显示出来,如果不能就会出现如图2的情况
float heigth= DrawGrid1->Height;//根据这两宽和高可以进行单个单元格设置多少为妙
float width = DrawGrid1->Width;
AnsiString str = "H:";
AnsiString output = str+" "+FloatToStr(heigth)+" ";
str = "W:";
output+=str+""+FloatToStr(width);
Canvas->TextOut(Rect.Left+4,Rect.Top+2,output);
Canvas->Font->Color = clBlack;
}
//---------在窗体创建时就会触发------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
DrawGrid1->DefaultRowHeight =200;//也可以在控件属性中进行设置
DrawGrid1->DefaultColWidth = 250;
}
//---------------------------------------------------------------------------
图1 窗体还原状态
图2 窗体最大化状态