RESOURCES AND MEMORY

The LoadIcon and LoadCursor functions certainly sound as if they load the icon or cursor from the .EXE file into memory. They do not. Windows doesn't load the icon or cursor until it needs the object for drawing. During loading, Windows may alter the object to fit the dimensions and color capabilities of the display.

Icons and cursors (as well as all other resources except bitmaps) are ”owned“ by the program. Multiple instances of the same program share the same cursors and icons loaded into memory. When the last instance terminates, Windows frees up the memory occupied by the resource. And for most resources, Windows can discard the resource from memory to generate free space and then load it back into memory when needed.

You can override some of these characteristics, however. For all resources except the keyboard accelerators (covered in Chapter 9), you can specify ”load“ and ”memory“ options in the resource script file. These options are similar to the module definition file options for code and data segments discussed in Chapter 7. In the resource script, the load and memory options follow the resource type. This is the generalized form of the ICON statement in a resource script file:

iconID ICON [load-option] [memory-option] iconfile.ico

The load option can be either PRELOAD or LOADONCALL. A resource defined as PRELOAD will be loaded into memory when the program is loaded. LOADONCALL means that the resource will not be loaded until Windows needs it. LOADONCALL is the default for all resources. You will probably want to use PRELOAD only when you know that your program will need the resource immediately after beginning to execute.

The memory options are FIXED, MOVEABLE, and DISCARDABLE. DISCARDABLE resources must also be MOVEABLE. For the resources discussed in this chapter, the icon, cursor, and character string resources have default memory options of MOVEABLE and DISCARDABLE. The bitmap and user-defined resources are MOVEABLE only. Why the difference? Icon, cursor, and character string resources are read-only, so Windows can safely discard them from memory. Windows allows bitmaps and user-defined resources to be modified from within a program—and modified resources cannot be discarded.