我在代码中使用 CFileDialog
时遇到问题。
当我从 ModalDialog 调用 CFileDialog
以选择一个文件时。
一旦退出并重新打开当前 View ,我的整个 ModalDialog 背景就会被删除。
程序如下:
- 主对话框
- 打开 ModalDialog
- 打开
CFileDialog
以选择文件 - 退出模态对话框
- 重新打开 ModalDialog [背景被删除]
注意:只有当我选择一个文件时才会出现此问题。
如果我在 CFileDialog
中单击取消。没有问题。
PFB,我的CFileDialog
的代码片段使用:
//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);
}
最佳答案
我已经找到问题背后的原因。
当我们使用 CFileDialog 保存/选择文件时,默认行为是更改正在运行的进程的 WorkingDirectory。
因此,无法在新位置找到背景图像,因此背景被删除。
为了确保不会发生这种情况,我们需要在 CFileDialog 中使用 OFN_NOCHANGEDIR 标志,它会保留工作目录。
关于c++ - 使用 CFileDialog 选择文件时的绘画问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40278875/