I am facing an issue using
//This is the code to Open the DoModal dialog from MainWindow
//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
dlgFile.GetOFN().lpstrFile = fileName.GetBuffer(FILE_LIST_BUFFER_SIZE);
dlgFile.GetOFN().nMaxFile = FILE_LIST_BUFFER_SIZE;
INT_PTR nResult = dlgFile.DoModal();
//This is the code to paint the background image for ModalDialog
CPaintDC dc(this); // device context for painting
graph.DrawImage(m_pImage, (INT)0, (INT)0, (INT)rt.Width() , (INT)rt.Height() );
DefWindowProc(WM_PAINT, (WPARAM)dc.m_hDC, (LPARAM)0);
I have Found the reason behind the issue.
When we save/select the file using CFileDialog, the default behavior is to change the WorkingDirectory of the running process.
Due to this, the background image could not be found in the new location and Hence background getting erased.
In order to ensure that this does not happen, we need to use the OFN_NOCHANGEDIR flag in CFileDialog, which retains the working directory.