我一直在尝试将此 Delphi 代码与 C++ Builder XE 一起使用。我手动将语言转换为 C++ Builder 语言。
此过程运行正常。当我尝试最大化保存 DBGrid 的表单时,此过程成功地根据网格宽度拆分列宽度。但是,如果我尝试使用表单右侧或左侧的鼠标控制来最小化表单宽度,则此过程不会将列宽调整为每列上值的长度或标签的长度。
有什么办法可以解决吗?或者有人有其他程序来解决这样的问题吗?
编辑:好的,这是代码。我已经手动将上面的代码转换成这个代码:
void __fastcall TfmMain::FixGridColumnsWidth(TDBGrid *DBGrid)
{
int TotalColumnWidth, ColumnCount, GridClientWidth, Filler, i;
ColumnCount = DBGrid->Columns->Count;
if (ColumnCount == 0)
exit;
// compute total width used by grid columns and vertical lines if any
TotalColumnWidth = 0;
for ( i = 0; i <=ColumnCount-1; i++)
TotalColumnWidth = TotalColumnWidth + DBGrid->Columns->Items[i]->Width;
if ( DBGrid->Options.Contains(dgColLines) )
// include vertical lines in total (one per column)
TotalColumnWidth = TotalColumnWidth + ColumnCount;
// compute grid client width by excluding vertical scroll bar, grid indicator,
// and grid border
GridClientWidth = DBGrid->Width - GetSystemMetrics(SM_CXVSCROLL);
if ( DBGrid->Options.Contains(dgIndicator) ) {
GridClientWidth = GridClientWidth - IndicatorWidth;
if ( DBGrid->Options.Contains(dgColLines) )
GridClientWidth--;
}
if ( DBGrid->BorderStyle == bsSingle ) {
if ( DBGrid->Ctl3D ) { // border is sunken (vertical border is 2 pixels wide)
GridClientWidth = GridClientWidth - 4;
}
else { // border is one-dimensional (vertical border is one pixel wide)
GridClientWidth = GridClientWidth - 2;
}
}
// adjust column widths
if ( TotalColumnWidth < GridClientWidth ) {
Filler = (GridClientWidth - TotalColumnWidth) / ColumnCount;
for ( i = 0; i <= ColumnCount-1; i++ )
DBGrid->Columns->Items[i]->Width = DBGrid->Columns->Items[i]->Width + Filler;
}
else
if ( TotalColumnWidth > GridClientWidth ) {
Filler = (TotalColumnWidth - GridClientWidth) / ColumnCount;
if ( (TotalColumnWidth - GridClientWidth) % ColumnCount != 0 )
Filler++;
for ( i = 0; i <= ColumnCount-1; i++ )
DBGrid->Columns->Items[i]->Width = DBGrid->Columns->Items[i]->Width - Filler;
}
}
提前致谢。