You are previewing Programming Windows®, Fifth Edition.
O'Reilly logo
Programming Windows®, Fifth Edition

Book Description

“Look it up in Petzold” remains the decisive last word in answering questions about Windows development. And in PROGRAMMING WINDOWS, FIFTH EDITION, the esteemed Windows Pioneer Award winner revises his classic text with authoritative coverage of the latest versions of the Windows operating system—once again drilling down to the essential API heart of Win32 programming. Topics include:

  • The basics—input, output, dialog boxes

  • An introduction to Unicode

  • Graphics—drawing, text and fonts, bitmaps and metafiles

  • The kernel and the printer

  • Sound and music

  • Dynamic-link libraries

  • Multitasking and multithreading

  • The Multiple-Document Interface

  • Programming for the Internet and intranets

  • Packed as always with definitive examples, this newest Petzold delivers the ultimate sourcebook and tutorial for Windows programmers at all levels working with Microsoft Windows 95, Windows 98, or Microsoft Windows NT. No aspiring or experienced developer can afford to be without it.

    An electronic version of this book is available on the companion CD.

    For customers who purchase an ebook version of this title, instructions for downloading the CD files can be found in the ebook.

    Table of Contents

    1. Programming Windows®, Fifth Edition
    2. A Note Regarding Supplemental Files
    3. Author's Note
    4. I. The Basics
      1. 1. Getting Started
        1. The Windows Environment
          1. A History of Windows
          2. Aspects of Windows
          3. Dynamic Linking
        2. Windows Programming Options
          1. APIs and Memory Models
          2. Language Options
          3. The Programming Environment
          4. API Documentation
        3. Your First Windows Program
          1. A Character-Mode Model
          2. The Windows Equivalent
          3. The Header Files
          4. Program Entry Point
          5. The MessageBox Function
          6. Compile, Link, and Run
      2. 2. An Introduction to Unicode
        1. A Brief History of Character Sets
          1. American Standards
          2. The World Beyond
          3. Extending ASCII
          4. Double-Byte Character Sets
          5. Unicode to the Rescue
        2. Wide Characters and C
          1. The char Data Type
          2. Wider Characters
          3. Wide-Character Library Functions
          4. Maintaining a Single Source
        3. Wide Characters and Windows
          1. Windows Header File Types
          2. The Windows Function Calls
          3. Windows' String Functions
          4. Using printf in Windows
          5. A Formatting Message Box
          6. Internationalization and This Book
      3. 3. Windows and Messages
        1. A Window of One's Own
          1. An Architectural Overview
          2. The HELLOWIN Program
          3. Thinking Globally
            1. The Windows Function Calls
            2. Uppercase Identifiers
            3. New Data Types
            4. Getting a Handle on Handles
            5. Hungarian Notation
          4. Registering the Window Class
          5. Creating the Window
          6. Displaying the Window
          7. The Message Loop
          8. The Window Procedure
          9. Processing the Messages
          10. Playing a Sound File
          11. The WM_PAINT Message
          12. The WM_DESTROY Message
        2. The Windows Programming Hurdles
          1. Don't Call Me, I'll Call You
          2. Queued and Nonqueued Messages
          3. Get In and Out Fast
      4. 4. An Exercise in Text Output
        1. Painting and Repainting
          1. The WM_PAINT Message
          2. Valid and Invalid Rectangles
        2. An Introduction to GDI
          1. The Device Context
          2. Getting a Device Context Handle: Method One
          3. The Paint Information Structure
          4. Getting a Device Context Handle: Method Two
          5. TextOut: The Details
          6. The System Font
          7. The Size of a Character
          8. Text Metrics: The Details
          9. Formatting Text
          10. Putting It All Together
          11. The SYSMETS1.C Window Procedure
          12. Not Enough Room
          13. The Size of the Client Area
        3. Scroll Bars
          1. Scroll Bar Range and Position
          2. Scroll Bar Messages
          3. Scrolling SYSMETS
          4. Structuring Your Program for Painting
        4. Building a Better Scroll
          1. The Scroll Bar Information Functions
          2. How Low Can You Scroll?
          3. The New Sysmets
          4. But I Don't Like to Use the Mouse
      5. 5. Basic Drawing
        1. The Structure of GDI
          1. The GDI Philosophy
          2. The GDI Function Calls
          3. The GDI Primitives
          4. Other Stuff
        2. The Device Context
          1. Getting a Device Context Handle
          2. Getting Device Context Information
          3. The DEVCAPS1 Program
          4. The Size of the Device
          5. Finding Out About Color
          6. The Device Context Attributes
          7. Saving Device Contexts
        3. Drawing Dots and Lines
          1. Setting Pixels
          2. Straight Lines
          3. The Bounding Box Functions
          4. Bezier Splines
          5. Using Stock Pens
          6. Creating, Selecting, and Deleting Pens
          7. Filling in the Gaps
          8. Drawing Modes
        4. Drawing Filled Areas
          1. The Polygon Function and the Polygon-Filling Mode
          2. Brushing the Interior
        5. The GDI Mapping Mode
          1. Device Coordinates and Logical Coordinates
          2. The Device Coordinate Systems
          3. The Viewport and the Window
          4. Working with MM_TEXT
          5. The Metric Mapping Modes
          6. The "Roll Your Own" Mapping Modes
            1. The MM_ISOTROPIC Mapping Mode
            2. MM_ANISOTROPIC: Stretching the Image to Fit
          7. The WHATSIZE Program
        6. Rectangles, Regions, and Clipping
          1. Working with Rectangles
          2. Random Rectangles
          3. Creating and Painting Regions
          4. Clipping with Rectangles and Regions
          5. The CLOVER Program
      6. 6. The Keyboard
        1. Keyboard Basics
          1. Ignoring the Keyboard
          2. Who's Got the Focus?
          3. Queues and Synchronization
          4. Keystrokes and Characters
        2. Keystroke Messages
          1. System and Nonsystem Keystrokes
          2. Virtual Key Codes
          3. The lParam Information
            1. Repeat Count
            2. OEM Scan Code
            3. Extended Key Flag
            4. Context Code
            5. Previous Key State
            6. Transition State
          4. Shift States
          5. Using Keystroke Messages
          6. Enhancing SYSMETS for the Keyboard
        3. Character Messages
          1. The Four Character Messages
          2. Message Ordering
          3. Control Character Processing
          4. Dead-Character Messages
        4. Keyboard Messages and Character Sets
          1. The KEYVIEW1 Program
          2. The Foreign-Language Keyboard Problem
          3. Character Sets and Fonts
          4. What About Unicode?
          5. TrueType and Big Fonts
        5. The Caret (Not the Cursor)
          1. The Caret Functions
          2. The TYPER Program.
      7. 7. The Mouse
        1. Mouse Basics
          1. Some Quick Definitions
          2. The Plural of Mouse Is…
        2. Client-Area Mouse Messages
          1. Simple Mouse Processing: An Example
          2. Processing Shift Keys
          3. Mouse Double-Clicks
        3. Nonclient-Area Mouse Messages
          1. The Hit-Test Message
          2. Messages Beget Messages
        4. Hit-Testing in Your Programs
          1. A Hypothetical Example
          2. A Sample Program
          3. Emulating the Mouse with the Keyboard
          4. Add a Keyboard Interface to CHECKER
          5. Using Child Windows for Hit-Testing
          6. Child Windows in CHECKER
          7. Child Windows and the Keyboard
        5. Capturing the Mouse
          1. Blocking Out a Rectangle
          2. The Capture Solution
          3. The BLOKOUT2 Program
        6. The Mouse Wheel
          1. Still to Come
      8. 8. The Timer
        1. Timer Basics
          1. The System and the Timer
          2. Timer Messages Are Not Asynchronous
        2. Using the Timer: Three Methods
          1. Method One
          2. Method Two
          3. Method Three
        3. Using the Timer for a Clock
          1. Building a Digital Clock
          2. Getting the Current Time
          3. Displaying Digits and Colons
          4. Going International
          5. Building an Analog Clock
        4. Using the Timer for a Status Report
      9. 9. Child Window Controls
        1. The Button Class
          1. Creating the Child Windows
          2. The Child Talks to Its Parent
          3. The Parent Talks to Its Child
          4. Push Buttons
          5. Check Boxes
          6. Radio Buttons
          7. Group Boxes
          8. Changing the Button Text
          9. Visible and Enabled Buttons
          10. Buttons and Input Focus
        2. Controls and Colors
          1. System Colors
          2. The Button Colors
          3. The WM_CTLCOLORBTN Message
          4. Owner-Draw Buttons
        3. The Static Class
        4. The Scroll Bar Class
          1. The COLORS1 Program
          2. The Automatic Keyboard Interface
          3. Window Subclassing
          4. Coloring the Background
          5. Coloring the Scroll Bars and Static Text
        5. The Edit Class
          1. The Edit Class Styles
          2. Edit Control Notification
          3. Using the Edit Controls
          4. Messages to an Edit Control
        6. The Listbox Class
          1. List Box Styles
          2. Putting Strings in the List Box
          3. Selecting and Extracting Entries
          4. Receiving Messages from List Boxes
          5. A Simple List Box Application
          6. Listing Files
            1. Using file attribute codes
            2. Ordering file lists
          7. A head for Windows
      10. 10. Menus and Other Resources
        1. Icons, Cursors, Strings, and Custom Resources
          1. Adding an Icon to a Program
          2. Getting a Handle on Icons
          3. Using Icons in Your Program
          4. Using Customized Cursors
          5. Character String Resources
          6. Custom Resources
        2. Menus
          1. Menu Concepts
          2. Menu Structure
          3. Defining the Menu
          4. Referencing the Menu in Your Program
          5. Menus and Messages
          6. A Sample Program
          7. Menu Etiquette
          8. Defining a Menu the Hard Way
          9. Floating Popup Menus
          10. Using the System Menu
          11. Changing the Menu
          12. Other Menu Commands
          13. An Unorthodox Approach to Menus
        3. Keyboard Accelerators
          1. Why You Should Use Keyboard Accelerators
          2. Some Rules on Assigning Accelerators
          3. The Accelerator Table
          4. Loading the Accelerator Table
          5. Translating the Keystrokes
          6. Receiving the Accelerator Messages
          7. POPPAD with a Menu and Accelerators
          8. Enabling Menu Items
          9. Processing the Menu Options
      11. 11. Dialog Boxes
        1. Modal Dialog Boxes
          1. Creating an "About" Dialog Box
          2. The Dialog Box and Its Template
          3. The Dialog Box Procedure
          4. Invoking the Dialog Box
          5. Variations on a Theme
          6. A More Complex Dialog Box
          7. Working with Dialog Box Controls
          8. The OK and Cancel Buttons
          9. Avoiding Global Variables
          10. Tab Stops and Groups
          11. Painting on the Dialog Box
          12. Using Other Functions with Dialog Boxes
          13. Defining Your Own Controls
        2. Modeless Dialog Boxes
          1. Differences Between Modal and Modeless Dialog Boxes
          2. The New COLORS Program
          3. HEXCALC: Window or Dialog Box?
        3. The Common Dialog Boxes
          1. POPPAD Revisited
          2. Unicode File I/O
          3. Changing the Font
          4. Search and Replace
          5. The One-Function-Call Windows Program
      12. 12. The Clipboard
        1. Simple Use of the Clipboard
          1. The Standard Clipboard Data Formats
          2. Memory Allocation
          3. Transferring Text to the Clipboard
          4. Getting Text from the Clipboard
          5. Opening and Closing the Clipboard
          6. The Clipboard and Unicode
        2. Beyond Simple Clipboard Use
          1. Using Multiple Data Items
          2. Delayed Rendering
          3. Private Data Formats
        3. Becoming a Clipboard Viewer
          1. The Clipboard Viewer Chain
          2. Clipboard Viewer Functions and Messages
          3. A Simple Clipboard Viewer
    5. II. More Graphics
      1. 13. Using the Printer
        1. Printing Fundamentals
          1. Printing and Spooling
          2. The Printer Device Context
          3. The Revised DEVCAPS Program
          4. The PrinterProperties Call
          5. Checking for BitBlt Capability
          6. The Simplest Printing Program
        2. Printing Graphics and Text
          1. Bare-Bones Printing
          2. Canceling Printing with an Abort Procedure
          3. How Windows Uses AbortProc
          4. Implementing an Abort Procedure
          5. Adding a Printing Dialog Box
          6. Adding Printing to POPPAD
      2. 14. Bitmaps and Bitblts
        1. Bitmap Basics
          1. Where Do Bitmaps Come From?
        2. Bitmap Dimensions
          1. Color and Bitmaps
          2. Real-World Devices
          3. Bitmap Support in GDI
        3. The Bit-Block Transfer
          1. A Simple BitBlt
          2. Stretching the Bitmap
          3. The StretchBlt Mode
          4. The Raster Operations
          5. The Pattern Blt
        4. The GDI Bitmap Object
          1. Creating a DDB
          2. The Bitmap Bits
          3. The Memory Device Context
          4. Loading Bitmap Resources
          5. The Monochrome Bitmap Format
          6. Brushes from Bitmaps
          7. Drawing on Bitmaps
          8. The Shadow Bitmap
          9. Using Bitmaps in Menus
          10. Nonrectangular Bitmap Images
          11. Some Simple Animation
          12. Bitmaps Outside the Window
      3. 15. The Device-Independent Bitmap
        1. The DIB File Format
          1. The OS/2-Style DIB
          2. Bottoms Up!
          3. The DIB Pixel Bits
          4. The Expanded Windows DIB
          5. Reality Check
          6. DIB Compression
          7. Color Masking
          8. The Version 4 Header
          9. The Version 5 Header
          10. Displaying DIB Information
        2. Displaying and Printing
          1. Digging into the DIB
          2. Pixel to Pixel
          3. The Topsy-Turvy World of DIBs
          4. Sequential Display
          5. Stretching to Fit
          6. Color Conversion, Palettes, and Performance
        3. The Union of DIBs and DDBs
          1. Creating a DDB from a DIB
          2. From DDB to DIB
          3. The DIB Section
          4. More DIB Section Differences
          5. The File-Mapping Option
          6. In Summary
      4. 16. The Palette Manager
        1. Using Palettes
          1. Video Hardware
          2. Displaying Gray Shades
          3. The Palette Messages
          4. The Palette Index Approach
          5. Querying the Palette Support
          6. The System Palette
          7. Other Palette Functions
          8. The Raster-Op Problem
          9. Looking at the System Palette
        2. Palette Animation
          1. The Bouncing Ball
          2. One-Entry Palette Animation
          3. Engineering Applications
        3. Palettes and Real-World Images
          1. Palettes and Packed DIBs
          2. The All-Purpose Palette
          3. The Halftone Palette
          4. Indexing Palette Colors
          5. Palettes and Bitmap Objects
          6. Palettes and DIB Sections
        4. A Library for DIBs
          1. The DIBSTRUCT Structure
          2. The Information Functions
          3. Reading and Writing Pixels
          4. Creating and Converting
          5. The DIBHELP Header File and Macros
          6. The DIBBLE Program
            1. File Loading and Saving
            2. Displaying, Scrolling, and Printing
            3. The Clipboard
            4. Flipping and Rotating
          7. Simple Palettes; Optimized Palettes
            1. The Uniform Distribution
            2. The "Popularity" Algorithm
            3. The "Median Cut" Algorithm
          8. Converting Formats
      5. 17. Text and Fonts
        1. simple text output
          1. The Text Drawing Functions
          2. Device Context Attributes for Text
          3. Using Stock Fonts
        2. background on fonts
          1. The Types of Fonts
          2. TrueType Fonts
          3. Attributes or Styles?
          4. The Point Size
          5. Leading and Spacing
          6. The Logical Inch Problem
        3. The Logical Font
          1. Logical Font Creation and Selection
          2. The PICKFONT Program
          3. The Logical Font Structure
          4. The Font-Mapping Algorithm
          5. Finding Out About the Font
          6. Character Sets and Unicode
          7. The EZFONT System
          8. Font Rotation
        4. Font Enumeration
          1. The Enumeration Functions
          2. The ChooseFont Dialog
        5. Paragraph Formatting
          1. Simple Text Formatting
          2. Working with Paragraphs
          3. Previewing Printer Output
        6. The Fun and Fancy Stuff
          1. The GDI Path
          2. Extended Pens
          3. Four Sample Programs
      6. 18. Metafiles
        1. The Old Metafile Format
          1. Simple Use of Memory Metafiles
          2. Storing Metafiles on Disk
          3. Old Metafiles and the Clipboard
        2. Enhanced Metafiles
          1. The Basic Procedure
          2. Looking Inside
          3. Metafiles and GDI Objects
          4. Metafiles and Bitmaps
          5. Enumerating the Metafile
          6. Embedding Images
          7. An Enhanced Metafile Viewer and Printer
          8. Displaying Accurate Metafile Images
          9. Scaling and Aspect Ratios
          10. Mapping Modes in Metafiles
          11. Mapping and Playing
    6. III. Advanced Topics
      1. 19. The Multiple-Document Interface
        1. MDI Concepts
          1. The Elements of MDI
          2. MDI Support
        2. A Sample MDI Implementation
          1. Three Menus
          2. Program Initialization
          3. Creating the Children
          4. More Frame Window Message Processing
          5. The Child Document Windows
          6. Cleaning Up
      2. 20. Multitasking and Multithreading
        1. Modes of Multitasking
          1. Multitasking Under DOS?
          2. Nonpreemptive Multitasking
          3. PM and the Serialized Message Queue
          4. The Multithreading Solution
          5. Multithreaded Architecture
          6. Thread Hassles
          7. The Windows Advantage
          8. New! Improved! Now with Threads!
        2. Windows Multithreading
          1. Random Rectangles Revisited
          2. The Programming Contest Problem
          3. The Multithreaded Solution
          4. Any Problems?
          5. The Benefits of Sleep
        3. Thread Synchronization
          1. The Critical Section
        4. Event Signaling
          1. The BIGJOB1 Program
          2. The Event Object
        5. Thread Local Storage
      3. 21. Dynamic-Link Libraries
        1. Library Basics
          1. Library: One Word, Many Meanings
          2. A Simple DLL
          3. The Library Entry and Exit Point
          4. The Test Program
          5. Shared Memory in DLLs
          6. The STRPROG Program
          7. Sharing Data Among STRPROG Instances
        2. Miscellaneous DLL Topics
          1. Dynamic Linking Without Imports
          2. Resource-Only Libraries
      4. 22. Sound and Music
        1. Windows and Multimedia
          1. Multimedia Hardware
          2. An API Overview
          3. Exploring MCI with TESTMCI
          4. MCITEXT and CD Audio
        2. Waveform Audio
          1. Sound and Waveforms
          2. Pulse Code Modulation
          3. The Sampling Rate
          4. The Sample Size
          5. Generating Sine Waves in Software
          6. A Digital Sound Recorder
          7. The MCI Alternative
          8. The MCI Command String Approach
          9. The Waveform Audio File Format
          10. Experimenting with Additive Synthesis
          11. Waking Up to Waveform Audio
        3. MIDI and Music
          1. The Workings of MIDI
          2. The Program Change
          3. The MIDI Channel
          4. MIDI Messages
          5. An Introduction to MIDI Sequencing
          6. Playing a MIDI Synthesizer from the PC Keyboard
          7. A MIDI Drum Machine
          8. The Multimedia time Functions
          9. RIFF File I/O
      5. 23. A Taste of the Internet
        1. Windows Sockets
          1. Sockets and TCP/IP
          2. Network Time Services
          3. The NETTIME Program
        2. WinInet and FTP
          1. Overview of the FTP API
          2. The Update Demo
    7. Index
    8. About the Author
    9. Copyright