Before we get any deeper I will cover the topic of resources so that I won't have to re-write it for each section. You don't actually need to compile the stuff in this section, it's as example only.
Resources are pre-defined bits of data stored in binary format inside your executable file. You can create resources manually in a resources script, a file with an extension of ".rc". Commercial development environments, such as Stony Brook M2, allow you to create resources by means of a visual Resource Editor which does not require manual editing of a resource script. The resource editor creates files with extension ".res", which are compiled resource files. All resource files we use in this tutorial are included in the download.
We will not go into detail on how to work with the Resource File editor, but it is important to mention that to every object (menus, menu items, icons, etc.) in the resource file we can assign a unique ID, which we will use in our program to identify the object.
Let's have a look at the resource we will use in the next chapter of this tutorial.
This resource file contains an icon and a menu.
As you can see
For this purpose, the identifiers are defined in a Modula-2 definition file named
DEFINITION MODULE resource; CONST IDR_MYMENU = 101; IDI_MYICON = 102; ID_FILE_EXIT = 40001; ID_STUFF_GO = 40002; ID_STUFF_GOSOMEWHEREELSE = 40003; END resource.
Now lets look at the menu resource:
As you can see we have assigned an identifier
Notes on using a resource file in our program
Now an example of how to use a resource in our program. The problem at hand is how to load the icon from the resource and how to refer to it in our program.
PROCEDURE LoadIcon(a : HINSTANCE; lpIconName : ARRAY OF ACHAR) : HICON;
The first parameter of
The solution is to use the corresponding procedure exported from the WINX module:
PROCEDURE LoadIconId(inst : HINSTANCE; idNum : CARDINAL) : HICON;
It's a good idea to browse through the WINX module. This module provides strongly typed versions of some of the more loosely typed API calls. WINX also contains some strongly typed constants, and some NIL variables for Modula-2 modified parameters such as RECORD and ARRAY OF parameters. These NIL variables exist because Windows often provides optional behaviors on API functions when a NIL pointer is passed. In Modula-2, since these have been mapped to structures and arrays, a clean way was needed to access these optional behaviors. For example, if you wanted to call an API that required a NIL string be passed you would call the function as:
Copyright © 1998-2011, Brook Miles. All rights reserved. Adapted for Modula-2 by Frank Schoonjans, with permission. Description of WINX module from Stony Brook documentation.