怎么写个子程序:在StringGrid某单元Cells[x][y]的中央显示串(不是所有单元)
如何写个子程序:在StringGrid某单元Cells[x][y]的中央显示串(不是所有单元)?
即在StringGrid的y行x列的单元格中央显示指定的串s,
不要告诉我针对所有单元格的方法(这会的)。
------解决方案--------------------
void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol,
int ARow, TRect &Rect, TGridDrawState State)
{
TStringGrid *grid = (TStringGrid*)Sender;
if( ACol==1 && ARow==1)//第2行第二列
grid->Canvas->TextOut(Rect.left + 2,Rect.top + Rect.Height() / 2 - grid->Canvas->TextHeight(grid->Cells[ACol][ARow]) / 2, grid->Cells[ACol][ARow]);
}
------解决方案--------------------
改一下,有bug
void GridCenterDisp(TStringGrid * StringGrid,int x,int y,String s)
{
String str=s.Trim();
int BlkWidth=StringGrid->Canvas->TextWidth(" ");
int StrWidth=StringGrid->Canvas->TextWidth(str);
int CellWidth=StringGrid->ColWidths[x];
int BlkCount=(CellWidth-StrWidth)/(BlkWidth+BlkWidth);
if (BlkCount>0)
str=AnsiString::StringOfChar(' ',BlkCount)+str;
StringGrid->Cells[x][y]=str;
}
即在StringGrid的y行x列的单元格中央显示指定的串s,
不要告诉我针对所有单元格的方法(这会的)。
------解决方案--------------------
void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol,
int ARow, TRect &Rect, TGridDrawState State)
{
TStringGrid *grid = (TStringGrid*)Sender;
if( ACol==1 && ARow==1)//第2行第二列
grid->Canvas->TextOut(Rect.left + 2,Rect.top + Rect.Height() / 2 - grid->Canvas->TextHeight(grid->Cells[ACol][ARow]) / 2, grid->Cells[ACol][ARow]);
}
------解决方案--------------------
改一下,有bug
void GridCenterDisp(TStringGrid * StringGrid,int x,int y,String s)
{
String str=s.Trim();
int BlkWidth=StringGrid->Canvas->TextWidth(" ");
int StrWidth=StringGrid->Canvas->TextWidth(str);
int CellWidth=StringGrid->ColWidths[x];
int BlkCount=(CellWidth-StrWidth)/(BlkWidth+BlkWidth);
if (BlkCount>0)
str=AnsiString::StringOfChar(' ',BlkCount)+str;
StringGrid->Cells[x][y]=str;
}