2

CFileDialog在代码中使用时遇到问题。

当我CFileDialog从 ModalDialog 调用时,选择一个文件。退出并重新打开当前视图后,我的整个 ModalDialog 背景都会被删除。

遵循的程序:

  1. 主对话框
  2. 打开的模态对话框
  3. 打开CFileDialog用于选择文件
  4. 退出模态对话框
  5. 重新打开 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);

}
4

1 回答 1

2

我找到了问题背后的原因。

当我们使用 CFileDialog 保存/选择文件时,默认行为是更改正在运行的进程的 WorkingDirectory。

因此,在新位置找不到背景图像,因此背景被删除。

为了确保不会发生这种情况,我们需要在 CFileDialog 中使用 OFN_NOCHANGEDIR 标志,该标志保留了工作目录。

于 2016-11-10T12:59:12.567 回答