Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Beginning Visual C++ 2005 (2006) [eng]-1

.pdf
Скачиваний:
94
Добавлен:
16.08.2013
Размер:
18.66 Mб
Скачать

 

Contents

Using a List Box

852

Removing the Scale Dialog

852

Creating a List Box Control

853

Creating the Dialog Class

854

Displaying the Dialog

855

Using an Edit Box Control

856

Creating an Edit Box Resource

856

Creating the Dialog Class

858

The CString Class

858

Adding the Text Menu Item

859

Defining a Text Element

860

Implementing the CText Class

861

The CText Constructor

861

Drawing a CText Object

862

Moving a CText Object

862

Creating a Text Element

863

Summary

864

Exercises

865

Chapter 17: Storing and Printing Documents

867

Understanding Serialization

867

Serializing a Document

868

Serialization in the Document Class Definition

868

Serialization in the Document Class Implementation

869

The Serialize() Function

870

The CArchive Class

870

Functionality of CObject-Based Classes

872

The Macros Adding Serialization to a Class

872

How Serialization Works

873

How to Implement Serialization for a Class

874

Applying Serialization

874

Recording Document Changes

874

Serializing the Document

876

Serializing the Element Classes

877

The Serialize() Functions for the Shape Classes

879

Exercising Serialization

881

Moving Text

882

Printing a Document

884

The Printing Process

885

The CPrintInfo Class

886

xxix

Contents

Chapter 16: Working with Dialogs and Controls

817

Understanding Dialogs

817

Understanding Controls

818

Common Controls

820

Creating a Dialog Resource

820

Adding Controls to a Dialog Box

820

Testing the Dialog

822

Programming for a Dialog

822

Adding a Dialog Class

823

Modal and Modeless Dialogs

824

Displaying a Dialog

824

Code to Display the Dialog

826

Code to Close the Dialog

827

Supporting the Dialog Controls

828

Initializing the Controls

828

Handling Radio Button Messages

830

Completing Dialog Operations

831

Adding Pen Widths to the Document

832

Adding Pen Widths to the Elements

832

Creating Elements in the View

833

Exercising the Dialog

834

Using a Spin Button Control

835

Adding the Scale Menu Item and Toolbar Button

835

Creating the Spin Button

835

The Controls’ Tab Sequence

838

Generating the Scale Dialog Class

838

Dialog Data Exchange and Validation

840

Initializing the Dialog

840

Displaying the Spin Button

841

Using the Scale Factor

842

Scaleable Mapping Modes

842

Setting the Document Size

844

Setting the Mapping Mode

844

Implementing Scrolling with Scaling

846

Setting Up the Scrollbars

847

Working with Status Bars

848

Adding a Status Bar to a Frame

848

Defining the Status Bar Parts

850

Updating the Status Bar

851

xxviii

Contents

Implementing Multipage Printing

888

Getting the Overall Document Size

889

Storing Print Data

890

Preparing to Print

891

Cleaning Up After Printing

892

Preparing the Device Context

893

Printing the Document

894

Getting a Printout of the Document

898

Summary

898

Exercises

899

Chapter 18: Writing Your Own DLLs

901

Understanding DLLs

901

How DLLs Work

903

Run-Time Dynamic Linking

904

Contents of a DLL

906

The DLL Interface

906

The DllMain() Function

906

DLL Varieties

906

MFC Extension DLL

906

Regular DLL — Statically Linked to MFC

907

Regular DLL — Dynamically Linked to MFC

907

Deciding What to Put in a DLL

907

Writing DLLs

908

Writing and Using an Extension DLL

908

Understanding DllMain()

910

Adding Classes to the Extension DLL

911

Exporting Classes from the Extension DLL

912

Building a DLL

913

Using the Extension DLL in Sketcher

914

Files Required to Use a DLL

915

Exporting Variables and Functions from a DLL

916

Importing Symbols into a Program

917

Implementing the Export of Symbols from a DLL

917

Using Exported Symbols

918

Summary

920

Exercises

920

xxx

 

Contents

Chapter 19: Connecting to Data Sources

921

Database Basics

921

A Little SQL

924

Retrieving Data Using SQL

