问题描述
我有一个 SDI MFC 应用程序,它的 CMainFrame 类是从 CFrameWndEx 派生的.该应用程序使用 CSingleDocument 模板来连接文档/视图/框架.
I have a SDI MFC application which CMainFrame class is derived from CFrameWndEx.The app uses the CSingleDocument template for connecting Document/View/Frame.
我需要另一个窗口,其中包含与主窗口视图的同一文档相关的视图的内容.将 CMainFrame 的另一个实例作为辅助窗口是否正确?
I need to have another window with the contents of a View related to the same document of the view of the Main Window.Is it correct to have another instance of the CMainFrame as a secondary window?
推荐答案
经过一些方法我找到了一个解决方案:
After some approaches I came to a solution:
第一个:
在应用程序上有两个 CMainFrame 实例,其中第二个有一个布尔字段来指示它是一个还是另一个.这个布尔值允许我只在第二个跳过创建所有内容,因为我只希望它是一个带有视图的矩形.我在 CMainFrame 中添加了一个特殊的公共构造函数,它总是将引用的布尔值设置为 true.
Have two CMainFrame instances on the App, where the second has a boolean field to indicate it is one or other. This boolean allowed me to skip the creation of everthing creation only on the second, because I only want it to be a rectangle with the view. I've added a special public constructor that to CMainFrame that always puts the referred boolean to true.
CMainFrame *pSecondFrame = new CMainFrame(TRUE /*dumb*/);
它有效,但速度很慢.由于速度不足,第一个 MainFrame 的视图完全无法使用.
It worked, but it was very slow. The view of the first MainFrame was completely unusable due to its lack of speed.
第二个:
有一个 CSecondFrame,它也是从 CFrameWndEx 派生的,它的主体上几乎没有任何东西.由于 IDR_MAINFRAME 什么都没有,我可以期望执行 LoadFrame(IDR_MAINFRAME) 并且在应用程序上有一个几乎空的框架.
Have a CSecondFrame that is also derived from CFrameWndEx and has almost nothing on its body. As IDR_MAINFRAME has nothing, I can expect to do LoadFrame(IDR_MAINFRAME) and have an almost empty frame on the App.
CSecondFrame *pSecondFrame = new CSecondFrame();
它有效,但速度很慢.由于速度不足,第一个 MainFrame 的视图完全无法使用.
It worked, but it was very slow. The view of the first MainFrame was completely unusable due to its lack of speed.
对于第一种和第二种方法,我不得不添加一个新的 CreateContext,因为我不能重用大型机视图:
For both 1st and 2nd approaches, I had to add a new CreateContext, as I can not reuse the mainframe View:
if (!pSecondFrame->LoadFrame(IDR_MAINFRAME))
return FALSE;
CMainFrame* pMainFrame=(CMainFrame*)::AfxGetMainWnd();
if (!pMainFrame)
return FALSE;
CMyView* pView=(CMyView*)(pMainFrame->GetActiveView());
if (!pView)
return FALSE;
CCreateContext context;
context.m_pCurrentDoc=pMainFrame->GetActiveDocument();
context.m_pNewViewClass= RUNTIME_CLASS(CMyView);
context.m_pLastView= pView;
context.m_pCurrentFrame = pSecondFrame;
pSecondFrame->CreateView(&context);
第三:
在 InitInstance 函数的开头我有文档模板:
At the beginning of the InitInstance function I have the Document Template:
所以让我们玩一下它的 CreateNewFrame 方法:位置 pos= pDocTemplate->GetFirstDocPosition();
So lets play with its CreateNewFrame method: POSITION pos= pDocTemplate->GetFirstDocPosition();
//pFrameTemp attribution
CFrameWnd* pSecondFrame= pDocTemplate->CreateNewFrame(pDocTemplate->GetNextDoc(pos),pFrameTemp);
我为 pFrameTemp 尝试了不同的方法:NULL、CMainFrame、CSecondFrame.每次我得到一个主要副本的框架,我不想要这个.
I tried different things for the pFrameTemp: NULL, a CMainFrame, CSecondFrame. Everytime I got a frame that is carbon-copy of the main and I dont't want this.
对于 CMainFrame 和 CSecondFrame 案例,我试图明确调用它们的 Create(...) 函数,但失败了,所以它们甚至没有传递它们的 OnCreate(...) 方法.取而代之的是,我还尝试为这两种情况执行 LoadFrame(IDR_MAINFRAME).现在我将拥有三个框架:主框架、第二框架,它是主框架的副本,以及一个用 LoadFrame 创建的框架,它完全是空白的.
For the CMainFrame and CSecondFrame cases I tried to explicity call their Create( ...) functions, which fail,so they don't even pass on their OnCreate(...) method.Instead of this I also tried to do a LoadFrame(IDR_MAINFRAME) for both cases. Now I will have three frames: The Main, the Second, which is carbon-copy of the main, and the one created with LoadFrame, which is totally blank.
第四:在看到他的 CreateNewFrame 方法基于模板上的框架而不是在其参数中传递的框架创建一个新框架后,我决定尝试另外创建另一个模板:
4th:After seeing that he CreateNewFrame method creates a new Frame based on the frame it has on the template and not the one that is passed in its parameter, I decided to try create addicionally another template:
CSingleDocTemplate* pSecondDocTemplate;
pSecondDocTemplate2 = new CSingleDocTemplate(
MenuID,
RUNTIME_CLASS(CMyDoc),
RUNTIME_CLASS(CSecondFrame),//only the frame differs
RUNTIME_CLASS(CMyView));
AddDocTemplate(pSecondDocTemplate);
POSITION pos= pDocTemplate->GetFirstDocPosition();
CFrameWnd* pSecondFrame=pSecondDocTemplate->CreateNewFrame(pDocTemplate->GetNextDoc(pos), NULL);
请注意,我将第一个模板的文档作为参数提供给 CreateNewFrame,而不是第二个.这是我测试过的唯一有效且不会减慢视图操作速度的解决方案.
Notice that I am giving the doc of the first template as parameter, not the second, to CreateNewFrame.This is the only solution I tested that works and doesn't slow down operations in the view.
似乎我需要,对于我提出的每一个解决方案
It seems I need, for every solution I presented to do
pSecondFrame->ShowWindow(SW_SHOW);
用于使窗口可见.
这篇关于Mfc 多窗口同一个文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!