Microsoft Visual C++ Windows Applications by Example Code and Explanation for Real-World MFC C++ Applications Stefan Björnander BIRMINGHAM - MUMBAI Microsoft Visual C++ Windows Applications by Example Copyright © 2008 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, Packt Publishing, nor its dealers or distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: June 2008 Production Reference: ������00��0088 Published by Packt Publishing Ltd. 32 Lincoln Road Olton Birmingham, B2� �PA, UK. ISBN 9�8-�-84��95-5�-2 www.packtpub.com Cover Image by karl.moore ([email protected]) Credits Author Project Manager Stefan Björnander Abhijeet Deobhakta Reviewer Project Coordinator S. G. Ganesh Brinell Catherine Lewis Senior Acquisition Editor Indexer David Barnes Monica Ajmera Development Editor Proofreader Swapna V. Verlekar Angie Butcher Technical Editor Production Coordinator Bhupali Khule Shantanu Zagade Editorial Team Leader Cover Work Akshara Aware Shantanu Zagade About the Author Stefan Björnander is a Ph.D. candidate at Mälardalen University, Sweden. He has worked as a software developer and has taught as a senior lecturer at Umeå University, Sweden. He holds a master's degree in computer science and his research interests include compiler construction, mission-critical systems, and model-driven engineering. You can reach him at [email protected]. I dedicate this book to my parents Ralf and Gunilla, my sister Catharina, her husband Magnus, and their son Emil About the Reviewer S. G. Ganesh is currently working as a research engineer in Siemens Corporate Technology, Bangalore. He works in the area of Code Quality Management (CQM). He has good experience in system software development having worked for around five years in Hewlett-Packard's C++ compiler team in Bangalore. He also represented the ANSI/ISO C++ standardization committee (JTC1/SC22/WG21) from 2005 to 200�. He has authored several books. The latest one is 60 Tips for Object Oriented Programming (Tata-McGraw Hill/ISBN-13 978-0-07-065670-3). He has a master's degree in computer science. His research interests include programming languages, compiler design and design patterns. If you're a student or a novice developer, you might find his website www.joyofprogramming.com to be interesting. You can reach him at [email protected]. Table of Contents Preface 1 Chapter 1: Introduction to C++ 7 The Compiler and the Linker 8 The First Program 8 Comments 9 Types and Variables 9 Simple Types 10 Variables 10 Constants 11 Input and Output 12 Enumerations 12 Arrays 13 Pointers and References 13 Pointers and Dynamic Memory 15 Defining Our Own Types 18 The Size and Limits of Types 18 Hungarian Notation 20 Expressions and Operators 21 Arithmetic Operators 21 Pointer Arithmetic 21 Increment and Decrement 23 Relational Operators 23 Logical Operators 23 Bitwise Operators 24 Assignment 25 The Condition Operator 25 Precedence and Associativity 26 Statements 27 Table of Contents Selection Statements 27 Iteration Statements 30 Jump Statements 32 Expression Statements 32 Functions 32 Void Functions 34 Local and Global Variables 34 Call-by-Value and Call-by-Reference 36 Default Parameters 39 Overloading 40 Static Variables 40 Recursion 41 Definition and Declaration 42 Higher Order Functions 43 The main() Function 44 The Preprocessor 45 The ASCII Table 47 Summary 48 Chapter 2: Object-Oriented Programming in C++ 49 The Object-Oriented Model 50 Classes 51 The First Example 52 The Second Example 55 Inheritance 58 Dynamic Binding 60 Arrays of Objects 65 Pointers and Linked Lists 65 Stacks and Linked Lists 66 Operator Overloading 70 Exceptions 76 Templates 77 Namespaces 80 Streams and File Processing 82 Summary 84 Chapter 3: Windows Development 87 Visual Studio 88 The Document/View Model 89 The Message System 90 The Coordinate System 93 The Device Context 94 [ ii ] Table of Contents The Registry 98 The Cursor 98 Serialization 99 Summary 101 Chapter 4: Ring: A Demonstration Example 103 The Application Wizard 104 Colors and Arrays 109 Catching the Mouse 110 Drawing the Rings 112 Setting the Coordinate System and the Scroll Bars 113 Catching the Keyboard Input 116 Menus, Accelerators, and Toolbars 117 The Color Dialog 123 The Registry 123 Serialization 124 Summary 125 Chapter 5: Utility Classes 127 The Point, Size, and Rectangle Classes 128 The Color Class 129 The Font Class 130 The Caret Class 133 The List Class 136 The Set Class 137 The Array Class 140 Error Handling 140 Summary 142 Chapter 6: The Tetris Application 143 The Tetris Files 144 The Square Class 146 The Color Grid Class 146 The Document Class 147 The View Class 155 The Figure Class 160 The Figure Information 167 The Red Figure 168 The Brown Figure 168 The Turquoise Figure 169 The Green Figure 169 The Yellow Figure 170 The Blue Figure 171 [ iii ] Table of Contents The Purple Figure 171 Summary 172 Chapter 7: The Draw Application 173 The Resource 177 The Class Hierarchy 179 The Figure Class 180 The TwoDimensionalFigure Class 183 The LineFigure Class 185 The ArrowFigure Class 192 The RectangleFigure Class 197 The EllipseFigure Class 200 The TextFigure Class 204 The FigureFileManager Class 213 The Document Class 215 The View Class 233 Summary 237 Chapter 8: The Calc Application 239 The Resource 242 Formula Interpretation 243 The Tokens 244 The Reference Class 246 The Scanner—Generating the List of Tokens 248 The Parser—Generating the Syntax Tree 251 The Syntax Tree—Representing the Formula 262 The Spreadsheet 268 The Cell—Holding Text, Value, or Formula 268 The Cell Matrix—Managing Rows and Columns 286 The Target Set Matrix Class 287 The Document/View Model 291 The Document Class 291 The View Class 311 Summary 328 [ iv ]
Description: