wpe4.jpg (1337 bytes) Company Profile
whatsnew.gif (4096 bytes) What's new?
jmnicon.gif (1393 bytes) More about me
wpe3.jpg (1219 bytes) Related sites
Graduat5.wmf (6028 bytes) Courses
? Why a flounder?
cornucopia.gif (1567 bytes) Downloads
MVP Tips Page
Amzn-buy.gif (3051 bytes)

wpe9.jpg (6713 bytes)

How do you write a book this long? Easy. One page at a time. And even then, it isn't all there; perhaps two books would have done the subject justice, but I had only time to write one...

My experience is a considerable part of this book; I have been a Microsoft MVP for many years. You can read some of my MVP essays by going to my MVP page.


Table of Contents


Welcome xliii
Tools Required xliii
Typeface conventions xlv
Icons for insertions xlv

Chapter 1 Introduction to Windows Concepts 1

What Is Windows? 1
What Is Win32? 2
An Historical Perspective on User Interfaces 3
Differences between a Windows Program and a Typical DOS or Unix Program 8
Resource Sharing 8
Graphical User Interface 9
What about Unix, X, Motif, OS/2, or the Macintosh? 9
Input Facilities 9
Memory Management 10
Device-independent Graphics 10
The Windows Programming Model 11
The Conceptual Model 11
Windows and Their Associated Window Functions 14
Windows Queues and the Message Loop 15
Windows Operating Modes 17
Program Memory Models 18
Memory Models and Porting 16-bit Code 18
__cdecl and __stdcall Calling Sequences 18
Static and Dynamic Linking 20
Dynamic Link Libraries 21
Exports and Imports, or Who's Looking for Whom? 22
Windows Memory Management 23
The windows.h Header File 23
windows.h typedef Declarations 33
Getting a Handle on Handles 36
Using the Windows C Runtime Libraries and Header Files 37
Using Strict Type Checking 38
Naming Conventions 38
Function Names 38
Parameter Names 39
Example Code 41
Diving Right In 41
Further Reading 42

Chapter 2 A Skeletal Windows Application 43

The Skeleton Application Source Program 44
The WinMain Function 48
The Parameters to WinMain 48
WinMain Initialization 52
Initialization: initInstance 52
WinMain's Message Loop 72
The Window Function 75
General Structure 75
Message Handling: WM_DESTROY 79
Message Handling: WM_COMMAND 82
The About Dialog Function 85
The Components of the Skeleton Application 87
Resource Definition Files 89
The Skeleton.h Header File 92
Building and Running the Skeleton Program 94
Message Crackers 95
Further Reading 100

Chapter 3 Exploring Variations on a Window 103

Default Behavior for a Window 104
My, How Stylish You Look 109
Class Styles 110
Window Styles 113
Overlapped Windows 114
Pop-up Windows 115
Child Windows 116
Other Window Styles 118
Extended Window Styles 124
The Panes Example Program 129
The Panes Source Code 130
Class Registration in Panes 139
Window Creation in Panes 140
Message Handling in Panes 144
The Window Explorer 150
Further Reading 151

Chapter 4 Displaying text in a Window 153

Device and Display Contexts 155
When Do You Get a Display Context? 155
Displaying Output upon Receipt of a WM_PAINT Message 156
How Do You Get a Display Context? 158
Processing a WM_PAINT Message 160
Using TextOut to Display Text 162
Logical Coordinates and Device Coordinates 163
The MM_TEXT Mapping Mode 165
What Do the Characters Look Like? 168
How to Use Text Metrics 169
Displaying Information about the Windows Environment 171
Introduction to Scroll Bars 177
The Parts of a Scroll Bar 177
Scrolling Text 180
Subclassing a Window Class 188
What is subclassing? 188
Subclassing Techniques 190
The Sysinfo Program 193
The Source Code 194
Sysinfo Initialization 203
Subclassing the System Metrics Window Class 204
Displaying the System Metrics 205
Scrolling the Window 211
Coming Attractions 217

Chapter 5 Examining a Device Context in Depth 219

What Is a Device Context? 219
Display Contexts: A Specific Type of Device Context 221
The Common Display Context 221
The Class Display Context 223
The Private Display Context 225
The Parent Display Context 225
The Window Display Context 226
Other Types of Device Contexts 226
Creating a Device Context 227
The Information Context 227
The Memory Device Context 228
The Metafile Device Context 230
The Capabilities of a Device 233
The Attributes of a Device Context 240
Color Attributes 242
Defining a Color 244
The Bitmap, Brush, Font, Pen, and Region Attributes and Objects 247
Deleting objects 248
Special Considerations on Object Selection 249
The Mapping Mode, Window Origin and Extent, and Viewport Origin
and Extent 253
Various Windows Coordinate Systems 255
The Viewport and the Window 257
The Physical Measurement Mapping Modes 262
The Remaining Device Context Attributes 269
Saving and Restoring the State of a Device Context 272
The DC Explorer 272
Further Reading 273

Chapter 6 Graphical Output: Pixels, Lines, and Polygons 275

Getting and Setting the Color of a Pixel 276
Lines, Pens, and Drawing Modes 277
The GDI Queue 278
Drawing Lines 278
Creating Pens 289
Drawing Modes 293
More-complex Lines 299
Drawing Filled Areas 309
Drawing Rectangles and Ellipses 311
Drawing Polygons 317
Creating and Using Stock and Custom Brushes 320
Stock Brushes 322
Custom Brushes 322
Creating and Using Bitmaps 328
Bitmap Functions 329
Creating a Bitmap 331
Creating a Bitmap Resource from a Bitmap File 332
Creating and Drawing into a Blank Bitmap 338
Creating an Initialized Bitmap 342
Creating an Initialized Bitmap from a DIB 344
The BitBlt, StretchBlt, and StretchDIBits Functions 347
The StretchBlt Function 350
The StretchDIBits Function 352
The MaskBlt and PlgBlt Functions 352
The PlgBlt function 352
The MaskBlt Function 356
Miscellaneous Bitmap and Drawing Functions 357
Bitmap Functions 357
ScrollDC 359
The DrawEdge Function 359
The GDI Batch Queue (in detail) 362
Region Functions 363
Advanced Windows Graphics: Paths and Transformations 370
Transformation Matrices 370
Regions Revisited: ExtCreateRegion 382
Paths: Completely General Shapes for Drawing, Clipping,
and Filling 383
The GDI Explorer 402
What's Next? 402
Further Reading 402

Chapter 7 Keyboard, Mouse, and Timer Input 405

Keyboard Input 406
The Input Focus 410
The Key Press and Release Messages 411
The Virtual-key Code 414
Key Transition Message Crackers 421
Character Messages 424
The Character Sets 427
The Unicode Character Set 433
The Caret 438
The Keyboard Explorer Program 440
Mouse Input 451
Mouse Messages 452
Hit Testing: The WM_NCHITTEST Message 454
Nonclient-area Mouse Messages 456
Client-area Mouse Messages 459
Capturing the Mouse 465
Simple Drag-and-Drop 466
The Cursor 472
Custom Cursors 473
Predefined Cursors 474
Displaying the Cursor 474
Loading Cursors from Other Sources 480
Loading System Cursors 482
Setting System Cursors 484
Moving the Cursor with the Keyboard 484
Towers of Hanoi Example Program 487
Timer Input 504
Sending WM_TIMER Messages to a Window Function 507
Sending WM_TIMER Messages to a Callback Function 507
Using the Timer 508
Structured Exception Handling 510
Further Reading 514

Chapter 8 Using Controls: Overview; Static, Button, and Edit Classes 515

Creating a Control in a Window 517
Control Notifications 520
Using Controls 523
Fonts in Controls 524
The STATIC Class 525
"Dynamic" Static Controls 532
Owner-draw Static Controls 533
Simple Frame Example 534
The BUTTON Class 534
BUTTON Class Styles 534
Button Control Notification Messages 541
Problems with Buttons and the Input Focus 541
Handling Input Focus Changes in Child Windows 542
Push Buttons 543
Check Boxes 545
Radio Buttons 549
Owner-draw Buttons 552
Group Boxes 561
The EDIT Class 563
The Edit Class Styles 563
Changing Styles 567
Messages to an Edit Control 569
Working with Selections 577
Setting Contents, Copy, Cut, and Paste 580
Working with Multiline Edit Controls 581
Line Scrolling and Formatting 583
Coordinate Mapping 586
Edit Control Notification Messages 588
Summary 590

Chapter 9 Using Controls: ListBox, ComboBox, ImageList, and
TreeView Classes 593

