Special Edition Using Visual C++ 5 - Introduction

## 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 has 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:

- The C++ programming language: You should already be familiar with C++. There are occasional mentions of relevent of C++ and Object Oriented concepts. The introductory text, C++ By Example, is included on the CD.
- How to use Windows applications: You should be a proficient Windows user, able to resize and move Windows, double-click, and recognize familiar toolbar buttons, for example.
- How to use Visual C++ as a C compiler: If you already work in C, you can use Visual C++ as your compiler, but new developers should take the plunge to C++.
- Windows programming without MFC: This too is OK for those who know it, but not something to learn now that MFC exists.
- The internals of ActiveX programming: This is referred to in the ActiveX chapters, which tell you only what you need to know to make it work.

You should read this book if you fit one of these categories:

- You know some C++ and some Windows programming techniques, and are new to Visual C++. You will learn the product much more quickly than you would if you just tried writing programs.
- You've been working with previous versions of Visual C++. Many times users learn one way to do things and end up overlooking some of the newer productivity features.
- You've been working with Visual C++ 5.0 for a while and are beginning to suspect you're doing things the hard way. Maybe you are.
- You work in Visual C++ 5.0 regularly, and you need to add a feature to your product. For tasks like Help, printing, and threading, you'll find a "hand up" to get started.

### Before You Start Reading

You will need a copy of Visual C++ 5.0, 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++ 5.0, you'll need a 32-bit Windows operating system: Windows 95, or Windows NT Server, or Workstation. That means your machine will have to be reasonably powerful and modern: say a 486 or better for your processor, at least 16M of RAM and 500M of disk space, and a screen that can do 800 x 600 pixel displays, or even finer resolutions. The illustrations in this book were all prepared at a resolution of 800 x 600 and, as you'll see, there are times when things get a little crowded.

Finally, you need to make a promise to yourself: that you will follow along in Visual C++ as you read this book, 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 for you to look at. But you should be ready to open the files and look at the code as you go.

### What This Book Covers

A topic such as Windows programming in Visual C++ covers a lot of ground. This book is split into learning chapters (1 to 28) and appendixes (A to F). Be sure to look over the titles of the appendixes now, and turn to them whenever you aren't quite sure how to do something. They are:

- Appendix A, "Windows Programming Review and a Look Inside CWnd," covers the specifics of Windows programming that are now hidden from you by MFC classes like CWnd.
- Appendix B, "The Developer Studio User Interface," explains all the menus, toolbars, editing areas on the screens, shortcuts, and so on that make up the highly complicated and richly powerful interface between you and Developer Studio.
- Appendix C, "Debugging," explains the extra menus, windows, toolbars, and commands involved in debugging a running application.
- Appendix D, "MFC Macros and Globals," summarizes the many preprocessor macros and global variables and functions sprinkled throughout code generated by the Developer Studio wizards.
- Appendix E, "Useful Classes," describes the classes used throughout the book to manipulate dates, strings, and collections of objects.
- Appendix F, "What's On The CD?," is self-explanatory.

Depending on your background and your willingness to poke around under menus and in the online help, you may just skim these chapters once and never return, or you may fill them full of bookmarks and yellow stickies. While they don't work you through sample applications, they will teach you a lot.

The mainstream of the book is Chapters 1 through 28. Each chapter teaches you an important programming task, or sometimes two closely related tasks, like building a taskbar, or adding Help to an application. Detailed instructions show you how to build a working application, or several working applications, in each chapter.

The first nine chapters cover concepts that will be in almost every Windows application; after that the tasks become less general. Here's a quick overview of some of the work that is covered.

Dialogs and Controls

What Windows program doesn't have a dialog box? An edit box? A button? Dialog boxes and controls are vital to Windows user interfaces, and all of them, even the simple button or piece of static text, are windows. The common controls allow you to take advantage of the learning time users have put in on other programs, and the programming time developers have put in on the operating system, to use the same File Open dialog box as everybody else, the same hierarchical tree control, and so on. Learn more about all these controls in Chapter 2, "Dialog Boxes and Controls," and Chapter 3, "Windows 95 Common Controls."

Messages and Commands

Messages form the heart of Windows programming. Whenever anything happens on a Windows machine, such as a user clicking the mouse or pressing a key, a message is triggered and sent to one or more windows, which do something about it. Visual C++ makes it easy for you to write code that catches these messages and acts on them. Chapter 4, "Messages and Commands," explains the concept of messages and how MFC and other aspects of Visual C++ lets you deal with them.

The View/Document Paradigm

