Book Image

Microsoft Visual C++ Windows Applications by Example

By : Stefan Bjornander, Stefan Björnander
Book Image

Microsoft Visual C++ Windows Applications by Example

By: Stefan Bjornander, Stefan Björnander

Overview of this book

Table of Contents (15 chapters)
Microsoft Visual C++ Windows Applications by Example
Credits
About the Author
About the Reviewer
Preface
Index

Preface

This is a book about Windows application development in C++. It addresses some rather difficult problems that occur during the development of advanced applications. Most books in this genre have many short code examples. This one has only four main code examples, but rather extensive ones. They are presented in increasing complexity order. The simplest one is the Tetris application, which deals with graphics, timing, and message handling. The Draw application adds a generic coordinate system and introduces more complex applications states. The Calc application deals with formula interpretation and graph searching. Finally, in the Word application every character is allowed to hold its own font and size, resulting in a rather complex size and position calculation.

The book starts with an introduction to object-oriented programming in C++, followed by an overview of the Visual Studio environment with the Ring demonstration application as well as a presentation of some basic generic classes. Then the main applications are presented in one chapter each.

Chapter1. Introduction to C++—C++ is a language built on C. It is strongly typed; it has types for storing single as well as compound values. It supports dynamic memory management with pointers. It has a large set of operators to perform arithmetic, logical, and bitwise operations. The code can be organized into functions, and there is a pre-processor available, which can be used to define macros.

Chapter 2. Object-oriented Programming in C++—C++ is an object-oriented language that fully supports the object-oriented model. The main feature of the language is the class, which can be instantiated into objects. A class can inherit another class. The inheritance can be virtual, which provides dynamic binding. A class can contain an object or have a pointer to another object. We can overload operators and we can throw exceptions. We can create generic classes by using templates and we can organize our classes into namespaces.

Chapter 3. Windows Development—The development environment of this book is Microsoft Visual Studio, which holds several Wizards that generate skeleton code. With their help, we create a framework which we can add our own application specific code to. Microsoft Foundation Classes (MFC) is a powerful C++ class library built upon the Windows 32 bits Application Interface (Win32 API). It holds many classes to build and modify graphical Windows applications.

When an event occurs in Windows, a message is sent to the application in focus. When we want to paint or write in a window, we need a device context, which can be thought of both as painting toolbox and a connection to the painting canvas. When we develop an application such as a spreadsheet program, we want the users to be able to save their work. It can easily be obtained by serialization.

Chapter 4. Ring: A Demonstration Example—As an introduction to the main applications of this book, we go through the step-by-step development process of a simple application that draws rings on the painting area of a window. The rings can be painted in different colors. We increase the painting area by using scroll bars. We increase the user-friendliness by introducing menus, toolbars, and accelerators. The RGB (Red, Green, Blue) standard can theoretically handle more than sixteen million colors. We use the Color Dialog to allow the user to handle them. Finally, we add serialization to our application.

Chapter 5. Utility Classes—There are several generic classes available in MFC, we look into classes for handling points, sizes, and rectangles. However, some generic classes we have to write ourselves. We create classes to handle fonts, colors, and the caret. We also inherit MFC classes to handle lists and sets. Finally, we look into some appropriate error handling.

Chapter 6. The Tetris Application—Tetris is a classic game. We have seven figures of different shapes and colors falling down. The player's task is to move and rotate them into appropriate positions in order to fill as many rows as possible. When a row is filled it disappears and the player gets credit. The game is over when it is not possible to add any more figures.

Chapter 7. The Draw Application—In the Draw application, the users can draw lines, arrows, rectangles, and ellipses. They can move, resize, and change the color of the figures. They can cut and paste one or more figures, can fill the rectangles and ellipses, and can load and save a drawing. They can also write and modify text in different fonts.

Chapter 8. The Calc Application—The Calc application is a spreadsheet program. The users can input text to the cells and they can change the text's font as well as its horizontal and vertical alignment. They can also load and save a spreadsheet and can cut and paste a block of cells. Furthermore, the user can input a formula into a cell. They can build expressions with the four arithmetic operators as well as parentheses.

Chapter 9. The Word Application—The Word application is a word processor program. The users can write and modify text in different fonts and with different horizontal alignment. The program has paragraph handling and a print preview function. The users can cut and paste blocks of text, they can also load and save a document.

What you need for this book

In order to execute the code you need Visual C++ 2008, which is included in Visual Studio 2008.

Who this book is for

The book is ideal for programmers who have worked with C++ or other Windows-based programming languages. It provides developers with everything they need to build complex desktop applications using C++.

If you have already learned the C++ language, and want to take your programming to the next level, then this book is ideal for you.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

There are three styles for code. Code words in text are shown as follows: "The predefined constant NULL (defined in the header file cstdlib) holds the pointer equivalence of the zero value"

A block of code will be set as follows:

int i = 123;
double x = 1.23;

int j = (int) x;
double y = (double) i;

When we wish to draw your attention to a particular part of a code block, the relevant lines or items will be made bold:

// Standard print setup command
  ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
  ON_COMMAND(ID_APP_EXIT, OnAppExit)
END_MESSAGE_MAP()

New terms and important words are introduced in a bold-type font. Words that you see on the screen, in menus or dialog boxes for example, appear in our text like this: "Let us start by selecting New Project in the File menu and choosing Visual C++ Projects and MFC Application with the name Ring and a suitable place on the hard drive".

Reader Feedback

Feedback from our readers is always welcome. Let us know what you think about this book, what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply drop an email to , making sure to mention the book title in the subject of your message.

If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on www.packtpub.com or email .

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer Support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the Example Code for the Book

Visit http://www.packtpub.com/files/code/5562_Code.zip to directly download the example code.

The downloadable files contain instructions on how to use them.

Errata

Although we have taken every care to ensure the accuracy of our contents, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in text or code—we would be grateful if you would report this to us. By doing this you can save other readers from frustration, and help to improve subsequent versions of this book. If you find any errata, report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the Submit Errata link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata are added to the list of existing errata. The existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Questions

You can contact us at if you are having a problem with some aspect of the book, and we will do our best to address it.