Ebook banner rotater for Programming

Using
Visual C++ 4.2

Written by Kate Gregory, Clayton Walnum,
                  and Paul Kimmel


CONTENTS

Introduction

Chapter 1   Working with Developer Studio

Chapter 2   Developer Studio Commands

Chapter 3   AppWizard and ClassWizard

Chapter 4   Overview: What's in an Application?

Chapter 5   Messages and Commands

Chapter 6   The Document/View Paradigm

Chapter 7   5Dialog and Controls

Chapter 8   Win95 Common Controls

Chapter 9   Property Pages and Sheets and Wizards

Chapter 10   Utility and Collection Classes

Chapter 11   Drawing on the Screen

Chapter 12   Persistence and File I/O

Chapter 13   Sockets, MAPI, and the Internet

Chapter 14   Database Access

Chapter 15   MFC Macros and Globals

Chapter 16   Choosing an Application Type and Building an Empty Shell

Chapter 17   Building Menus and Dialogs

Chapter 18   Interface Issues

Chapter 19   Printing and Print Preview

Chapter 20   Debugging

Chapter 21   Help

Chapter 22   ActiveX Concepts

Chapter 23   Building an ActiveX Container Application

Chapter 24   Building an ActiveX Server Application

Chapter 25   ActiveX Automation

Chapter 26   Building an ActiveX Control

Chapter 27   Internet Programming with the WinInet Classes

Chapter 28   Building an Internet ActiveX Control

Chapter 29   Power-User Features in Developer Studio

Chapter 30   Power-User C++ Features

Chapter 31   Multitasking with Windows Threads

Chapter 32   Additional Advanced Topics

Credits


Special Edition Using Visual C++ 4.2

Copyright © 1996 by Que ® Corporation.

All rights reserved. Printed in the United States of America. No part of this book may be used or reproduced in any form or by any means, or stored in a database or retrieval system, without prior written permission of the publisher except in the case of brief quotations embodied in critical articles and reviews. Making copies of any part of this book for any purpose other than your own personal use is a violation of United States copyright laws. For information, address Que Corporation, 201 W. 103rd Street, Indianapolis, IN, 46290. You may reach Que's direct sales line by calling 1-800-428-5331.

ISBN: 0-7897-0893-0

HTML conversion by :
    M/s. LeafWriters (India) Pvt. Ltd.
    Website : http://leaf.stpn.soft.net
    e-mail : [email protected]


PresidentRoland Elgey PublisherJoseph B. Wikert
Publishing ManagerFred Slone Director of MarketingLynn E. Zingraf
Editorial Services DirectorElizabeth Keaffaber Managing EditorSandy Doell
Title ManagerBryan Gambrel Project DirectorAngela Kozlowski
Production EditorMatthew B. Cox EditorsKelli Brooks, Sean Dixon, Bill McManus, Jeani Smith
Product Marketing ManagerKim Margolius Assistant Product Marketing Manager Christy M. Miller
Technical EditorsDavid Medinets, Bob Reselman, Daniel Berkowitz, Marc Gusmano
Technical SpecialistNadeem Muhammed Acquisitions CoordinatorCarmen Krikorian
Operations CoordinatorPatricia J. Brooks Editorial AssistantAndrea Duvall
Book DesignerRuth Harvey Cover DesignerDan Armstrong
Production TeamStephen Adams, Debra Bolhuis, Marcia Brizendine, Kevin Cliburn, Jason Hand, Daniel Harris, Bob LaRoche, Angela Perry, Casey Price, Kaylene Riemen, Laura Robbins, Bobbi Satterfield, Staci Somers, Donna Wright
IndexerChris Wilcox

About the Authors

