【发布时间】:2016-10-27 07:38:59
【问题描述】:
我在代码中使用 CFileDialog 时遇到问题。
当我从 ModalDialog 调用 CFileDialog 时,选择一个文件。
退出并重新打开当前视图后,我的整个 ModalDialog 背景都会被删除。
程序如下:
- 主对话框
- 打开模态对话框
- 打开
CFileDialog用于选择文件 - 退出模态对话框
- 重新打开 ModalDialog [背景被擦除]
注意:仅当我选择一个文件时才会出现此问题。
如果我单击CFileDialog 中的取消。没有问题。
PFB,我CFileDialog使用的代码sn-p:
//This is the code to Open the DoModal dialog from MainWindow
//
void CCommonDlg::OnBnClickedButton1()
{
COSDADlg dlg;
//m_pMainWnd = &dlg;
INT_PTR nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
}
// This is the code for open CFileDialog from ModalDialog to save file
//
void COSDADlg::OnBnClickedButton1()
{
CFileDialog dlgFile(FALSE);
CString fileName;
dlgFile.GetOFN().lpstrFile = fileName.GetBuffer(FILE_LIST_BUFFER_SIZE);
dlgFile.GetOFN().nMaxFile = FILE_LIST_BUFFER_SIZE;
INT_PTR nResult = dlgFile.DoModal();
fileName.ReleaseBuffer();
}
//This is the code to paint the background image for ModalDialog
//
void COSDADlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
Graphics graph(dc.m_hDC);
CRect rt;
GetWindowRect(&rt);
graph.DrawImage(m_pImage, (INT)0, (INT)0, (INT)rt.Width() , (INT)rt.Height() );
DefWindowProc(WM_PAINT, (WPARAM)dc.m_hDC, (LPARAM)0);
}
【问题讨论】:
-
DefWindowProc(WM_PAINT, (WPARAM)dc.m_hDC, (LPARAM)0);在我看来很可疑,请尝试删除它。 -
@MichaelWalz :我尝试了您删除 DefWindowProc 功能的建议。但是,这并没有解决问题,从表面上看,我认为这不是画家的问题。似乎与 CFileDialog 更相关。只是为了重新迭代,当我取消 CFileDialog 时不会发生这种情况。而只有当我从中选择一个文件并保存时。
-
您的代码中有一个错误,但我们看不到它。您从
WM_PAINT处理程序调用窗口过程这一事实表明,对 MFC 的工作原理缺乏令人担忧的理解。如果这是您唯一的错误,我会感到惊讶。 -
@IInspectable :好的。让我也纠正一下。还有一点,如果它可能有任何意义的话,那就是这个问题仅在 WinPE 中使用时才会出现。而且,在普通窗口操作系统(windows 7 或 windows 8)上工作正常。
-
这可能并不重要。您的代码表现出未定义的行为,并且在不同平台上具有不同的可观察效果。 “看起来工作正常” 是未定义行为的合法形式。它仍然未定义。但是,如果没有minimal reproducible example,我们将无能为力。
标签: c++ winapi visual-c++ mfc winpe