The LISTBOX Class 593
The LISTBOX Class Styles 594
Messages to a List Box Control 601
Adding, Inserting, and Deleting Items from a List Box 607
Selecting an Item in a Single-selection List Box 612
Determining the Selection in a Single-selection List Box 613
Selecting Items in a Multiple-selection or Extended-selection List Box 613
Determining the Selection in a Multiple-select List Box 614
File Names in List Boxes 617
Establishing a Horizontal Scroll Bar 619
List Box Notification Messages 620
The COMBOBOX Class 622
The COMBOBOX Class Styles 622
Messages to a Combo Box Control 624
Smart Drop-down Lists 631
Adding, Inserting, and Deleting Items from a Combo Box 634
Selecting an Item in a Combo Box 634
Combo Box Notification Messages 635
Combo Boxes and List Boxes Are Not Interchangeable 636
Improving Performance for List Boxes and Combo Boxes 637
Setting Locales for List Boxes and Combo Boxes 637
Image Lists 642
The TreeView Class 652
Messages to Tree View Controls 655
Inserting Items in a Tree View 660
Selecting an Item 665
Getting Information about an Item 666
Tree View Notifications 667
Image Lists 672
Implementing Drag-and-Drop Features 676
"Smart" Drag-and-Drop 689
Label Editing 697
Stop the Presses 700
Tree Control Summary 701
Further Reading 701

Chapter 10 Using Controls: Scroll Bar, Trackbar, Up/Down, and
Progress Classes 703

The SCROLLBAR Class 704
The SCROLLBAR Class Styles 705
Messages to a Scroll Bar Control 706
Setting Scroll Bar Parameters 709
Retrieving Scroll Bar Parameters 710
Enabling Scroll Bar Arrows 710
Scroll Bar Notifications 711
Keyboard Notifications 712
Using the Size Box 712
The Trackbar Control 714
Using Trackbars 716
Trackbar Messages 718
Simple Trackbars 722
Setting the Line and Page Sizes 723
Trackbars with Selection 723
Tick Marks 724
Simple Tick Marks 725
Custom Tick Marks 725
Problems with Tick Marks in Trackbar Controls 728
Stop The Presses News 733
Up/Down Controls 733
Messages to Up/Down Controls 735
Using Up/Down Controls 736
Notifications from Up/Down Controls 737
Buddy Controls 739
Up/Down Accelerators 743
The Progress Bar Control 744
Stop The Presses 746
The Control Explorer 746
Further Reading 747

Chapter 11 Dialog Boxes 749

What Is a Dialog Box? 750
Dialog Functions and Callback Functions 751
Modal and Modeless Dialog Boxes 752
Modal Dialog Boxes 752
Modeless Dialog Boxes 754
Using Registered Window Messages 759
Creating a Dialog Box 760
Using CreateWindow versus CreateDialog 766
Passing Parameters When Creating a Dialog Box 767
The WM_INITDIALOG Message 768
What to Do in the WM_INITDIALOG Handler 769
Creating a Dialog Box Template in Your Resource Definition File 770
The Pizza-ordering Program 778
Resizable Dialogs 783
Handling Double-click Activation 789
Window-related Data and How to Manage It 790
The GWL_USERDATA Field 793
Returning Values from a Dialog 793
Using Properties 795
Initializing the Pizza Dialog 797
Setting Initial Control Values 800
Changing the Caption Text 803
Enabling and Disabling Controls 804
Saving the State of the Dialog 805
Using Idle Time to Update Controls 806
Some Remaining Problems 809
Creating a Dialog Box Using DLGTEMPLATE Structure 809
Creating a Dialog Box Template: An Example 813
Creating Controls in the Dialog 819
Adding Other Control Functions 822
The Message Box 824
The MessageBox Explorer 830

Chapter 12 Menus, Accelerators, Icons, String Resources, and MessageTable Resources 831

