Puzzle, a Game with the MFC Doc/View Architecture (SDI/MDI)

This article shows a completely operational game and allows you to use all pictures available on the PC (only if they are tall enough).

But, the programmer’s interest is to show what could be drawn out from the Microsoft architecture Doc/View. This part will be treated more in the next article. But, all the needed windows don’t use this architecture; so, this game brings other ways and it proves their advantages.

  • Menu with thumbnails: Doc/View
  • Puzzle: Doc/View
  • Model: Windows type CWnd
  • Comments: Windows type CWnd
  • Options, DirPicker: CDialog

Another aspect is to indicate the usage of linked lists for the management of puzzle pieces. They could be either found cut off, scattered in the game, connected to neighbours, or stuck in the right place. The t_PIECE structure allows you to show this state with the best display speed:

typedef struct _PIECE {
struct _PIECE *pBack, *pFor, *pFirst; // list and sub list ptr
CPoint src, dsp; // position of pieces
// (image source and display)
} t_PIECE;

The two first pointers, pBack and pFor, are the pointers back and forth from the classical double-linked list. The pieces are arranged in this list in inversed display order: The first one on the list is the last one drawn, so it is at the top of the Z list. The third pointer allows you to build a sub-list. The pFirst pointer gives the first piece of a sub-list. All pieces belonging to the same sub-list possess the same pFirst value. They are successive in the main list.

The pieces are stored in a table, ordered in succession of lines in original picture. The first piece is top left and the last one is bottom right. The piece’s position in a table allows you to identify a specific piece and the CPoint src from t_PIECE gives redundant information, but it avoids making the calculation again. The selected piece (with the mouse click) is taken at the head of the list (and displayed in the top position). The CpuzCtrl class manages and displays the pieces of the puzzle.

To allow a display without flicker, two precautions are taken during the movement of pieces:

  • Don’t redraw the complete window; only draw the moved piece and the related background.
  • Execute this operation two times: First, draw in memory with a CMemDC, and then copy this area on the screen without prior clearing.

One other interesting feature of the Doc/View architecture is the saving of the play when we exit the program, while the game is not completed.

A help is implemented but in French, so it is not necessary to use this game. The Resource.rc.fr file included in the source package allows you to create the French release.

Prototype of the Use of an MFC Doc/View Architecture in a Multi-SDI Aspect


To show an application with multiple windows, not bordered on main window IDR_MAINFRAME, with the displayed and specialised documents and with the same usage of DDE, as in MDI architecture. Another demand is to have only one view per document type and not to have multiple application instances at each new DDE calling.


  1. Use the Visual Studio CPP Wizard in MFC AppWizard(exe) and SDI Doc/View options to create a SDI application with a look of the main requested window in this project (update a type of files to use, and if needed, modify the class of the view).
  2. Modify the resources by duplexing the String table entry IDR_MAINFRAME (as in an MDI application). Personify this duplicate with new titles of views, new file names, and extensions for the new document types. Make a duplicate of the IDR_MAINFRAME menu. A menu is needed per document type.
  3. Update the source code of the derived class of CWinApp:
    • Include the modified class files DocManagerEx.h and SingleDocTemplateEx.h.
    • Add a calling of these classes (before the template’s definition).
    • Add a definition of the new templates. The first template ptr must be saved because a empty View must be loaded with this to initialise the m_pMainWnd ptr (the MAINFRAME of this application).
  4. Compile this application; it must work correctly. For a later one, give a better value to the application by specifying the management of the classes inside the different frames; one way is to create new fitted classes.

For more explanation, you can read the supplied sources with this article.

More by Author

Must Read