【发布时间】:2014-01-26 14:07:58
【问题描述】:
我希望有人向我解释为什么以下在 CDialog 派生对象中使用 CScrollView 派生对象有效,以及这种方法是否存在隐藏问题。
我担心将 CDialog 转换为 CFrameWnd,以便使用 CFrameWnd 类的 CreateView() 方法为 CDialog 对象中的 CScrollView 对象创建 Document/View 对。
我正在构建一个 MFC DLL,它将提供一系列 GUI 函数来显示和允许在一次显示一个页面的旧应用程序中编辑某些类型的信息。
其中一个是提供一种在对话框中显示报表的方法,该对话框以可滚动组件的形式呈现报表,允许用户查看整个报表。
研究在 CDialog 派生的 GUI 对象中使用 CSrollView 派生控件时,我发现了这篇文章,Creating a View in Dialog (An easy way),因为我在使用 CScrollView 时遇到了许多关于 CDialog 关闭异常的问题。我还在“警告:创建没有 CDocument 的窗格”的调试输出窗口中看到警告。我不再看到了。
使用文章中的基本概念,我的代码似乎在 Windows XP 中的 Visual Studio 2005 调试器中运行良好。
我在 CDialog 派生类中用于在OnInitDialog() 中初始化的代码如下。我首先创建文档并将文本行放入内存区域,然后为 CDialog 派生对象的构造函数提供文档地址m_pDocument,然后在OnInitDialog() 函数中使用该地址。
BOOL CScrollReportDialog::OnInitDialog ()
{
// Get the client area size of the dialog we are putting the
// CScrollView into and pull the right edge in sufficient to
// clear buttons on the right hand side of the dialog.
RECT rectSize;
GetClientRect (&rectSize);
rectSize.right -= 120;
// allocate and set up the view document context linking the view
// to a particular document, in our case a CScrollDocument.
CCreateContext pContext;
pContext.m_pCurrentDoc = m_pDocument;
pContext.m_pNewViewClass = RUNTIME_CLASS(CScrollReport);
// Cast the pointer to this dialog into a CFrameWnd pointer allowing
// us to access the CFrameWnd methods. Both CDialog and CFrameWnd are
// derived from CWnd so we can get away with this.
CFrameWnd* pFrameWnd = (CFrameWnd *) ((CWnd *)this);
CScrollReport *pView = (CScrollReport *)pFrameWnd->CreateView(&pContext);
ASSERT(pView);
// Set an initial scroll size for the CScrollView which will be
// modified in the OnDraw () later when presenting the actual view
// and we have the complete document and can calculate the document's
// scrollable size properly.
CSize sizeTotal;
sizeTotal.cx = rectSize.right;
sizeTotal.cy = 1 * rectSize.bottom;
pView->SetScrollSizes(MM_TEXT, sizeTotal);
pView->ShowWindow(SW_NORMAL);
/**
* After a view is created, resize window area of the view to fit into the
* dialog. Since this is a CScrollView, set an initial size for the
* size of the object being scrolled.
*/
pView->MoveWindow(&rectSize);
return TRUE;
}
【问题讨论】:
-
“CDialog 和 CFrameWnd 都是从 CWnd 派生的,所以我们可以摆脱这个” - 哦,不,你不能。您只能将层次结构转换为基类,而
CFrameWnd不是CDialog的基类。 -
@RogerRowland,因为它是一种 C 风格的转换,可用于将内存区域或对象从任何东西解释为其他任何东西,转换工作但问题是它是否相当安全。我找到了
CFrameWnd::CreateView()at this web location 的 MFC 源代码的副本,看起来该方法实际上只是创建了视图窗口,并且没有使用任何 CFrameWnd 数据。它使用this指针作为父级指向创建的窗口。 -
我相信,在查看了
CFrameWnd::CreateView()的 MFC 源代码后,虽然这可行,但它并不安全,因为它依赖于不修改方法的人。如果有人要修改方法以使用 CFrameWnd 中的某些数据,而这些数据不在 CWnd 的基类中,那么这个构造就会中断。 -
是否有另一种方法可以使用 CScrollView 和关联的 CDocument 通过使用 CDialog 以外的其他方式来提供对话框类型的界面?在网上看这个使用对话框呈现 CScrollView 的问题已经被问过很多次了。似乎需要在由许多人表达的模态对话框内的可滚动视图中呈现文档、文本或图形。这就是我面临的问题,需要在可滚动区域中以模式对话框的形式呈现简短报告。
标签: c++ user-interface dll mfc