我正在使用C ++ Builder XE,并且对TCheckBox的“显示”属性有疑问。
我有TForm
(ChannelConfigForm),其中有一个TGroupBox
(AlarmsGroupBox),其中有一个TCheckBox
(A4_en_Xbox)。
有时候,我看不到表格上的某些控件。
根据帮助文档: “如果某个组件的Visible属性及其父层次结构中的所有父项都为true,则表示显示为true。如果包含该控件的父项之一的Visible属性值为false,则显示可能为true或false。 显示是一个只读属性。“
为了找出发生了什么,我编写了以下函数来调试程序: (注意:这个函数中的debugf只是我编写的一个调试语句,类似于printf,它将调试内容写入表单)
void ShowParentTree(TControl *Control)
{
wchar_t Name[32];
static int level=0;
TWinControl *wc;
level++;
if (level==1)
debugf(L"Parents of control \"%s\" (%s) :",
Control->Name.c_str(),
Control->ClassName().c_str());
// Display what Control has as parents and if they're visible and showing
debugf(L"level %d : %s->Visible = %s",level,
Control->Name.c_str(),
Control->Visible?L"true":L"false");
wc=(TWinControl *)Control;
debugf(L"level %d : %s->Showing = %s",level,
wc->Name.c_str(),
wc->Showing?L"true":L"false");
if (Control->Parent)
ShowParentTree((TControl *)Control->Parent);
level--;
}
有时当我展示ChannelConfigForm时,我得到以下内容:
Parents of control "A4_en_Xbox" (TCheckBox) :
level 1 : A4_en_Xbox->Visible = true
level 1 : A4_en_Xbox->Showing = false
level 2 : AlarmsGroupBox->Visible = true
level 2 : AlarmsGroupBox->Showing = true
level 3 : ChannelConfigForm->Visible = true
level 3 : ChannelConfigForm->Showing = true
我理解为A4_en_Xbox->显示属性是假的,当我认为它应该是真的时。
答案 0 :(得分:0)
检查A4_en_Xbox->HandleAllocated()
返回的内容。如果Showing
返回false,则HandleAllocated()
将为false。 VCL不保证TWinControl派生的组件始终始终分配HWND
。有时它会在内部释放HWND
并在需要时重新创建它们。
此外,您的代码中存在一个小的逻辑错误。并非所有TControl
个后代都来自TWinControl
,但您的代码假设输入TControl
始终是TWinControl
后代。您需要检查这一点,以便您可以避免访问非Showing
控件的TWinControl
属性。您也可以完全取消递归,只需使用简单的循环:
void ShowParentTree(TControl *Control)
{
if (!Control)
return;
int level = 0;
TWinControl *wc = dynamic_cast<TWinControl*>(Control);
debugf(L"Parents of control \"%s\" (%s) :",
Control->Name.c_str(),
Control->ClassName().c_str());
// Display what Control has as parents and if they're visible and showing
do
{
level++;
if (wc)
{
debugf(L"level %d : %s Visible = %s, Showing = %s, HandleAllocated = %s",
level,
Control->Name.c_str(),
Control->Visible ? L"true" : L"false",
wc->Showing ? L"true" : L"false",
wc->HandleAllocated() ? L"true" : L"false");
}
else
{
debugf(L"level %d : %s Visible = %s",
level,
Control->Name.c_str(),
Control->Visible ? L"true" : L"false");
}
wc = Control->Parent;
Control = wc;
}
while (Control);
}