What Is A Carchive Object: Visual C++ Material
What Is A Carchive Object: Visual C++ Material
Like any program for Windows, your framework application has a WinMain function. In a
framework application, however, you don't write WinMain. It is supplied by the class library and is
called when the application starts up. WinMain performs standard services such as registering
window classes. Then it calls member functions of the application object to initialize and run the
application. (You can customize WinMain by overriding the CWinApp member functions that
WinMain calls.)
Teams Global Village Academy
Page 1
A variable that declares the one and only object of the class
The application class is placed in the project header and main source files. The names of the class and
files created are based on the project name you supply in AppWizard. The easiest way to view the
code for these classes is through the Class View in the Project Workspace window.
The standard implementations and message map supplied are adequate for many purposes, but you
can modify them as needed. The most interesting of these implementations is the InitInstance member
function. Typically you will add code to the skeletal implementation of InitInstance.
CFile
CFile is the base class for Microsoft Foundation file classes. It directly provides unbuffered, binary
disk input/output services, and it indirectly supports text files and memory files through its derived
classes. CFile works in conjunction with the CArchive class to support serialization of Microsoft
Foundation Class objects.
The hierarchical relationship between this class and its derived classes allows your program to operate
on all file objects through the polymorphic CFile interface. A memory file, for example, behaves like
a disk file.
Use CFile and its derived classes for general-purpose disk I/O. Use ofstream or other Microsoft
iostream classes for formatted text sent to a disk file.
Normally, a disk file is opened automatically on CFile construction and closed on destruction. Static
member functions permit you to interrogate a files status without opening the file.
Page 2
CException is the base class for all exceptions in the Microsoft Foundation Class Library. The
derived classes and their descriptions are listed below:
CMemoryException
Out-of-memory exception
CNotSupportedException
CArchiveException
Archive-specific exception
CFileException
File-specific exception
CResourceException
COleException
OLE exception
CDBException
COleDispatchException
CUserException
CDaoException
CInternetException
CDialog
The CDialog class is the base class used for displaying dialog boxes on the screen. Dialog boxes are
of two types: modal and modeless. A modal dialog box must be closed by the user before the
application continues. A modeless dialog box allows the user to display the dialog box and return to
another task without canceling or removing the dialog box.
Teams Global Village Academy
Page 3
CEdit
The CEdit class provides the functionality of a Windows edit control. An edit control is a rectangular
child window in which the user can enter text.
You can create an edit control either from a dialog template or directly in your code. In both cases,
first call the constructor CEdit to construct the CEdit object, then call the Create member function to
create the Windows edit control and attach it to the CEdit object.
Construction can be a one-step process in a class derived from CEdit. Write a constructor for the
derived class and call Create from within the constructor.
CEdit inherits significant functionality from CWnd. To set and retrieve text from a CEdit object, use
the CWnd member functions SetWindowText and GetWindowText, which set or get the entire
contents of an edit control, even if it is a multiline control. Also, if an edit control is multiline, get
and set part of the controls text by calling the CEdit member functions GetLine, SetSel, GetSel, and
ReplaceSel.
If you want to handle Windows notification messages sent by an edit control to its parent (usually a
class derived from CDialog), add a message-map entry and message-handler member function to the
parent class for each message.
Following is a list of potential message-map entries and a description of the cases in which they
would be sent to the parent:
ON_EN_CHANGE The user has taken an action that may have altered text in an edit
control. Unlike the EN_UPDATE notification message, this notification message is sent after
Windows updates the display.
ON_EN_ERRSPACE The edit control cannot allocate enough memory to meet a specific
request.
ON_EN_HSCROLL The user clicks an edit controls horizontal scroll bar. The parent
window is notified before the screen is updated.
Page 4
ON_EN_MAXTEXT The current insertion has exceeded the specified number of characters
for the edit control and has been truncated. Also sent when an edit control does not have the
ES_AUTOHSCROLL style and the number of characters to be inserted would exceed the
width of the edit control. Also sent when an edit control does not have the
ES_AUTOVSCROLL style and the total number of lines resulting from a text insertion
would exceed the height of the edit control.
ON_EN_UPDATE The edit control is about to display altered text. Sent after the control
has formatted the text but before it screens the text so that the window size can be altered, if
necessary.
ON_EN_VSCROLL The user clicks an edit controls vertical scroll bar. The parent
window is notified before the screen is updated.
If you create a CEdit object within a dialog box, the CEdit object is automatically destroyed when
the user closes the dialog box.
If you create a CEdit object from a dialog resource using the dialog editor, the CEdit object is
automatically destroyed when the user closes the dialog box.
CListBox
The CListBox class provides the functionality of a Windows list box. A list box displays a list of
items, such as filenames, that the user can view and select.
In a single-selection list box, the user can select only one item. In a multiple-selection list box, a range
of items can be selected. When the user selects an item, it is highlighted and the list box sends a
notification message to the parent window.
You can create a list box either from a dialog template or directly in your code. To create it directly,
construct the CListBox object, then call the Create member function to create the Windows list-box
control and attach it to the CListBox object. To use a list box in a dialog template, declare a list-box
variable in your dialog box class, then use DDX_Control in your dialog box class's
DoDataExchange function to connect the member variable to the control. (ClassWizard does this for
you automatically when you add a control variable to your dialog box class.)
Following is a list of potential message-map entries and a description of the cases in which they
would be sent to the parent:
ON_LBN_DBLCLK The user double-clicks a string in a list box. Only a list box that has
the LBS_NOTIFY style will send this notification message.
Page 5
ON_LBN_ERRSPACE The list box cannot allocate enough memory to meet the request.
ON_LBN_SELCHANGE The selection in the list box is about to change. This notification
is not sent if the selection is changed by the CListBox::SetCurSel member function. This
notification applies only to a list box that has the LBS_NOTIFY style. The
LBN_SELCHANGE notification message is sent for a multiple-selection list box whenever
the user presses an arrow key, even if the selection does not change.
If you create a CListBox object within a dialog box (through a dialog resource), the CListBox object
is automatically destroyed when the user closes the dialog box.
Page 6
Accelerator tables
Cursors
Dialog boxes
HTML pages
Menus
String tables
Toolbar resources
Version information
Menus: Overview
MFC supplies two elements to help you work with menus:
Class CMenu for manipulating your programs menus at run time. Use the documentation for
CMenu and the sample to learn how to use CMenu effectively.
A mechanism for updating menus and toolbar buttons: enabling or disabling them on the fly
to suit current program conditions.
Menus allow you to arrange commands in a logical, easy-to-find fashion. With the Menu editor, you
can create and edit menus by working directly with a menu bar that closely resembles the one in your
finished application.
With the Menu editor, you can:
Page 7
Click the menu caption on the menu bar or the parent item of the cascading menu. Then click
the menu item you want.
Accelerator Editor
An accelerator table is a Windows resource that contains a list of accelerator keys (also known as
shortcut keys) and the command identifiers that are associated with them. A program can have more
than one accelerator table.
Normally, accelerators are used as keyboard shortcuts for program commands that are also available
on a menu or toolbar. However, you can use the accelerator table to define key combinations for
commands that dont have a user-interface object associated with them.
With the Accelerator editor, you can:
Add, delete, change, and browse the accelerator key assignments in your project.
View and change the resource identifier associated with each entry in the accelerator table.
The identifier is used to reference each accelerator table entry in program code.
Find one or more accelerator keys in the accelerator table, and use regular expressions with
the Find command to locate an accelerator key that matches a pattern.
Select multiple accelerator keys and change their properties at one time.
Page 8
Dialog Editor
Use the Dialog editor to create or edit dialog boxes. You can store a dialog box as a template so you
can reuse it. And you can easily switch between designing the dialog box and editing the code that
implements it.
With the Dialog editor, you can:
Create Windows message handlers for the dialog box or its controls.
Jump to the code that implements the dialog box and its controls.
You add controls to a dialog box by using the Controls toolbar, which enables you to choose the
control you want and to drag it to the dialog box. When displayed, the toolbar stays positioned above
other open windows in your workspace. You can add ActiveX controls to the Controls toolbar. You
can also add ActiveX controls, which may not be on the toolbar, directly to a dialog box by using the
Insert ActiveX Controls command from the Dialog editor shortcut menu.
The fastest way to add controls to a dialog box, reposition existing controls, or move controls from
one dialog box to another is to use the drag-and-drop method. The controls position is outlined in a
dotted line until it is dropped into the dialog box. When you add a control to a dialog box with the
drag-and-drop method, the control is given a standard height appropriate to that type of control.
When you add a control to a dialog box or reposition it, its final placement may be determined by
guides or margins, or whether you have the layout grid turned on.
Page 9
Bitmaps
Without graphics images, Microsoft Windows-based applications would be pretty dull. Some
applications depend on images for their usefulness, but any application can be spruced up with the
addition of decorative clip art from a variety of sources. Windows bitmaps are arrays of bits mapped
to display pixels. That might sound simple, but you have to learn a lot about bitmaps before you can
use them to create professional applications for Windows. This module starts with the "old" way of
programming bitmaps, creating the device-dependent GDI bitmaps that work with a memory device
context. You need to know these techniques because many programmers are still using them and
you'll also need to use them on occasion.
Next you'll graduate to the modern way of programming bitmaps, creating device-independent
bitmaps (DIBs). If you use DIBs, you'll have an easier time with colors and with the printer. In some
cases you'll get better performance. The Win32 function CreateDIBSection() gives you the benefits of
DIBs combined with all the features of GDI bitmaps. Finally, you'll learn how to use the MFC
CBitmapButton class to put bitmaps on pushbuttons. Using CBitmapButton to put bitmaps on
pushbuttons has nothing to do with DIBs, but it's a useful technique that would be difficult to master
without an example.
Loading a GDI Bitmap from a Resource
The easiest way to use a bitmap is to load it from a resource. If you look in ResourceView in the
Workspace window, you'll find a list of the project's bitmap resources. If you select a bitmap and
examine its properties, you'll see a filename as shown below.
Page 10
Sent When
A character is input from the keyboard.
The user selects an item from a menu, or a
WM_COMMAND
control sends a notification to its parent.
WM_CREATE
A window is created.
WM_DESTROY
A window is destroyed.
WM_LBUTTONDOWN The left mouse button is pressed.
WM_LBUTTONUP
The left mouse button is released.
WM_MOUSEMOVE
The mouse pointer is moved.
WM_PAINT
A window needs repainting.
WM_QUIT
The application is about to terminate.
WM_SIZE
A window is resized.
Table 1: Windows messages example.
A message manifests itself in the form of a call to a window's window procedure. Bundled with the
call are four input parameters:
1. The handle of the window to which the message is directed,
2. A message ID, and
3. Two 32-bit parameters known as wParam and lParam.
The window handle is a 32-bit value that uniquely identifies a window. Internally, the value
references a data structure in which Windows stores relevant information about the window such as
its size, style, and location on the screen. The message ID is a numeric value that identifies the
message type: WM_CREATE, WM_PAINT, and so on. wParam and lParam contain information
specific to the message type. When a WM_LBUTTONDOWN message arrives, for example, wParam
holds a series of bit flags identifying the state of the Ctrl and Shift keys and of the mouse buttons.
lParam holds two 16-bit values identifying the location of the mouse pointer when the click occurred.
Together, these parameters provide the window procedure with all the information it needs to process
the WM_LBUTTONDOWN message.
A large portion of programming for the Windows environment involves message handling. Each time
an event such as a keystroke or mouse click occurs, a message is sent to the application, which must
then handle the event. The Microsoft Foundation Class Library offers a programming model
optimized for message-based programming. In this model, "message maps" are used to designate
which functions will handle various messages for a particular class. Message maps contain one or
more macros that specify which messages will be handled by which functions. For example, a
message map containing an ON_COMMAND macro might look something like this:
BEGIN_MESSAGE_MAP(CMyDoc, CDocument)
ON_COMMAND(ID_MYCMD, OnMyCommand)
Teams Global Village Academy
Page 11
Page 12
Next, you add a message map to your class implementation. To see how this is done, let's take a look
at the message map implementation created for the CHiMomApp class in HiMom.cpp:
// CHiMomApp
BEGIN_MESSAGE_MAP(CHiMomApp, CWinApp)
//{{AFX_MSG_MAP(CHiMomApp)
ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
Teams Global Village Academy
Page 13
Each of these classes has a specific role to play in an MFC Document/View application. The
document class is responsible for the program's data. The view class handles interaction between the
document and the user. The frame class contains the view and other user interface elements, such as
the menu and toolbars. The application class is responsible for actually starting the program and
handling some general-purpose interaction with Windows. Figure 9.1 shows the four main parts of a
Document/View program.
Page 14
Although the name "Document/View" might seem to limit you to only word-processing applications,
the architecture can be used in a wide variety of program types. There is no limitation as to the data
managed by CDocument; it can be a word processing file, a spreadsheet, or a server at the other end
of a network connection providing information to your program. Likewise, there are many types of
views. A view can be a simple window, as used in the simple SDI applications presented so far, or it
can be derived from CFormView, with all the capabilities of a dialog box.
SDI and MDI Applications
There are two basic types of Document/View programs:
An SDI program supports a single type of document and almost always supports only a single view.
Only one document can be open at a time. An SDI application focuses on a particular task and usually
is fairly straightforward.
Several different types of documents can be used in an MDI program, with each document having one
or more views. Several documents can be open at a time, and the open document often uses a
customized toolbar and menus that fit the needs of that particular document.
Why Use Document/View?
The first reason to use Document/View is that it provides a large amount of application code for free.
You should always try to write as little new source code as possible, and that means using MFC
classes and letting AppWizard and ClassWizard do a lot of the work for you. A large amount of the
code that is written for you in the form of MFC classes and AppWizard code uses the Document/View
architecture.
The Document/View architecture defines several main categories for classes used in a Windows
program. Document/View provides a flexible framework that you can use to create almost any type of
Windows program. One of the big advantages of the Document/View architecture is that it divides the
Page 15
Work that involves handling the toolbar, status bar, and menus, usually belonging to the
frame class
Interaction between the application and Windows occurring in the class derived from
CWinApp
Dividing work done by your program helps you manage the design of your program more effectively.
Extending programs that use the Document/View architecture is fairly simple because the four main
Document/View classes communicate with each other through well-defined interfaces. For example,
to change an SDI program to an MDI program, you must write little new code. Changing the user
interface for a Document/View program impacts only the view class or classes; no changes are needed
for the document, frame, or application classes.
Using AppWizard
Use AppWizard to create SDI and MDI applications. In earlier chapters, you use AppWizard to create
the SDI programs used as examples. Although doing so is more complicated, you can use AppWizard
to create an MDI application almost as easily as an SDI.
The basic difference between an SDI application and an MDI application is that an MDI application
must manage multiple documents and, usually, multiple views. The SDI application uses only a single
document, and normally only a single view.
Both SDI and MDI applications use an object called a document template to create a relationship
between a view, a document, and a frame class, as well as an identifier used for the program's menu,
icon, and other resources. You use the CSingleDocTemplate class for SDI applications and the
CMultiDocTemplate class for MDI applications. These two classes share a common base class,
CDocTemplate. Listing 9.1 is an example of a document template used for an SDI program.
Program Examples 1
Let go straight to the program example using MFC. This is a Single Document Interface (SDI)
"Hello, world!" classic example. The application has only one window, an object of a class derived
from CFrameWnd. All drawing occurs inside the frame window and all messages are handled there.
This just a warm up session and we are using AppWizard that can automate most of our task in
building Windows application. Follow the steps one-by-one.
Launch your Visual C++, click the File menu and click the New sub menu. Click the Projects tab
for the AppWizard as shown below. Select the MFC AppWizard (exe), type your project name and
set the project location as needed. Leave other setting as default and click the OK button.
Page 16
The wizard has 6 steps and you can exit at any step. Select the Single document radio button and
uncheck the Document/View architecture support then click Next button.
Page 17
Page 18
Page 19
Page 20
Compile and run. You now have a complete SDI application that has no dependencies on the
document-view architecture.
Page 21