使用CFileDialog选择文件时出现画图问题
Painting Issue when selecting file using CFileDialog
我在代码中遇到了使用CFileDialog
的问题。
当我从ModalDialog调用CFileDialog
时,选择一个文件。一旦当前视图退出并重新打开,我的整个ModalDialog背景将被擦除。
过程遵循:
- <
- 主对话框/gh>
- 打开ModalDialog
- 打开
CFileDialog
选择文件 - 退出ModalDialog
- 重新打开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保存/选择文件时,默认的行为是改变正在运行进程的工作目录。
因此,背景图像无法在新位置找到,因此背景被擦除。
为了确保这种情况不会发生,我们需要在CFileDialog中使用OFN_NOCHANGEDIR标志,它保留了工作目录
相关文章:
- 警告处理为错误这里有什么问题
- 最小硬币更换问题(自上而下方法)
- 为"adjacent"变量赋值时出现问题
- 我的神经网络不起作用 [XOR 问题]
- 在Ubuntu 16.04上安装Cilk时出现问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 编译包含字符串的代码时遇到问题
- Project Euler问题4的错误解决方案
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 一个关于在C++中重载布尔运算符的问题
- 首要问题的答案让值班员搞错了
- setlocale的C++土耳其字符串问题
- 如何重构类层次结构以避免菱形问题
- 基于boost的程序的静态链接——zlib问题
- 使用CFileDialog选择文件时出现画图问题
- CFileDialog多文件选择的问题