0

我正在尝试在 C++ Builder XE 的 THeaderControl 中使用 headersection 的宽度。THeaderControl 被称为“Header” 我试图对齐宽度的 TStringGrid 被称为“Grid”。在 Header 的 OnResize 事件处理程序中,我有以下代码:

void __fastcall TMainForm::HeaderResize(TObject *Sender)
{
    for (int col=0; col<Header->Sections->Count; col++)
    {
        Grid->ColWidths[col]=Header->Sections[0].Width;
    }
}

我认为可以,但它不会编译。

似乎无法找出如何访问标题的宽度。

此外,当我在此处填充某些内容以使其编译时(例如 Grid->ColWidths[col]=100),不会调用 HeaderResize 事件处理程序(即,如果我在此循环中放置断点,则运行程序并调整标题的大小,它不会到达断点)。

4

1 回答 1

1

您没有正确访问各个标题部分。你需要这样做:

void __fastcall TMainForm::HeaderResize(TObject *Sender)
{
    for (int col=0; col<Header->Sections->Count; col++)
    {
        Grid->ColWidths[col] = Header->Sections->Items[col]->Width;
    }
}

请注意,Sections[col]替换为Sections->Items[col].Width替换为->Width

至于OnResize未触发的事件,OnResize只有在整个THeaderControl调整大小时才会触发。在调整各个部分的大小时,OnSectionResize会触发该事件。该事件告诉您调整了哪个部分的大小,例如:

void __fastcall TMainForm::HeaderSectionResize(TCustomHeaderControl *HeaderControl, THeaderSection *Section)
{
    Grid->ColWidths[Section->Index] = Section->Width;
}
于 2013-05-15T01:00:34.760 回答