TForm* Tfrmain::FormExist(AnsiString szCaption)
{
for(int i=0; i<this->MDIChildCount; i++)
{
TForm *pForm = this->MDIChildren[i];
if(pForm->Caption == szCaption)
return pForm; // 已存在窗体,返回该窗体
}
return NULL; // 不存在窗体,返回空值
}
void __fastcall Tfrmain::actProductManageExecute(TObject *Sender)
{
//打开商品管理
TForm *pForm = this->FormExist("产品管理");
if(pForm)
pForm->SetFocus(); //如果窗体存在,则设置为焦点
else
frproduct = new Tfrproductmanage(NULL);
frproduct->Show();
}
先自定义一个函数,遍历所有子窗体,如果遍历到窗体的标题,说明窗体存在,则返回窗体;
在窗体的打开事件中,调用函数,如果存在则让该窗体获得焦点,否则创建一个新窗体并打开。