Tutorials
|
|
Menu of tutorialsTutorial 1: The Simplest Window
In this final tutorial we will show how to store the application's
settings in the registry, implement tracing and handle exceptions. 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. If a registry key name has been set using the Saving the Most Recently Used (MRU) List Applications that 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 To see the MRU entries listed in the menu, add "Recent Files" menu item
to the menu definitions in Resource.rc as follows: MRU entries are added with Command Line Arguments Command line arguments are passed to the program when it is started. The
The following code demonstrates how to load a file when the filename is
supplied as a command line argument. This allows the application to start and load the file when it is
selected and opened from within Windows Explorer. This also provides drag
and drop support for running the application by dropping a data file on the
program's icon or .exe file. 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 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::OnMouseMove for tracing. Handling Exceptions When an exception is thrown, it should be handled, otherwise our program
will terminate. A program is terminated whenever an exception isn't handled. The Win32++ framework will throw exceptions when it can't perform certain
tasks. Examples of tasks that can raise exceptions include: We can use exception handling to warn of these problems and possibly
allow our program to continue running. To handle all exceptions we should
place a try/catch block in our program's entry point (WinMain or wWinMain).
We also need and a try/catch block in the WndProc function for each CWnd
class and the DialogProc for each CDialog class, even those that don't do
special message handling. The following code show how to display exceptions that might be raised
when our program starts. The following code shows how to handle exceptions that might be raised
when our program handles window messages. Note that this code will also handle exceptions raised in functions
called by WndProcDefault, such as OnCommand, OnDraw, OnNotify etc. Code that is expected to raise exceptions should also have its own
exception handling. Code that uses files, archives and printing can throw
exceptions. The following code shows how we might handle exception when
opening a file. 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. |