A paradigm is a model, a way of looking at things. The designers of MFC chose to design the framework by using the assumption that every program has something it wants to save in a file. That collection of information is referred to as the document. A view is one way of looking at a document. There are a lot of advantages to separating the view and the document, and they are explained further in Chapter 5, "Documents and Views." MFC provides classes from which to inherit your document class and your view class, so that common programming tasks like implementing scroll bars are no longer your problem.

Drawing on the Screen

No matter how smart your Windows program is, if you can't tell the user what's going on by putting some words or pictures onto the screen, no one will know what the program has done. A remarkably large amount of the work is done automatically by your view classes (one of the advantages of adopting the document/view paradigm), but there will be times you have to do the drawing yourself. You learn about device contexts, scrolling, and more in Chapter 6, "Drawing on the Screen."

Printing on Paper

Adding printing capabilities to your program is sometimes the simplest thing in the world, because the code you use to draw on the screen can be reused to draw on paper. But if there is more than one page of information involved, things start to get tricky. Chapter 7, "Printing and Print Preview," explains all of this, plus mapping modes, headers and footers, and more.

Persistence and File I/O

Some good things are meant to be only temporary, like the display of a calculator or an online chat window. But most programs can save their documents to a file, and open and load that file to recreate a document that has been stored. MFC makes this remarkably easy by using archives and extending the use of the stream I/O operators >> and <<. You learn all about reading and writing to files in Chapter 8, "Persistence and File I/O."

ActiveX Programming

ActiveX is the successor to OLE, and it is the technology that facilitates communication between applications at the object level, allowing you to embed a Word document in an Excel spreadsheet, or any of hundreds of kinds of objects in any ActiveX application. ActiveX chapters include Chapter 13, "ActiveX Concepts," Chapter 14, "Building an ActiveX Container Application," Chapter 15, "Building an ActiveX Server Application," Chapter 16, "Building an AutomationServer," and Chapter 17, "Building an ActiveX Control."

The Internet

Microsoft recognizes that distributed computing, in which work is shared among two or more different computers, is becoming more and more common. Programs need to talk to each other, people need to send messages across a LAN or around the world, and MFC has classes that support these kinds of communication. The four Internet chapters in this book are Chapter 18, "Sockets, MAPI, and the Internet," Chapter 19, "Internet Programming with the WinInet Classes," Chapter 20, "Building an Internet ActiveX Control," and Chapter 21, "The Active Template Library."

Database Access

Database programming just keeps getting easier. ODBC, Microsoft's Open DataBase Connectivity package, allows your code to call API functions that access a huge variety of database files-Oracle, DBase, an Excel spreadsheet, a plain text file, old legacy mainframe systems using SQL, whatever! You call a standardlynamed function and the API provided by the database vendor or a third party handles the translation. The details are in Chapter 22, "Database Access," and Chapter 23, "SQL and theEnterprise Edition."

Advanced Material

For developers who have mastered the basics, this book features some advanced chapters to move your programming skills forward. You will learn how to prevent memory leaks, find bottlenecks, and find bugs in your code with the techniques discussed in Chapter 24, "Improving Your Application's Performance."

Reuse is a hugely popular concept in software development at the moment, especially with managers who see a chance to lower their development budget. If you'd like to write reusable code and components, Chapter 25, "Achieving Reuse with the Gallery and Your Own AppWizards," will get you there.

The C++ language is still quite new, and it changes every year. A working ANSI committee is building the C++ standard, and as they work towards a final definition of the language, compiler vendors add entirely new keywords and capabilitities. You will learn in Chapter 26, "Exceptions, Templates, and the Latest Additions to C++," what's new in the language itself.

As user demands for high-performance software continue to rise, developers are having to learn entirely new techniques to produce powerful applications that provide fast response times. For many developers, writing multithreaded applications is a vital technique. Learn about threading in Chapter 27, "Multitasking with Windows Threads."

Chapter 28, "Future Explorations," introduces you to topics that are definitely not for beginners. Learn how to create console applications, use and build your own DLLs, and work with Unicode.

### Conventions Used in This Book

One thing this book has plenty of is code. Sometimes you just need to see a line or two, so the code is mixed 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 used for each of them. Sometimes, you'll see a piece of code that is too large to mix in with the text: You'll find an example in Listing 0.1.

Listing 0.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

         [ccc] quit.",MB_OK|MB_ICONSTOP);

         }

The character on the second last line ([ccc]) is called the code continuation character. It shows a place where a line of code had to be broken 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, 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, youll work your way through the development of an application and see several versions of a block of code as you go--the final version is on the CD-ROM.

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

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

> 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.

When a word is in italic, it's usually being defined. (Sometimes the word's just being emphasized.) 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. You've learned what you need to get started, including some advanced warning about the notations used throughout the book. 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.

© 1997, QUE Corporation, an imprint of Macmillan Publishing USA, a Simon and Schuster Company.