10: Finishing Touches

Win32++

Tutorials

Menu of tutorials

Tutorial 1:   The Simplest WindowTutorial 2:   Using Classes and Inheritance
Tutorial 3:   Using Messages to Create a Scribble Window
Tutorial 4:   Repainting the Window
Tutorial 5:   Wrapping a Frame around our Scribble Window
Tutorial 6:   Customising Window Creation
Tutorial 7:   Customising the ToolBar
Tutorial 8:   Loading and Saving Files
Tutorial 9:   Printing
Tutorial 10: Finishing Touches

Tutorial 10:  Finishing Touches

In this final tutorial we will show how to store the application's settings in the registry, and implement tracing.  The code for this final tutorial is the scribble demo application.

Saving the Window Position

Users will expect modern applications to save their settings, such as the position and size of the frame window.  These settings are stored in the registry. LoadRegistrySettings is used to set the name of the registry key.  Typically the name takes the form of  "CompanyName\\ApplicationName" as demonstrated below.

CMainFrame::CMainFrame()
{
  .
  .
  .

  // Set the registry key name, and load the initial window position
  // Use a registry key name like "CompanyName\\Application"
  LoadRegistrySettings(_T("Win32++\\Scribble Sample"));
}

If a registry key name has been set using the LoadRegistrySetting function, the registry settings will be loaded from the registry when the application starts, and stored in the registry when the application ends. Override the LoadRegistrySettings and SaveRegistrySettings functions in CMainFrame if you wish to store other settings in the registry.

Saving the Most Recently Used (MRU) List

Applications which load and store files typically allow the user to choose from a list of recently used file names to load from. This MRU list is also stored in the registry. To add this capability to your Win32++ application, use the LoadRegistryMRUSettings function to specify the number of entries in the MRU list (up to a maximum of 16) as shown below.

CMainFrame::CMainFrame()
{
  .
  .
  .

  // Set the registry key name, and load the initial window position
  // Use a registry key name like "CompanyName\\Application"
  LoadRegistrySettings(_T("Win32++\\Scribble Sample"));

  // Load the settings from the registry with 4 MRU entries
  LoadRegistryMRUSettings(4);
}

To see the MRU entries listed in the menu, add "Recent Files" menu item to the menu definitions in Resource.rc. as follows:

MENUITEM "Recent Files",                IDW_FILE_MRU_FILE1, GRAYED

MRU entries are added with AddMRUEntry, and removed with RemoveMRUEntry.

Command Line Arguments

Command line arguments are passed to the program when it is started. The GetCommandLine function is used to retrieve the the command line string. The CommandLineToArgvW can parse this string and convert it to an array of LPWSTR pointers.

The following code demonstrates how to load a file when the filename is supplied as a command line argument.

void CMainFrame::OnInitialUpdate()
{
  int argCount = 0;
  LPWSTR* lpArgv = ::CommandLineToArgvW(::GetCommandLineW(), &argCount;);

  // The second argument (if any) contains our file name.
  if (argCount >= 2)
  {
    m_View.FileOpen((W2T(lpArgv[1])));
  }
}

This allows the application to start and load the file when it is selected and opened from within Windows Explorer.

Debugging your Application with Tracing

One important debugging technique is to trace what is happening with the application while it is running.  The tracing allows you to display messages or the contents of variables in the output pane of Visual Studio (or whichever Integrated Development Environment you use).

In order to take advantage of this, you will need do the following

  • Use Version 6.0 (or later) of Win32++
  • Run your application in debug mode (see below)
  • Use the TRACE function to display the messages.

To run the application in debug mode, you need to have the _DEBUG variable defined.  Microsoft's Visual Studio products define this variable for you when you compile in debug mode.  For other compilers you should use the appropriate compiler option to set this variable.  The source code for this example, as well as the samples in the download section, include project files for debug modes for both DevC++ and CodeBlocks  to make this task easier.

If you are not running in debug mode, TRACE statements have no effect.  You can leave them in place for Release mode if you wish.

In the sample code for this section we add tracing to our scribble application to display the position of the mouse for the lines we draw.

This is the code added to CView::WndProc for tracing.

char str[80];
::wsprintf(str, "Draw Point: %hd, %hd\n", GET_X_LPARAM(lParam),
             GET_Y_LPARAM(lParam));
TRACE(str);

These are the lines of code added to CMainFrame::WndProc

TRACE("Red pen selected\n");
TRACE("Blue pen selected\n");
TRACE("Green pen selected\n");
TRACE("Black pen selected\n");

The source code for this tutorial is located within the Tutorial folder of the software available from SourceForge at http://sourceforge.net/projects/win32-framework.