Get a site

VC++ 6.0 ebook chapter index
Free counters!

Chapter 4

An Exercise in Text Output

In the previous chapter, we explored the workings of a simple Windows 98 program that displayed a single line of text in the center of its window or, more precisely, the center of its client area. As we learned, the client area is that part of the total application window that is not taken up by the title bar, the window-sizing border, and, optionally, the menu bar, tool bars, status bar, and scroll bars. In short, the client area is the part of the window on which a program is free to draw and deliver visual information to the user.

You can do almost anything you want with your program's client area—anything, that is, except assume that it will be a particular size or that the size will remain constant while your program is running. If you are not accustomed to writing programs for a graphical windowing environment, these stipulations may come as a bit of a shock. You can't think in terms of a fixed number of 80-character lines. Your program must share the video display with other Windows programs. The Windows user controls how the programs' windows are arranged on the screen. Although it is possible for a programmer to create a window of a fixed size (which might be appropriate for calculators or similar utilities), users are usually able to size application windows. Your program must accept the size it's given and do something reasonable with it.

This works both ways. Just as your program may find itself with a client area barely large enough in which to say "Hello," it may also someday be run on a big-screen, high-resolution video system and discover a client area large enough for two entire pages of text and plenty of closet space besides. Dealing intelligently with both eventualities is an important part of Windows programming.

In this chapter, we will learn how a program displays something on the surface of its client area with more sophistication than that illustrated in the last chapter. When a program displays text or graphics in its client area, it is often said to be "painting" its client area. This chapter is about learning to paint.

Although Windows has extensive Graphics Device Interface (GDI) functions for displaying graphics, in this chapter I'll stick to displaying simple lines of text. I'll also ignore the various font faces and font sizes that Windows makes available and use only Windows' default "system font." This may seem limiting, but it really isn't. The problems we will encounter and solve in this chapter apply to all Windows programming. When you display a combination of text and graphics, the character dimensions of Windows' default font often determine the dimensions of the graphics.

Although this chapter is ostensibly about learning how to paint, it's really about learning the basics of device-independent programming. Windows programs can assume little about the size of their client areas or even the size of text characters. Instead, they must use the facilities that Windows provides to obtain information about the environment in which the program runs.