Kate Gregory is a founding partner of Gregory Consulting Limited (www.gregcons.com) and longtime C++ programmer. Her experience with C++ stretches back to before Visual C++ existed, at which point she enthusiastically converted. Gregory Consulting develops software and Internet sites, and specializes in combining software development with Web site development to create active sites. Kate teaches on a variety of related topics including C++, Object-Oriented techniques, using the Internet, HTML, and Java. Her books for Que include "Using UseNet Newsgroups" and "Building Internet Applications with Visual C++," and she has contributed to the "VBPJ Guide to Visual Basic," "Designing Windows 95 Help Systems," and a previous "Special Edition Using Visual C++ " before undertaking this edition. She welcomes mail at [email protected] and provides updates for this book at the gregcons Web site.

Paul Kimmel is the author of Building Delphi 2 Database Applications and Using Borland C++ 5 also from Que. He is the president of the Okemos, Michigan-based Software Conceptions, Inc. which offers software developer training and custom application development around the world. In the mid-Michigan area, we offer training at our partners-in-training location, Automation Resource Corporation founded by Janice Szur. Software Conceptions can be reached at [email protected] or CompuServe at 70353,2711. Paul is the father of three boys: Trevor, Douglas, Noah, and a girl, Alex, and loving husband to Lori.

Clayton Walnum, who has a degree in Computer Science, has been writing about computers for almost 15 years and has published hundreds of articles in major computer publications. He is also the author of over 25 books, which cover such diverse topics as programming, computer gaming, and application programs. His most recent book is Windows 95 Game SDK Strategy Guide, also published by Que. His other titles include the award-winning Building Windows 95 Applications with Visual Basic (Que); 3-D Graphics Programming with OpenGL (Que); Borland C++ 4.x Tips, Tricks, and Traps (Que); Turbo C++ for Rookies (Que); Dungeons of Discovery (Que); PC Picasso: A Child's Computer Drawing Kit (Sams); Powermonger: The Official Strategy Guide (Prima); DataMania: A Child's Computer Organizer (Alpha Kids); Adventures in Artificial Life (Que); and C-manship Complete (Taylor Ridge Books). Mr. Walnum lives in Connecticut with his wife Lynn and their four children: Christopher, Justin, Stephen, and Caitlynn.


Acknowledgments

Kate Gregory: To my husband, Brian, and my wonderful children, Beth and Kevin, my heartfelt thanks once again for all the understanding and support. My neglected family and friends have also earned my gratitude, by not complaining too loudly about my absences from their lives. Writing a book takes a team effort and this one is no exception. My coauthors, technical editors, and illustrators all deserve to share the credit for the good things you see here. The mistakes and omissions I have to claim as mine alone. To Fred Slone, Angela Kozlowski, Matthew Cox, and the other terrific Que people who made this happen, thank you for doing such a great job. Special thanks go out to those readers of my earlier books who took the time to send me email with questions, corrections, and suggestions: you have helped this book to exist, because without readers, writers cannot write. Keep up the good work!


We'd Like to Hear from You!

As part of our continuing effort to produce books of the highest possible quality, Que would like to hear your comments. To stay competitive, we really want you, as a computer book reader and user, to let us know what you like or dislike most about this book or other Que products.

You can mail comments, ideas, or suggestions for improving future editions to the address below, or send us a fax at (317) 581-4663. Our staff and authors are available for questions and comments through our Internet site, at http://www.mcp.com/que, and Macmillan Computer Publishing also has a forum on CompuServe (type GO QUEBOOKS at any prompt).

In addition to exploring our forum, please feel free to contact me personally to discuss your opinions of this book: I'm [email protected] on the Internet.

Thanks in advanceóyour comments will help us to continue publishing the best books available on new computer technologies in today's market.

Angela Kozlowski
Product Director
Que Corporation
201 W. 103rd Street
Indianapolis, Indiana 46290
USA

Introduction

Visual C++ is a powerful and complex tool for building 32-bit applications for Window 95 and Windows NT. These applications are far larger and more complex than their predecessors for 16-bit Windows, or older programs that did not use a graphical user interface. Yet as program size and complexity have grown, programmer effort has actually decreased, at least for programmers who are using the right tools.

