首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用CFileDialog选择文件时出现的绘图问题

使用CFileDialog选择文件时出现的绘图问题
EN

Stack Overflow用户
提问于 2016-10-27 07:38:59
回答 1查看 147关注 0票数 2

我在代码中使用CFileDialog面临一个问题。

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

所遵循的程序:

  1. 主对话框
  2. 开放ModalDialog
  3. 打开CFileDialog以选择文件
  4. 出口ModalDialog
  5. 重新打开ModalDialog背景被擦除

注意:只有当我选择一个文件时,才会出现这个问题。如果我单击CFileDialog中的Cancel。没有问题。

PFB,我的CFileDialog使用的代码片段:

代码语言:javascript
复制
//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);

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-10 12:59:12

我已经找到了这个问题背后的原因。

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

由于这一点,背景图像无法在新的位置找到,因此背景被删除。

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40278875

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档