• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

在MFC配置新文档

用户头像
it1352
帮助1

问题说明

当用户在我的SDI应用程序中创建一个新文档时,我需要显示一个对话框,该对话框指定要创建的文档的详细信息(例如:分辨率,位深度等).我最初将用于显示此内容的代码OnNewDocument()中的对话框(打开现有文档时不需要),但是将用户界面代码放在文档类中感觉不对(另外,我没有任何CWnd *可以用作对话框的父级.)
在MFC中有更好的方法吗?

When the user creates a new document in my SDI-application, I need to present a dialog specifying details on the document to be created (think: resolution, bit-depth, etc.) I initially put the code for displaying this dialog in OnNewDocument() (I don't need it when opening an existing document), but putting user-interface code in the document-class just doesn't feel right (also, I don't have any CWnd* to use as a parent for the dialog).
Is there a better place to do this in MFC?

正确答案

#1

是的,文档类不是UI的好地方.

You're right, the document class is no good place for UI.

CDocTemplate::[OpenDocumentFile][1](pszPath)看起来是更好的候选者:

CDocTemplate::[OpenDocumentFile][1](pszPath) looks like a better candidate:

pszPath == NULL表示创建新文档".

pszPath==NULL means 'create a new document'.

该方法是虚拟的->只需从CSingleDocTemplate派生CMySingleDocTemplate并在CMyWinApp::InitInstance().

The method is virtual -> Just derive CMySingleDocTemplate from CSingleDocTemplate and use an instance of this class in CMyWinApp::InitInstance().

该类负责创建文档,框架和视图,因此我认为这是放置UI操作的好地方.

This class is responsible for creating docs, frames and views, hence I think it's a good place to put a UI operation.

BOOL CMyWinApp::InitInstance()
{
  ...
  CSingleDocTemplate* pDocTemplate;
  pDocTemplate = new CMySingleDocTemplate( // <--Derives from CSingleDocTemplate
    IDR_MAINFRAME,
    RUNTIME_CLASS(CMyDoc),
    RUNTIME_CLASS(CMainFrame),
    RUNTIME_CLASS(CMyView));
  AddDocTemplate(pDocTemplate);
  ...
}

CDocument* CMySingleDocTemplate::OpenDocumentFile(LPCTSTR lpszPathName,
    BOOL bMakeVisible)
{
  CDocument *pDoc = 
    CSingleDocTemplate::OpenDocumentFile(lpszPathName, bMakeVisible);

  if (lpszPathName==NULL)
  {
    // GUI to get user info

    // update doc
    m_pOnlyDoc->Blah(input);

    // update view
    m_pOnlyDoc->UpdateAllViews(NULL,...,...);
  }
}

但是,这可能并不理想:在SDI中,只有一个doc对象.在文件/加载"和文件/新建"操作中已重复使用.

This might not be ideal though: In SDI, there is one and only doc object. It's re-used accross File/Load and File/New operation.

此函数将在创建初始大型机之前的 中首次调用.您可能不希望在创建框架之前向用户 显示对话框.哎哟!有点复杂: 无需像上面那样在OpenDocumentFile(NULL)中弹出GUI,只需将自定义消息/命令发布到主机即可.然后添加一个处理程序,该处理程序将按顺序弹出GUI/update doc/update视图.这样,将在弹出GUI之前显示主框架,并且您的用户会更快乐.

This function will then be called a first time before the initial mainframe is created. You may not want to have a dialog presented to user before the frame is created. Ouch! It's a little more complicated: Instead of popping up a GUI in in OpenDocumentFile(NULL) as above, just post a custom message/command to the main frame. Then add a handler that will react by the sequence pop up GUI/update doc/update views. That way, the main frame will be displayed before the GUI is popped up and your user will be happier.

这也解决了您没有CWnd父级的问题:主框架已经创建,并且对话框将默认使用它.

This also solves your problem where you don't have a CWnd parent: the main frame is already created and your dialog will use it byt default.

顺便说一句,另一种解决方案是在CMyWinApp的消息映射中添加ID_FILE_NEW的命令处理程序,并添加自己的OnFileNew()覆盖.但是,当您编写OnFileNew()时,我相信您会很快发现这是一个丑陋的解决方案:-(

BTW, another solution consists in adding a command handler for ID_FILE_NEW in your CMyWinApp's message map and add your own override of OnFileNew(). But when you write OnFileNew(), I believe you'll quickly find out that it's an ugly solution :-(

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcgkhej
系列文章
更多 icon
同类精品
更多 icon
继续加载