Menus 832
Defining and Creating a Menu 835
Defining a Menu Template in Your Resource Definition File 836
Win32 4.x Menus 840
Creating a Menu Dynamically in Win32 3.x 844
Appending, Inserting, and Modifying Menu Items in Win32 3.x 845
Creating a Menu Dynamically: Win32 4.x 849
The MENUITEMINFO Structure 849
Appending, Inserting, and Modifying Menu Items in Win32 4.x 853
Radio Button Menu Items 855
Default Menu Items 856
Summary: A Comparison of Win32 3.x and Win32 4.x Menu Capabilities 857
Bitmap Menu Items 860
Owner-Draw Menu Items 860
Separator Menu Items 864
String Menu Items 864
Controlling the Appearance of an Item on a Menu in Win32 3.x 865
Controlling the Appearance of an Item on a Menu in Win32 4.x 865
Creating a Menu Based on a Template 865
The Win32 3.x Menu Templates 866
The Win32 4.x Menu Templates 866
Creating the Menu from a Template 868
Class and Window Menus 868
Initializing a Menu before It Is Displayed 869
Altering a Menu 871
Enabling, Disabling, and Graying an Item on a Menu 874
Checking and Unchecking a Menu Item 875
Using a Bitmap as a Menu Item 880
Using a Bitmap as a Custom Menu Item Check Mark 880
Menu Messages 886
Maintaining Menu State 887
Tracking the Menu Selection 890
Using a Floating Pop-up Menu 895
The System Menu 902
The Menus Example Program 905
The Menu Explorer 905
Accelerators 905
Creating an Accelerator Table 905
Loading an Accelerator Table 907
Creating Accelerator Tables Dynamically 908
Displaying Accelerators in the Menu 909
Accelerators and the Message Loop 909
Accelerators without Menu Items 911
Icon Resources 911
Defining an Icon Resource 912
Loading an Icon Resource 913
Displaying an Icon Resource 915
Class Icons 915
The LoadImage Function 916
Drawing Icons Yourself 917
Switching to a Different Icon 920
Displaying Different Icons in Win32 3.x 920
Displaying Different Icons in Win32 4.x 921
Displaying an Icon in a Dialog Box 921
String Resources 923
Defining a String Resource 923
Localization and Strings 924
FormatMessage Function and MESSAGETABLE Resources 927
A Formatted MessageBox Function 929
The FormatMessage Format String 932
The full FormatMessage specification 933
The MESSAGETABLE Resource 936
Further Reading 941

Chapter 13 Printing 943

An Overview of the Printing Process 944
Using the Default Printer 950
Using Any Installed Printer 951
Getting the Printer DC for Any Printer 956
Checking for Selection Options 959
Determining Device Mode Values 959
Hazards with Device Mode Values 967
Setting the dmTTOption 968
The dmPaperSize field 968
The dmDefaultSource Field 969
Setting Device Mode Values 969
Three Elements Required in a Printing Windows Application 973
Obtaining Information about a Printer 973
Allowing Other Program Interactions while Printing 975
The Abort Function 976
The Abort Dialog Function 979
A (Slightly) More Complex Abort Function 981
Printing a Document 982
Using Custom Dialog Templates and Hook Functions 984
Using a Custom Dialog Templates 984
Using a Hook Function 986
A General Printing DLL 993
The Goals of the Print DLL 994
The Input Structure 994
The Query Function 999
Writing the PRINTIT.DLL Callbacks 1000
The FormatLine Callback 1000
The PrintIt Code 1002
The doPrintIt Function 1002
The checkFilter Function 1004
The printLoop Function 1005
The doHeading Function 1010
The startPrint Function 1012
Using an External Abort Dialog Template and Hook Function 1013
Calling doPrintIt with a Custom Dialog and Hook 1015
The Page Setup Dialog 1015
Page Setup Custom Painting Hook 1019
The Print Explorer 1023

Chapter 14 Memory Management 1025

Dynamic Memory Allocation–The Simple Approach 1025
Some Details about Simple Memory Allocation 1026
Fixed, Moveable, and Discardable Memory Blocks 1027
Managing Memory Blocks Using the Global- Functions 1029
Allocating a Fixed Memory Block 1034
Allocating a Moveable Memory Block 1035
Allocating a Discardable Global Memory Block 1035
Allocating a Shareable Memory Block 1036
Locking and Unlocking Memory Blocks 1036
Reallocating a Memory Block 1037
Freeing a Memory Block 1039
Other Default Heap Functions 1039
Dynamic Memory Allocation–When Performance Matters 1040
System Memory Organization 1042
The Local Heap, the Global Heap, and malloc/new
(Win16 programmers only!) 1044
Supporting Multiple Heaps 1045
Considerations When Using the Default Heap 1046
Page Thrashing - Random Access 1047
Page Thrashing - Sequential Access 1047
Synchronization Bottlenecks 1048
Dynamic Heaps 1050
Heap Serialization 1057
Locality of Reference 1058
Reducing Heap Fragmentation 1058
Using C++ 1060
Thread-Local Storage 1065
Using Thread-Local Storage 1067
The thread Attribute 1069
Sharing Thread-Local Storage 1070
Thread-Local Storage Summary 1070
The Heap Explorer 1070
Summary 1070
Further Reading 1070

