BCB中StringGrid中文字居中
BCB或Delphi中,StringGrid中的文字無法置中,一般只能自己寫代碼来,下面的代碼可以完成這個功能,同時還支持列寬自動置中。
void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol,
int ARow, TRect &Rect, TGridDrawState State)
{
AnsiString Str=" ";
((TStringGrid*)Sender)->Canvas->Font=((TStringGrid*)Sender)->Font;
for(int i=0;i<((TStringGrid*)Sender)->RowCount;i++){
if(((TStringGrid*)Sender)->Cells[ACol][i].Length()>Str.Length())
Str=((TStringGrid*)Sender)->Cells[ACol][i];
}
int colWidth=((TStringGrid*)Sender)->Canvas->TextWidth(Str)+4;
if(colWidth>((TStringGrid*)Sender)->ColWidths[ACol])
((TStringGrid*)Sender)->ColWidths[ACol]=colWidth;
if(ACol==0||ARow==0)
((TStringGrid*)Sender)->Canvas->Brush->Color=clBtnFace;
else
((TStringGrid*)Sender)->Canvas->Brush->Color=clWhite;
((TStringGrid*)Sender)->Canvas->FillRect(Rect);
DrawText(((TStringGrid*)Sender)->Canvas->Handle,((TStringGrid*)Sender)->Cells[ACol][ARow].c_str(),-1,(RECT*)&Rect,DT_SINGLELINE|DT_VCENTER|DT_CENTER);
}
留言列表