924

Choosing Records

925

Joining Tables Using SQL

926

Sorting Records

929

Database Support in MFC

929

MFC Classes Supporting ODBC

930

Creating a Database Application

931

Registering an ODBC Database

931

Generating an MFC ODBC Program

933

Snapshot versus Dynaset Recordsets

935

Understanding the Program Structure

936

Understanding Recordsets

937

Understanding the Record View

941

Creating the View Dialog

943

Linking the Controls to the Recordset

946

Exercising the Example

948

Sorting a Recordset

948

Modifying the Window Caption

949

Using a Second Recordset Object

950

Adding a Recordset Class

950

Adding a View Class for the Recordset

954

Creating the Dialog Resource

954

Creating the Record View Class

955

Customizing the Recordset

957

Adding a Filter to the Recordset

958

Defining the Filter Parameter

958

Initializing the Record View

960

Accessing Multiple Table Views

961

Switching Views

961

Enabling the Switching Operation

963

Handling View Activation

965

Viewing Orders for a Product

966

Viewing Customer Details

967

Adding the Customer Recordset

967

Creating the Customer Dialog Resource

968

Creating the Customer View Class

968

Adding a Filter

970

Implementing the Filter Parameter

972

xxxi

Contents

Linking the Order Dialog to the Customer Dialog

973

Exercising the Database Viewer

976

Summary

976

Exercises

977

Chapter 20: Updating Data Sources

979

Update Operations

979

CRecordset Update Operations

980

Checking that Operations are Legal

981

Record Locking

982

Transactions

982

CDatabase Transaction Operations

983

A Simple Update Example

984

Customizing the Application

986

Managing the Update Process

988

Implementing Update Mode

990

Enabling and Disabling Edit Controls

991

Changing the Button Label

992

Controlling the Visibility of the Cancel Button

993

Disabling the Record Menu

994

Expediting the Update

996

Implementing the Cancel Operation

997

Adding Rows to a Table

999

The Order Entry Process

1000

Creating the Resources

1001

Creating the Recordsets

1002

Creating the Recordset Views

1002

Adding Controls to the Dialog Resources

1006

Implementing Dialog Switching

1010

Creating an Order ID

1014

Storing the New Order ID

1014

Creating the New Order ID

1015

Initiating ID Creation

1017

Storing the Order Data

1019

Setting Dates

1020

Selecting Products for an Order

1021

Adding a New Order

1023

Summary

1028

Exercises

1029

xxxii

 

Contents

Chapter 21: Applications Using Windows Forms

1031

Understanding Windows Forms

1031

Understanding Windows Forms Applications

1032

Modifying the Properties of a Form

1034

How the Application Starts

1035

Customizing the Application GUI

1035

Adding Controls to a Form

1036

Adding Menus

1037

Adding Submenus

1038

Adding a Tab Control

1040

Using GroupBox Controls

1042

Using Button Controls

1044

Using the WebBrowser Control

1047

Operation of the Winning Application

1048

Adding a Context Menu

1049

Creating Event Handlers

1049

Event Handlers for Menu Items

1050

Adding Members to the Form1 Class

1050

Handling the Play Menu Event

1052

Handling Events for the Limits Menu

1056

Creating a Dialog Box

1056

Adding a List to a ListBox

1058

Handling the Dialog Button Events

1060

Controlling the State of the ListBox Objects

1061

Creating the Dialog Object

1061

Using the Dialog Box

1062

Validating the Input

1063

Handler the Reset Menu Item Event

1067

Adding the Second Dialog

1068

Getting the Data from the Dialog Controls

1070

Disabling Input Controls

1073

Updating the Limits Menu Item Handlers

1074

Implementing the Help | About Menu Item

1075

Handling a Button Click

1076

Responding to the Context Menu

1079

The Logic for Dealing with the Choose Menu Item

1080

Creating the Dialog Form

1080

Developing the Dialog Class

1081

Handling the Click Event for the ChooseMenu

1083

Summary

1086

Exercises

1087

xxxiii

Contents

Chapter 22: Accessing Data Sources in a Windows Forms Application

1089

Working with Data Sources

1089