Visual C++ is one of the right tools. With its code-generating Wizards it can produce the shell of a working Windows application in seconds. The class library included with Visual C++, the Microsoft Foundation Classes, has become the industry standard for Windows software development in a variety of C++ compilers. The visual editing tools make layout of menus and dialogs a snap. The time you invest in learning to use this product will pay itself back on your first Windows programming project.

Who Should Read This Book?

This book will teach you how to use Visual C++ to build 32-bit Windows applications, including database applications, Internet applications, and applications that tap the power of the ActiveX technology. That's a tall order, and to fit all that in less than a thousand pages, some things had to go. This book will not teach you:

Before You Start Reading

You will need a copy of Visual C++ 4.2, and need to have it installed. The installation process is simple and easy to follow, so it's not covered in this book.

Before you buy Visual C++ 4.2, you'll need a 32-bit Windows operating system: Either Windows 95 or Windows NT Server or Workstation. That means your machine will have to be reasonably powerful and modern: Let's say a 486 or better for your processor, at least 16 MB of RAM and 500 MB of disk space, and a screen that can do 800 ´ 600 pixel displays, or even finer resolutions. The illustrations in this book were all prepared at a resolution of 800 ´ 600, and as you'll see, there are times when things get a little crowded.

Finally, you need to make a resolution: That you will follow along as you go, clicking and typing and trying things out. You don't need to type all the code if you don't want to: It's all on the CD-ROM for you to look at. But you should be ready to open the files and look at the code as you go.

Conventions Used in This Book

One thing this book has plenty of is code. Sometimes, we just need to show you a line or two, so we mix it in with the text like this:


int SomeFunction( int x, int y);

{

	return x+y;

}

You can tell the difference between code and regular text by the fonts we use for each of them. Sometimes, we need to discuss a piece of code that is too large to mix in with the text: you'll find an example in Listing 1.


Listing I.1

CHostDialog dialog(m_pMainWnd);

    if (dialog.DoModal() == IDOK)

    {

    	AppSocket = new CSocket();

    	if (AppSocket->Connect(dialog.m_hostname,119))

    	{

    		while (AppSocket->GetStatus() == CONNECTING)

    		{

    			YieldControl();

    		}

    		if (AppSocket->GetStatus() == CONNECTED)

    		{

			CString response = AppSocket->GetLine();

    			SocketAvailable = TRUE;

    		}

    	}

    }

	if (!SocketAvailable)

	{

		AfxMessageBox("Can't connect to server. Please 

		›;quit.",MB_OK|MB_ICONSTOP);

	}


The character on the second last line (›) is called the code continuation character. It shows a place where we had to break a line of code to fit it on the page, but the line is not broken there in reality. If you're typing code in from the book, don't break the line there, just keep going. If you're reading along in the code from the CD-ROM, don't get confused when the line doesn't break there.

Remember, the code is in the book so that you can understand what is going on, not for you to type in. All the code is on the CD-ROM as well. Sometimes, we'll work our way through the development of an application and show several versions of a block of code as we go-the final version is on the CD-ROM.

TIP
This is a Tip: A shortcut or interesting feature you might want to know about.

NOTE
This is a Note: It covers a subtle but important point. Don't skip notes even if you're the kind who skips tips.

CAUTION
This is a caution, and it's serious. It warns you of horrible consequences if you make a false step, so be sure to read all of these that you come across

TROUBLESHOOTING
Everyone runs into problems now and then while programming. We've anticipated some of them-and we discuss how to get out of whatever trouble you're in.

When a word is in italic, like the word italic just was, it's usually being defined. (Sometimes we're just emphasizing it.) The names of variables, functions, C++ classes, and things you should type are all in monospaced font. Internet URLs are in bold type. Remember, a URL never ends with punctuation, so ignore any comma or period after the URL.

Time to Get Started

That just about wraps things up for the introduction. We've told you what you need to get started, and given you some advanced warning about the notations we use throughout the book-now it's your turn. Jump right in, learn all about writing Windows applications with MFC, and then get started on some development of your own! Good luck and have fun.