Tutorial 3. Using Resources
Last updated: 5 January 2000
by Dolphinz

SUMMARY

This is my third tutorial on win32 programming in assembly languaje, and this time, I'll cover about how to use resource script files (.RC). The complete source code and program for this tutorial can be downloaded from here : Tutorial 3


THEORY

What's a Resource Script File (.RC)?
A resource script file, describes the appearance of the program in the screen, is used to define the menus, icons, cursors, dialog boxes, and all resources, that will be used in the program.
These resource scripts can be produced by normal text programs, like Notepad, or you can use special programs to generate it.

What's a Definition File (.DEF)?
A definition file defines the program name, segments used, memory requeriments of the program, and also the exported functions of the program. Remember that, all functions in your program that will be called by another program must be declared as exported.

THE CODE

First, let's see a resource script file example:


#define IDM_OPEN      100
#define IDM_EXIT      110

GenericMenu MENU PRELOAD MOVEABLE DISCARDABLE
  {
    POPUP    "File"
    {
      MENUITEM "Open"        , IDM_OPEN
      MENUITEM SEPARATOR
      MENUITEM "Exit"        , IDM_EXIT
    }
  }


When we select a menu-item, windows sends a WM_COMMAND message only for the menu-items activated, which is a 32 bit value, and must be interpreted by the program.

The format of MENUITEM is:
            MENUITEM "&Text", id [,options]

The options for MENUITEM are divided into the following styles:
  • CHECKED : There's a mark on the left of the text.
  • GRAYED : The menu-item is desactivated, and doesn't generate WM_COMMAND message.
  • INACTIVE : The menu-item is desactivated, and doesn't generate WM_COMMAND message.
And the format of the instruction POPUP is the following:
        POPUP   "Text" [,options]
          {
            (Menu list)
          }

Instead of keys "{ }", you can use BEGIN and END instructions. The "&" character, make that the next character appear underlined.

Now let's look to a Definition File (.DEF) example:
NAME         MENU
DESCRIPTION 'Tutorial 3 - Using Resources'
CODE         PRELOAD MOVEABLE DISCARDABLE
DATA         PRELOAD MOVEABLE MULTIPLE
EXETYPE      WINDOWS
HEAPSIZE     8192
STACKSIZE    8192
EXPORTS      WndProc

WndProc( ) is the callback function. This is where Windows sends messages to be processed. An application can have multiple callback functions for each window, dialog box...
Now let me explain the specifications for the data segment:

  PRELOAD     means that it loads when the program is first loaded.
  MOVEABLE   means that can be moved by Windows.
  MULTIPLE    means that every instance will have a own copy of the data segment.


WINDOWS MESSAGES

The first message, that will receive our window is a WM_CREATE message, in response to CreateWindowExA( ) function. When you receive this message, you can make the initializations on your program. In my simple program, I'll not make anything with this message.

Another message, WM_PAINT message, is sent when anything has happened to the window, which means that client area must be redrawn. This requires a little code that have to be included in the program, using the functions BeginPaint( ) and EndPaint( ).

A WM_COMMAND message, is sent by Windows when a menu-item is selected, with the identifier of the menu-item in wParam, and 0 in the low-half of lParam.

The WM_DESTROY message, is a message sent when your window, meaning that the window must be closed. This message is received when your window is destroyed, so you have to return to windows. In response to this message we should make a call to PostQuitMessage( ) function.
Another message is WM_CHAR, which is sent to the program when the user has pressed a keyboard key.

I think that a good way to know what messages mean and where they come, is making win32asm programs, so coding it's the best way to learn win32asm programming.
Well, that's all for now until next tutorial. If you want to ask me something, comments, or anything, you know where to find me.



Copyright © 1998, Dolphinz