Accessing and Displaying Data

1090

Using a DataGridView Control

1091

Using a DataGridView Control in Unbound Mode

1093

Customizing a DataGridView Control

1099

Customizing Header Cells

1101

Customizing Non-Header Cells

1101

Setting Up the Data

1102

Setting Up the Control

1103

Setting Up the Column Headers

1105

Formatting a Column

1106

Customizing Alternate Rows

1108

Dynamically Setting Cell Styles

1108

Using Bound Mode

1114

The BindingSource Component

1115

Using the BindingNavigator Control

1120

Binding to Individual Controls

1123

Working with Multiple Tables

1127

Summary

1129

Exercises

1129

Appendix A: C++ Keywords

1131

Appendix B: ASCII Codes

1133

Index

1139

xxxiv

Introduction

Welcome to Beginning Visual C++ 2005. With this book you can become an effective C++ programmer. The latest development system from Microsoft, Visual Studio 2005, supports two distinct but closely related flavors of the C++ language; it fully supports the original ISO/ANSI standard C++, and you also get support for a new version of C++ called C++/CLI that was developed by Microsoft but is now an ECMA standard. These two versions of C++ are complementary and fulfill different roles. ISO/ANSI C++ is there for the development of high-performance applications that run natively on your computer whereas C++/CLI has been developed specifically for the .NET Framework. This book will teach you the essentials of programming applications in both versions of C++.

You get quite a lot of assistance from automatically generated code when writing ISO/ANSI C++ programs, but you still need to write a lot of C++ yourself. You need a solid understanding of object-oriented programming techniques, as well as a good appreciation of what’s involved in programming for Windows. Although C++/CLI targets the .NET Framework, it also is the vehicle for the development of Windows Forms applications that you can develop with little or in some cases no, explicit code writing. Of course, when you do have to add code to a Windows Forms application, even though it may be a very small proportion of the total, you still need an in-depth knowledge of the C++/CLI language.

ISO/ANSI C++ remains the language of choice for many professionals, but the speed of development that C++/CLI and Windows Forms applications bring to the table make that essential, too. For this reason I decided to cover the essentials of both flavors of C++ in this book.

Who This Book Is For

This book is aimed at teaching you how to write C++ applications for the Microsoft Windows operating system using Visual C++ 2005 or any edition of Visual Studio 2005. I make no assumptions about prior knowledge of any particular programming language. This tutorial is for you if:

You have a little experience of programming in some other language, such as BASIC or Pascal, for example, and you are keen 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.

You are a newcomer to programming and sufficiently keen to jump in and the deep end with C++. To be successful you need to have at least a rough idea of how your computer works, including the way in which the memory is organized and how data and instructions are stored.

Introduction

What This Book Covers

My objective with this book is to teach you the essentials of C++ programming using both of the technologies supported by Visual C++ 2005. The book provides a detailed tutorial on both flavors of the C++ language, on native ISO/ANSI C++ Windows application development using the Microsoft Foundation Classes (MFC) and on the development of C++/CLI Windows applications using Windows Forms. Because of the importance and pervasiveness of database technology today, the book also includes introductions to the techniques you can use for accessing data sources in both MFC and Windows Forms applications. MFC applications are relatively coding-intensive compared to Windows Forms applications. This is because you create the latter using a highly developed design capability in Visual C++ 2005 that enables you to assemble the entire graphical user interface (GUI) for an application graphically and have all the code generated automatically. For this reason, there are more pages in the book devoted to MFC programming than to Windows Forms programming.

How This Book Is Structured

The contents of the book are structured as follows:

Chapter 1 introduces you to the basic concepts you need to understand for programming in C++ for native applications and for .NET Framework applications, together with the main ideas embodied in the Visual C++ 2005 development environment. It describes how you use the capabilities of Visual C++ 2005 for creating the various kinds of C++ applications you learn about in the rest of the book.

Chapters 2 to 10 are dedicated to teaching you both versions of the C++ language, plus the basic ideas and techniques involved in debugging. The content of each of the Chapters 2 to 10 is structured in a similar way; the first half of each chapter deals with ISO/ANSI C++ topics, and the second half deals with C++/CLI.

