除非隐藏cframewind,否则文件对话框不会刷新
File Dialog does not get refreshed unless CFrameWnd is hidden
我正在更新一些不能在Win7或更高版本上运行的旧软件。所以,我正在重建一些使用最新win32更新的MFC库。
现在我有两个问题:
- MessageBox出现在cframewind后面,所以它不能被访问,发送应用程序停止。
- 打开对话框(无论是基于CFileDialog还是IFileDilog)在更改文件类型时不刷新
然而,如果隐藏cframewind,这两个问题都解决了。或者,在MessageBox的情况下,如果您编写:PostMessage(0x118);其实我也不知道为什么。
我一定是漏掉了什么。
当使用从IFileDialog继承的OpenFileDialog类时,我也有另一个问题。是当关闭这个对话框而不选择文件时,应用程序崩溃。
//--targetver.h
#pragma once
#include <sdkddkver.h>
//--stdafx.h:
#ifndef CS_EXTRALEAN
#define CS_EXTRALEAN
#endif
#pragma once
#include "targetver.h"
#include<afxwin.h>
#include<afxext.h>
#include<afxcmn.h>
//--stdafx.cpp
#include "stdafx.h"
//--CMainWnd.h
#pragma once
class CMainWnd : public CFrameWnd
{
public:
CMainWnd();
~CMainWnd();
afx_msg void OnPaint();
afx_msg void OnLButtonDown(UINT, CPoint);
DECLARE_MESSAGE_MAP()
};
//--CMainWnd.cpp
#include "stdafx.h"
#include"CMainWnd.h"
BEGIN_MESSAGE_MAP(CMainWnd, CFrameWnd)
ON_WM_PAINT()
ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()
CMainWnd::CMainWnd()
: CFrameWnd()
{
CString class_name = AfxRegisterWndClass(
CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS,
AfxGetApp()->LoadStandardCursor(IDC_ARROW),
(HBRUSH)::GetStockObject(BLACK_BRUSH),
AfxGetApp()->LoadStandardIcon(IDI_ASTERISK));
HRESULT hResult = this->Create(
class_name,
L"This is CMainWnd",
WS_OVERLAPPEDWINDOW,
this->rectDefault,
NULL,
NULL,
0,
NULL);
}
CMainWnd::~CMainWnd() { }
void CMainWnd::OnPaint()
{ }
void CMainWnd::OnLButtonDown(UINT, CPoint)
{
MessageBox(L"HELLO MFC", L"MFC", MB_OK);
}
//--CAppWnd.h
#pragma once
class CAppWnd : public CWinApp
{
public:
CAppWnd();
~CAppWnd();
BOOL InitInstance();
DECLARE_MESSAGE_MAP()
};
//--CAppWnd.cpp
#include "stdafx.h"
#include "CAppWnd.h"
#include "CMainWnd.h"
BEGIN_MESSAGE_MAP(CAppWnd, CWinApp)
END_MESSAGE_MAP()
CAppWnd::CAppWnd()
:CWinApp()
{ }
CAppWnd::~CAppWnd()
{ }
BOOL CAppWnd::InitInstance()
{
this->m_pMainWnd = new CMainWnd;
this->m_pMainWnd->ShowWindow(m_nCmdShow);
return CWinApp::InitInstance();
}
CAppWnd The_App;
有一个简单的问题。您覆盖了OnPaint
,但没有调用默认过程。OnPaint
处理WM_PAINT
消息,它不会原谅这个错误。
void CMainWnd::OnPaint()
{
CFrameWnd::OnPaint(); //<= this was missing
//custom paint...
//CClientDC dc(this);
//dc.TextOut(0, 0, L"test");
//dc is automatically released...
}
或者您可以使用CPaintDC
,它是BeginPaint
/EndPaint
API的包装器
void CMainWnd::OnPaint()
{
CPaintDC dc(this);
//custom paint...
//dc.TextOut(0, 0, L"test");
//dc is automatically released...
}
如果你不做任何绘画在这个框架窗口,然后删除整个CMainWnd::OnPaint()
函数和相应的ON_WM_PAINT
消息。
以上更改应该修复您的错误。我会重写剩下的代码,让它先调用默认的重写。例子:
#include "stdafx.h"
#include "resource.h"
class CMainWnd : public CFrameWnd
{
public:
CMainWnd();
~CMainWnd();
afx_msg void OnPaint();
afx_msg void OnLButtonDown(UINT, CPoint);
DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP(CMainWnd, CFrameWnd)
ON_WM_PAINT()
ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()
CMainWnd::CMainWnd() : CFrameWnd() {}
CMainWnd::~CMainWnd() {}
void CMainWnd::OnPaint()
{
CFrameWnd::OnPaint();
}
void CMainWnd::OnLButtonDown(UINT f, CPoint pt)
{
CFrameWnd::OnLButtonDown(f, pt);
CFileDialog dlg(TRUE, 0, 0, 0,
L"All files|*.*|"
L"Text files|*.txt;*.txt||" , this);
if (dlg.DoModal() == IDOK)
MessageBox(dlg.GetPathName(), L"MFC", MB_OK);
}
class CAppWnd : public CWinApp
{
public:
BOOL InitInstance();
};
BOOL CAppWnd::InitInstance()
{
CWinApp::InitInstance();
CMainWnd *frame = new CMainWnd;
CString class_name = AfxRegisterWndClass(
CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS,
AfxGetApp()->LoadStandardCursor(IDC_ARROW),
(HBRUSH)::GetStockObject(BLACK_BRUSH),
AfxGetApp()->LoadStandardIcon(IDI_ASTERISK));
frame->Create(class_name, L"This is CMainWnd",
WS_OVERLAPPEDWINDOW, CFrameWnd::rectDefault, NULL, NULL, 0, NULL);
frame->ShowWindow(m_nCmdShow);
m_pMainWnd = frame;
return TRUE;
}
CAppWnd The_App;
请注意,您可以直接调用静态成员,例如CFrameWnd::rectDefault
,它不会导致错误,但它使代码更清晰。
相关文章:
- 选择文件对话框错误
- 在C++中创建文件夹选取器对话框的最简单方法是什么?
- 在 Win32 应用中,如果未选择文件夹并且用户单击"确定",则文件夹选择对话框将关闭
- C++ gtkmm:文件选择器对话框不适用于任何驱动器
- 使用文件对话框选择包含特定文件类型的文件夹?
- 用静态版本的QT编译时,文件对话框崩溃
- 如果我尝试将对话框从一个项目移动到另一个项目,我是否需要从 rc 文件中复制 DESIGNINFO、对话框信息和AFX_
- 如何获取资源文件中对话框的按钮控件的大小和位置
- 使用MFC在对话框上拖放文件
- 如何使用Ifileopendialog打开 *模态 *文件对话框
- 如何使用3d的max 2016 SDK(C++)在插件中添加保存文件对话框?
- 如何将.wav文件保存在MFC中(使用文件对话框)
- 如何为文件对话框过滤器设置通配符
- 是否可以从资源文件指定要使用的 COM 对话框
- OpenFileName对话框返回亚洲字母而不是文件路径
- 有没有办法选择通过标准的OPENFILE对话框向用户显示哪些文件
- 保存/加载对话框文本文件
- 使用 firebreath 插件打开文件夹对话框并异步返回文件夹选择,这样 java 脚本就不会被阻止
- 如何使用进度对话框在wxWidgets中下载文件
- 如何从 MFC 中的对话框头文件访问静态变量