48162

Configuring new document in MFC

Question:

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).<br /> Is there a better place to do this in MFC?

Answer1:

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

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

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

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

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,...,...); } }

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.

This function will then be called a first time <em>before</em> the initial mainframe is created. You may not want to have a dialog presented to user <em>before</em> the frame is created. Ouch! It's a little more complicated: Instead of popping up a GUI in in OpenDocumentFile(NULL) as above, just <strong>post</strong> 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.

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.

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 :-(

Recommend

  • MS MVC3 Model Binding an Object
  • JavaFX scrollbar customize. Eliminate extra square, when both scrollbars are activated
  • How do can I get XmlRoot to have a collection of objects?
  • SQL Server Integrated Security from an Azure Web Site
  • Is it one-to-one or Component ? Nhibernate Mapping
  • How to only store 3 values for a key in a dictionary? Python
  • Installing PAR::Packer on Windows, dmake error 255
  • Delete std::shared_ptr without destroying the managed object?
  • JsonConverter: Get parent object in ReadJson, without $refs
  • How to set an entity field that does not exist on the table but does exists in the raw SQL as an ali
  • Specifying virtual keyboard type for EditText in XML
  • MRO with multiple inheritance in python
  • how to ignore xml namespaces?
  • Is there any way to call saveCurrentTurnWithMatchData without sending a push notification?
  • Outlook to Excel hyperlink issue
  • Efficient User-Agent Regex to find Safari in Python
  • Get Currently Active User in Android
  • Mixing WebForms and MVC: What should I do with the MasterPage?
  • What is the correct way to synchronize a shared, static object in Java?
  • Two Tables Serving as one Model in Rails
  • as3-flash: any way to access all the instances placed in different frames from document class?
  • how does System.Web.HttpRequest::PathInfo work?
  • Element.tagName for python not working
  • Spring Cloud Microservice Architecture Confusion
  • gspread or such: help me get cell coordinates (not value)
  • How can I set a binding to a Combox in a UserControl?
  • SplFileObject error failed to open stream: No such file or directory
  • print() is showing quotation marks in results
  • Play WS (2.2.1): post/put large request
  • Refering to the class itself from within a class mehod in Objective C
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • Highlight one bar in a series in highcharts?
  • javaw.exe and eclipse startup problems
  • Calling of Constructors in a Java
  • PHP: When would you need the self:: keyword?
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?