Chapter 11 discusses how Microsoft Windows applications are structured and describes and demonstrates the essential elements that are present in every Windows application. The chapter explains elementary examples of Windows applications using ISO/ANSI C++ and the Windows API and the MFC, as well as an example of a basic Windows Forms application in C++/CLI.

Chapters 12 to 17 describe in detail the capabilities provided by the MFC for building a GUI. You learn how you create and use common controls to build the graphical user interface for your application and how you handle the events that result from user interactions with your program. In the process, you create a substantial working application. In addition to the techniques you learn for building a GUI, the application that you develop also shows you how you use MFC to print documents and to save them on disk.

Chapter 18 teaches you the essentials you need to know for creating your own libraries using MFC. You learn about the different kinds of libraries you can create, and you develop working examples of these that work with the application that you have evolved over the preceding six chapters.

In Chapters 19 and 20, you learn about accessing data sources in an MFC application. You gain experience in accessing a database in read-only mode; then you learn the fundamental programming techniques for updating a database using MFC. The examples use the Northwind database that can be downloaded from the Web, but you can also apply the techniques described to your own data source.

xxxvi

Introduction

In Chapter 21 you work with Windows Forms and C++/CLI to build an example that teaches you how to create, customize, and use Windows Forms controls in an application. You gain practical experience by building an application incrementally throughout the chapter.

Chapter 22 builds on the knowledge you gain in Chapter 21 and shows how the controls available for accessing data sources work, and how you customize them. You also learn how you can create an application for accessing a database with virtually no coding at all on your part.

All chapters include numerous working examples that demonstrate the programming techniques that are discussed. Every chapter concludes with a summary of the key points that were covered, and most chapters include a set of exercises at the end that you can attempt to apply what you have learned.

Solutions to the exercises together with all the code from the book are available for download from the publisher’s Web site (see the “Source Code” section later in this Introduction for more details).

The tutorial on the C++ language uses examples that are console programs with simple command-line input and output. This approach enables you to learn the various capabilities of C++ without getting bogged down in the complexities of Windows GUI programming. Programming for Windows is really only practicable after you have a thorough understanding of the programming language.

If you want to keep things as simple as possible, you can just learn ISO/ANSI C++ programming in the first instance. Each of the chapters that cover the C++ language (Chapters 2 to 10) first discusses particular aspects of the capabilities of ISO/ANSI C++ followed by the new features introduced by C++/CLI in the same context. The reason for organizing things this way is that C++/CLI is defined as an extension to the ISO/ANSI standard language, so an understanding of C++/CLI is predicated on knowledge of ISO/ANSI C++. Thus, you can just read the ISO/ANSI topics in each of Chapters 2 to 20 and ignore the C++/CLI sections that follow. You then can to progress to Windows application development with ISO/ANSI C++ without having to keep the two versions of the language in mind. You can return to C++/CLI when you are comfortable with ISO/ANSI C++. Of course, you can also work straight through and add to your knowledge of both versions of the C++ language incrementally.

What You Need to Use This Book

To use this book you need any of Visual Studio 2005 Standard Edition, Visual Studio 2005 Professional Edition, or Visual Studio 2005 Team System. Note that Visual C++ Express 2005 is not sufficient because the MFC is not included. Visual Studio 2005 requires Windows XP Service Pack 2 or Windows 2000 Service Pack 4.To install any of the three Visual Studio 2005 editions identified you need to have a 1 GHz processor with at least 256MB of memory and at least 1GB of space available on your system drive and 2GB available on the installation drive. To install the full MSDN documentation that comes with the product you’ll need an additional 1.8GB available on the installation drive.

The database examples in the book use the Northwind Traders database. You can find the download for this database by searching for “Northwind Traders” on http://msdn.microsoft.com. Of course, you can also adapt the examples to work with a database of your choice.

Most importantly, to get the most out of this book you need a willingness to learn, and a determination to master the most powerful programming tool for Windows applications presently available. You need the dedication to type in and work through all the examples and try out the exercises in the book. This sounds more difficult than it is, and I think you’ll be surprised how much you can achieve in a relatively

xxxvii