CONTENTS Summary 577 Chapter 11: winDows programming ConCepts 579... CONTENTS Chapter 12: winDows programming with the miCrosoft founDation Classes mfC 613 Summary 635... CONTENTS Summa
Trang 3Ivor Horton’s
IntroduCtIon xxxiii
CHapter 1 Programming with Visual C++ 1
CHapter 2 Data, Variables, and Calculations 25
CHapter 3 Decisions and Loops 91
CHapter 4 Arrays, Strings, and Pointers 129
CHapter 5 Introducing Structure into Your Programs 179
CHapter 6 More about Program Structure 221
CHapter 7 Defining Your Own Data Types 267
CHapter 8 More on Classes 323
CHapter 9 Class Inheritance and Virtual Functions 431
CHapter 10 The Standard Template Library 479
CHapter 11 Windows Programming Concepts 579
CHapter 12 Windows Programming with the Microsoft Foundation Classes (MFC) 613
CHapter 13 Working with Menus and Toolbars 637
CHapter 14 Drawing in a Window 663
CHapter 15 Improving the View 717
CHapter 16 Working with Dialogs and Controls 747
CHapter 17 Storing and Printing Documents 793
CHapter 18 Programming for Windows 8 827
Index 881
Trang 5
Ivor Horton’s BegInnIng
Trang 8Ivor Horton’s Beginning visual C++® 2013
Copyright © 2014 by Ivor Horton
Published by John Wiley & Sons, Inc., Indianapolis, Indiana
Published simultaneously in Canada
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means,
electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108
of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization
through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers,
MA 01923, (978) 750-8400, fax (978) 646-8600 Requests to the Publisher for permission should be addressed to the
Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201)
748-6008, or online at http://www.wiley.com/go/permissions
Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with
respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including
without limitation warranties of fitness for a particular purpose No warranty may be created or extended by sales or
promotional materials The advice and strategies contained herein may not be suitable for every situation This work
is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional
services If professional assistance is required, the services of a competent professional person should be sought Neither
the publisher nor the author shall be liable for damages arising herefrom The fact that an organization or Web site is
referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or Web site may provide or recommendations it may make Further,
readers should be aware that Internet Web sites listed in this work may have changed or disappeared between when this
work was written and when it is read.
For general information on our other products and services please contact our Customer Care Department within the
United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.
Wiley publishes in a variety of print and electronic formats and by print-on-demand Some material included with
standard print versions of this book may not be included in e-books or in print-on-demand If this book refers to
media such as a CD or DVD that is not included in the version you purchased, you may download this material at
http://booksupport.wiley.com For more information about Wiley products, visit www.wiley.com
Library of Congress Control Number: 2014930412
Trademarks: Wiley, the Wrox logo, Programmer to Programmer, and related trade dress are trademarks or registered
trademarks of John Wiley & Sons, Inc and/or its affiliates, in the United States and other countries, and may not be used without written permission Visual C++ is a registered trademark of Microsoft Corporation All other trademarks are the property of their respective owners John Wiley & Sons, Inc., is not associated with any product or vendor mentioned in
this book.
Trang 9This book is for my dear wife, Eve.
Trang 11aBout tHe autHor
Ivor Horton graduated as a mathematician and was lured into information technology by
promises of great rewards for very little work In spite of the reality usually being a great deal
of work for relatively modest rewards, he has continued to work with computers to the present day He has been engaged at various times in programming, systems design, consultancy, and the management and implementation of projects of considerable complexity
Horton has many years of experience in the design and implementation of computer systems applied
to engineering design and manufacturing operations in a variety of industries He has considerable experience in developing occasionally useful applications in a wide variety of programming
languages, and in teaching primarily scientists and engineers to do likewise He has been writing books on programming for several years, and his currently published works include tutorials on C, C++, and Java At the present time, when he is not writing programming books or providing advice
to others, he spends his time fishing, traveling, and enjoying life in general
aBout tHe teCHnICal edItors
gIovannI dICanIo is a Microsoft Visual C++ MVP, computer programmer, and Pluralsight author His computer programming experience dates back to the glorious Commodore 64 and Commodore Amiga 500 golden days He started with C=64 BASIC, then moved to assembly, Pascal, C, C++, Java, and C# Giovanni wrote computer programming articles on C++, MFC, OpenGL, and other programming subjects in Italian computer magazines He contributed code to some open-source projects as well, including a mathematical expression parser written in C++ for one of the first versions of QCAD Giovanni’s programming experience includes Windows programming using C++, Win32, COM, and ATL His favorite programming languages are C and C++
He has recently started cultivating an interest for mobile platforms and embedded systems
He can be contacted via e-mail at giovanni.dicanio@gmail.com
Trang 12MarC gregoIre is a software engineer from Belgium He graduated from the University of Leuven,
Belgium, with a degree in “Burgerlijk ingenieur in de computer wetenschappen” (equivalent to Master
of Science in engineering in computer science) The year after, he received the cum laude degree of
master in artificial intelligence at the same university After his studies, Marc started working for a
software consultancy company called Ordina Belgium As a consultant, he worked for Siemens and
Nokia Siemens Networks on critical 2G and 3G software running on Solaris for telecom operators
This required working in international teams stretching from South America and the United States
to EMEA and Asia Now, Marc is working for Nikon Metrology on 3D laser scanning software
His main expertise is C/C++, and specifically Microsoft VC++ and the MFC framework He has
experience in developing C++ programs running 24x7 on Windows and Linux platforms; for
example, KNX/EIB home automation software Next to C/C++, Marc also likes C# and uses PHP
for creating web pages
Since April 2007, he received the yearly Microsoft MVP (Most Valuable Professional) award for his
Visual C++ expertise
Marc is the founder of the Belgian C++ Users Group (www.becpp.org), author of Professional C++,
Wrox, 2011 (ISBN 978-047-0-93244-9) and a member on the CodeGuru forum (as Marc G) He
maintains a blog on www.nuonsoft.com/blog/
Trang 13Mary Beth Wakefield
director of Community Marketing
Trang 15tHe autHor is only one member of the large team of people necessary to get a book into print I’d like to thank the John Wiley & Sons and Wrox Press editorial and production teams for their help and support throughout
I would particularly like to thank my technical editors, Marc Gregoire and Giovanni Dicanio, for doing such a fantastic job of reviewing the text and checking out all the code fragments and examples Their many constructive comments and suggestions have undoubtedly made the book a much better tutorial
Trang 17Chapter 1: programming with Visual C++ 1
Templates 4
Summary 23
Trang 18CONTENTS
Chapter 2: Data, Variables, anD CalCulations 25
Whitespace 35
Keywords 39
Literals 45
Trang 19CONTENTS
Trang 20Summary 127
Chapter 4: arrays, strings, anD pointers 129
Trang 21CONTENTS
Accepting a Variable Number of Function Arguments 204
Trang 22CONTENTS
Summary 217
Chapter 6: more about program struCture 221
Exceptions 229
Trang 23CONTENTS
Placing friend Function Definitions Inside the Class 302
Trang 24CONTENTS
Summary 318
Implementing Full Support for Comparison Operators 335
Overloading the Increment and Decrement Operators 348
Trang 25CONTENTS
Chapter 9: Class inheritanCe anD Virtual funCtions 431
Trang 26CONTENTS
Summary 475
Chapter 10: the stanDarD template library 479
Containers 480Allocators 482Comparators 482
Trang 27CONTENTS
Summary 577
Chapter 11: winDows programming ConCepts 579
Trang 28CONTENTS
Chapter 12: winDows programming with
the miCrosoft founDation Classes (mfC) 613
Summary 635
Trang 29CONTENTS
Chapter 13: working with menus anD toolbars 637
Initializing the Color and Element Type Members 653
Summary 660
Trang 30WM_LBUTTONDOWN 682WM_MOUSEMOVE 682WM_LBUTTONUP 682
Summary 713
Trang 31CONTENTS
Summary 745
Chapter 16: working with Dialogs anD Controls 747
Trang 32CONTENTS
Summary 790
Chapter 17: storing anD printing DoCuments 793
Serialization in the Document Class Implementation 795
The Serialize( ) Functions for the Element Classes 807
Trang 33CONTENTS
Summary 825
Chapter 18: programming for winDows 8 827
The Extensible Application Markup Language (XAML) 836
Trang 34CONTENTS
Trang 35Welcome to Ivor Horton’s Beginning Visual C++ 2013 With this book, you can become an
effective C++ programmer using Microsoft’s latest application-development system, Visual Studio Professional 2013 I aim to teach you the C++ programming language, and then how to apply C++
in the development of your own Windows applications Along the way, you will also learn about many of the exciting new capabilities introduced by this latest version of Visual C++
Visual C++ 2013 comes with the Microsoft development environment Visual Studio Professional
2013 When I refer to Visual C++ in the rest of the book, I mean the Visual C++ 2013 capability that comes as part of Visual Studio Professional 2013 Note that the Visual Studio Express 2013
Edition does not provide sufficient facilities for this book None of the examples in Chapters 11
through 18 can be created with Visual Studio Express 2013
Who thIs Book Is For
This book is for anyone who wants to learn how to write C++ applications for Microsoft Windows using Visual C++ I make no assumptions about prior knowledge of any programming language, so there are no prerequisites other than some aptitude for programming and sufficient enthusiasm and commitment for learning C++ This tutorial is for you if:
➤ You are a newcomer to programming and sufficiently keen to jump into the deep end with C++ To be successful, you need to have at least a rough idea of how your computer works
➤ You have a little experience of programming in some other language, such as BASIC, and you want to learn C++ and develop practical Microsoft Windows programming skills
➤ You have some experience in C or C++, but not in a Microsoft Windows context and want
to extend your skills to program for the Windows environment using the latest tools and technologies
What thIs Book covers
The first part of the book teaches you the essentials of C++ programming using Visual Studio
Professional 2013 through a detailed, step-by-step tutorial You’ll learn the syntax and use of the C++ language and gain experience and confidence in applying it in a practical context through working examples You’ll find complete code for the examples that demonstrate virtually all aspects
of C++ There are also exercises that you can use to test your knowledge, with solutions available for download if you get stuck
The language tutorial also introduces and demonstrates the use of the C++ standard library facilities you are most likely to need You’ll add to your knowledge of the standard libraries incrementally as
Trang 36INTRODUCTION
you progress through the C++ language You’ll also learn about the powerful tools provided by the
Standard Template Library (STL)
When you are confident in applying C++, you move on to Windows programming You will learn
how to develop Windows desktop applications using the Microsoft Foundation Classes (MFC) by
creating a substantial working application of more than 2,000 lines of code You develop the
appli-cation over several chapters, utilizing a broad range of user interface capabilities provided by the
MFC You also learn the essentials of programming applications targeting tablets running Windows
8 You learn about creating an application with the Windows 8 Modern interface by incrementally
developing a working example of a game
hoW thIs Book Is structured
The book is structured so that as far as possible, each chapter builds on what you have learned in
previous chapters:
➤ Chapter 1 introduces you to the basic concepts you need to understand for programming in
C++ and the main ideas embodied in the Visual C++ development environment It describes
how you use Visual C++ to create the various kinds of applications you’ll learn about in the
rest of the book
➤ Chapters 2 through 9 teach you the C++ language You start with simple procedural
pro-gram examples and progress to learning about classes and object-oriented propro-gramming
➤ Chapter 10 teaches you how you use the Standard Template Library (STL) The STL is a
powerful and extensive set of tools for organizing and manipulating data in your C++
pro-grams The STL is application-neutral, so you can apply it in a wide range of contexts
➤ Chapter 11 discusses how Microsoft Windows desktop applications are structured and
describes and demonstrates the essential elements that are present in every desktop
applica-tion written for the Windows operating system The chapter explains through elementary
examples how Windows applications work, and you’ll create programs that use C++ with
the Windows API and with the MFC
➤ Chapters 12 through 17 teach you Windows desktop application programming You learn
to write Windows applications using the MFC for building a GUI You’ll be creating and
using common controls to build the graphical user interface for your application, and you’ll
learn how to handle the events that result from user interactions with your program In
addition to the techniques you learn for building a GUI, the application you develop will
show you how to handle printing and how your application can work with files
➤ Chapter 18 introduces the fundamental ideas involved in writing applications for Windows
8 You’ll develop a fully working application that uses the Windows 8 Modern user
interface
All chapters include working examples that demonstrate the programming techniques discussed
Every chapter concludes with a summary of the key points that were covered, and most chapters