Chapter 15 Fonts 1073

What Is a Font? 1074
Types of Windows Fonts 1076
Raster Fonts 1076
Stroke or Vector Fonts 1078
TrueType Fonts 1078
Font Resources 1080
Device Fonts 1080
Using a Stock Font 1081
Enumerating Fonts 1083
Defining a Logical Font 1087
The LOGFONT Structure 1088
The Font-matching Scheme 1095
Getting Information About a Font 1097
Kerning 1101
TrueType and ABC Widths 1103
Pair Kerning 1106
Track Kerning 1107
The Font Explorer 1108
Reading the Font Explorer Display 1109
Some Font Explorer Application Tips 1110
Kerning Explorer Anomalies 1111
Well-behaved Fonts 1112
Further Reading 1114

Chapter16 Dynamic Link Libraries 1115

A Special Note to Win16 Programmers 1116
What Is a Dynamic Link Library? 1117
Static and Dynamic Linking 1118
Benefits of Using DLLs 1123
Constraints Unique to a DLL 1124
The Stack 1124
Static Data 1125
The DllEntryPoint Function 1125
A DLL Module Definition File 1129
Importing Symbols 1129
Importing Data 1130
Summary of Export Techniques 1131
Exploring DLL Techniques 1131
A DLL Requiring No Initialization 1132
A DLL Requiring Initialization/Finalization 1135
A Simple Localization Function Library 1136
A DLL Containing Dialog Resources 1138
A Resource-only DLL Containing Strings and Other Resources 1139
DLLs with User-defined Resources 1141
A DLL That Uses the C Runtime System 1144
A DLL That Provides Thread-Local Support 1145
A DLL That Uses a Shared Data Segment 1148
The Wineyes Example DLL Module: Hook Support 1152
System Hook Function Types 1152
The Wineyes Application Module 1155
Further Reading 1163

Chapter 17 The Multiple Document Interface 1165

The Behavior of an MDI Application 1166
The Basic Steps to Creating an MDI Application 1167
Creating the Frame Window 1167
Creating the Client Window 1169
Creating a Child Window 1170
MDI Client Window Messages 1171
The MDI Example Program 1173
Program Initialization 1194
The MDI Application Message Loop 1195
The Frame Window Function 1196
The MDI Child Window Functions 1205
Embedding an Edit Control 1209
Updating Toolbars 1210
WM_CTLCOLORxxx Messages 1211
Non-solid Brushes 1218
Handling MessageBox Color Changes 1218
Object-oriented WM_CTLCOLOR 1223
The CtlColor Explorer 1224
Summary 1224
Further Reading 1225

Chapter 18 Synchronization 1227

Synchronization basics 1227
What is synchronization? 1227
Simple synchronization problems 1227
Synchronization Mechanisms 1229
Correctness doesn't just happen 1230
Win32 Synchronization Primitives 1231
Semaphores 1232
Mutexes 1235
Critical Sections 1237
Events 1240
Waiting for Synchronization 1243
Waiting for Threads 1247
Abandoned Mutexes 1250
Why you must use synchronization primitives 1250
Using Synchronization Primitives 1251
Avoiding Synchronization 1255
Synchronization via Messages 1255
Threads 1256
Thread Creation 1260
Inter-thread control 1261
The Synchronization Explorer 1262
Further Reading 1264

Appendix A Ternary Raster-Operation Codes 1267

Appendix B Message Cracker Summary 1277

Appendix C windowsx.h, commctrl.h, extensions.h, and tchar.h 1289

windowsx.h, commctrl.h and extensions.h 1289
The tchar.h file 1325

Appendix D Using #pragma(warning) 1341

Index 1343

110x40-w-IAW-logo.gif (1831 bytes)
Send mail to newcomer@flounder.com with questions or comments about this web site.
Copyright 1998 The Joseph M. Newcomer Co.
Last modified: March 20, 2003