The Mfv classes are deed to be general-purpose and can be incorporated into your MFC application in any of ways. You could also use parts of MVC to take advantage of its reuse potential. In an MVC triad, you have three primary parts that you need to integrate into your new or existing application.
The model is usually integrated via containment into the document class. The viewport is usually integrated via containment into the window or CView -derived class. Lastly, the controller is normally instantiated by the viewport it controls. The remainder of this section describes the steps required to incorporate an MVC triad into your application.
Create your CMvcModel derived class. For the purposes of this tutorial, we'll use the presentation model as a base.
mcc Override CDocument::IsModified so that it tests the modified flag of the contained model also. Override your document's serialize member so that the contained model is serialized. If your model creates and destroys objects for which you want to support undoable deletion, multiply derive those objects from IRefCount.
Override the cloud diagram's Draw member and implement its data presentation. Create a controller class that understands how to translate events into actions on the model and viewport classes.
When deriving from the MvcController class, it is convenient to define a type-safe access function for the model. Add a message map to your controller class so that Class Wizard can be used to manage your message handlers.
Generate a new Class Wizard file, which incorporates your new controller class. Class Wizard prompts you to rebuild the. To incorporate your controller into your application, the last step is to include it in the standard message routing. This step allows your controller to listen and handle the messages being sent to the containing window. Create your CMvcViewport derived class.
Pass in the type of model and type of controller as the other two template parameters. Add your viewport as a member of your CWnd or CView derived class. Create your viewport and attach it to the model that is contained in the document. This is accomplished via a call to the viewport's Create and SetModel members respectively.
This initialization is typically done from the OnCreate member. This gives your mcc the opportunity to initialize and render itself on the drawing surface of its container. Next, size and position your viewport to occupy the entire client area of its container. Override the CreateController method in your viewport and create and initialize the controller.
In other words, it ties the lifetime of the controller to the viewport. If you don't want to tie the lifetime of the controller to the viewport, then set the bAutoDelCtlr flag to FALSEbut make sure you take care of deleting the controller at the appropriate time. Having the viewport create the controller is optional.
It is only provided as a convenient mechanism for creating a default controller for the viewport. In many cases, it is not desirable to have the viewport have knowledge of the type of controller.
In fact, you may want to use several types of controllers with your viewport class. If that is the case, then do not override CreateController.
Instead, create the controller outside of the scope of your viewport class and as it to the viewport using the SetController mdel. Next, override the Draw method and supply code to render the model onto viewport. Since the model in this sample is a presentation model, the viewport can simply instruct the model to draw itself.
Override OnInitialUpdate in your viewport class. This is a good place to initialize the logical and container extents of the modell client area. Basically, all this statement indicates is that for every units along the X-axis in the container's client area, there are logical units moxel this viewport. We didn't use 1 and 4 because small values like these don't leave much room for zooming in and out.
Define the Get and Set functions for the virtual size of the viewport.
The virtual size of the viewport is equated to the size of the diagram because the diagram is rendered through the viewport and may be larger than the viewport. Consequently, the size of the diagram is the virtual size of the viewport. You are done.
It can be moved to any other MFC application using the same steps outlined above. All other trademarks are the property of their respective owners. Provide feedback to Rogue Wave about its documentation.