我有一个如下所示的无模式对话框,

CPlotDlg * newd = new CPlotDlg ();
newd->Create(IDD_PLOT,this->GetParentOwner());
newd->SetParent(this->GetParentFrame()->GetParent());
newd->ShowWindow(SW_SHOW);


我想在其他窗口(而不是父窗口)关闭时关闭此对话框。我该如何实现?
谢谢。

最佳答案

只需将CPlotDlg *保存到其他窗口中即可关闭CPlotDlg窗口。

如果更接近的窗口是SomeWhereDlg,

class SomeWhereDlg
{
  public:
  ...
  ...
  CPlotDlg* m_plotDlg;
};

void SomeWhereDlg::SetPlotDlg(CPlotDlg* plotDlg)
{
  ASSERT(plotDlg);
  if(plotDlg == nullptr) { return;}

  m_plotDlg = plotDlg;
}


然后,在创建CPlotDlg窗口时,保存指针。

CPlotDlg* newd = new CPlotDlg ();
//Save newd(CPlotDlg*) to somewhere
//i.e) specific window which will close this newd window
//SomeWhereDlg->SetPlotDlg(newd);

newd->Create(IDD_DIALOG1,this->GetParentOwner());
newd->SetParent(this);
newd->ShowWindow(SW_SHOW);


如果发生关闭事件,只需通过m_plotDlg调用Close()或delete等。

07-24 12:42