<Previous Lesson

Visual Programming

Next Lesson>

Lesson#17

Resources

17.1 TYPES OF WINDOWS RESOURCES 2
17.2 RESOURCE DEFINITION STATEMENTS 2
Resources 2
Controls 3
Statements 4
17.3 .RC FILES (RESOURCE FILES) 5
17.4 RESOURCE STATEMENTS IN RESOURCE FILE 5
17.5 USING RESOURCE COMPILER (RC) 5
Options 6
17.6 LOADING AN ICON FROM THE RESOURCE TABLE 7
17.7 STRING TABLE IN A RESOURCE FILE 8
17.8 LOADING STRING 8
17.9 KEYBOARD ACCELERATOR 9
17.10 DEFINING AN ACCELERATOR 10
17.11 LOADING ACCELERATOR RESOURCE 10
17.12 TRANSLATE ACCELERATOR 11
17.13 TRANSLATE ACCELERATOR AT WORK 12
17.14 HANDLING ACCELERATOR KEYS 13
17.14.1 WINDOWS PROCEDURE 13
SUMMARY 13
EXERCISES 14
Resources 2
Resource is binary data that you can add to the executable file of a Windows-based
application. A resource can be either standard or defined. The data in a standard resource
describes an icon, cursor, menu, dialog box, bitmap, enhanced metafile, font, accelerator
table, message-table entry, string-table entry, or version information. An applicationdefined
resource, also called a custom resource, contains any data required by a specific
application.

17.1 Types of windows resources

Following are the Windows Resources are used in windows.
Accelerator
String Table
Icon
Bitmap
Dialog
Menu
Cursor
Version

17.2 Resource Definition Statements

The resource-definition statements define the resources that the resource compiler puts in
the resource (.Res) file. After the .Res file is linked to the executable file, the application
can load its resources at run time as needed. All resource statements associate an
identifying name or number with a given resource.
The resource-definition statements can be divided into the following categories:
Resources
Controls
Statements
The following tables describe the resource-definition statements.

Resources

Resource Description

ACCELERATORS
Defines menu accelerator keys.

BITMAP

Defines a bitmap by naming it and specifying the name of the file
that contains it. (To use a particular bitmap, the application requests
it by name.)

CURSOR

Defines a cursor or animated cursor by naming it and specifying the
name of the file that contains it. (To use a particular cursor, the
application requests it by name.)
Resources 3
DIALOG Defines a template that an application can use to create dialog
boxes.
DIALOGEX Defines a template that an application can use to create dialog
boxes.
FONT Specifies the name of a file that contains a font.

ICON

Defines an icon or animated icon by naming it and specifying the
name of the file that contains it. (To use a particular icon, the
application requests it by name.)
MENU Defines the appearance and function of a menu.
MENUEX Defines the appearance and function of a menu.

MESSAGETABLE

Defines a message table by naming it and specifying the name of
the file that contains it. The file is a binary resource file generated
by the message compiler.
POPUP Defines a menu item that can contain menu items and submenus.
RCDATA Defines data resources. Data resources let you include binary data in
the executable file.
STRINGTABLE Defines string resources. String resources are Unicode or ASCII
strings that can be loaded from the executable file.
User-Defined Defines a resource that contains application-specific data.
VERSIONINFO Defines a version-information resource. Contains information such
as the version number, intended operating system, and so on.
Controls
Control Description
AUTO3STATE
Creates an automatic three-state check box control.
AUTOCHECKBOX Creates an automatic check box control.
AUTORADIOBUTTON Creates an automatic radio button control.
CHECKBOX Creates a check box control.
COMBOBOX Creates a combo box control.
CONTROL Creates an application-defined control.
CTEXT Creates a centered-text control.
DEFPUSHBUTTON Creates a default pushbutton control.
EDITTEXT Creates an edit control.
GROUPBOX Creates a group box control.
ICON Creates an icon control. This control is an icon displayed in a
dialog box.
LISTBOX Creates a list box control.
LTEXT Creates a left-aligned text control.
Resources 4
PUSHBOX Creates a push box control.
PUSHBUTTON Creates a push button control.
RADIOBUTTON Creates a radio button control.
RTEXT Creates a right-aligned control.
SCROLLBAR Creates a scroll bar control.
STATE3 Creates a three-state check box control.
Statements
Statement Description
CAPTION
Sets the title for a dialog box.
CHARACTERISTICS Specifies information about a resource that can be used by tool
that can read or write resource-definition files.
CLASS Sets the class of the dialog box.
EXSTYLE Sets the extended window style of the dialog box.
FONT Sets the font with which the system will draw text for the dialog
box.

LANGUAGE

Sets the language for all resources up to the next LANGUAGE statement or to the end of the file. When the LANGUAGE statement appears before the beginning of the body of an
ACCELERATORS, DIALOG, MENU, RCDATA, or
STRINGTABLE
resource definition, the specified language
applies only to that resource.
MENU Sets the menu for the dialog box.
MENUITEM Defines a menu item.
STYLE Sets the window style for the dialog box.
VERSION Specifies version information for a resource that can be used by
tool that can read or write resource-definition files.
Resources 5
17.3 .rc files (resource files)
Figure 1
17.4 Resource Statements in Resource File
ICON resource statement in a resource file (.rc)
#define IDI_ICON 101
IDI_ICON ICON DISCARDABLE “vu.ico”
Integer id reserved icon
word filename
101 ICON DISCARDABLE “vu.ico”

17.5 Using Resource Compiler (RC)

To start RC, use the RC command.
RC [[options]] script-file
.rc File (text file containing resource
statements
Link with other files to make final EXE
(using linker) File in windows.
Compile to .res file (using resource
compiler)
Resources 6
The script-file parameter specifies the name of the resource-definition file that contains
the names, types, filenames, and descriptions of the resources to be compiled. The
options parameter can be one or more of the following command-line options.
Options
/?Displays a list of RC command-line options.
/dDefines a symbol for the preprocessor that you can test with the #ifdef directive.
/foresname
Uses resname for the name of the .RES file.
/hDisplays a list of RC command-line options.
/iSearches the specified directory before searching the directories specified by the
INCLUDE environment variable.
/lcodepage
Specifies default language for compilation. For example, -l409 is equivalent to
including the following statement at the top of the resource script file:
LANGUAGE LANG_ENGLISH,SUBLANG_ENGLISH_US
For more information, see Language Identifiers.
Alternatively, you use #pragma code_page(409) in the .RC file.
/n Null terminates all strings in the string table.
/r Ignored. Provided for compatibility with existing makefiles.
/u Undefines a symbol for the preprocessor.
/v Displays messages that report on the progress of the compiler.
/x Prevents RC from checking the INCLUDE environment variable when searching
for header files or resource files.
Options are not case sensitive and a hyphen (-) can be used in place of a slash mark (/).
You can combine single-letter options if they do not require any additional parameters.
For example, the following two commands are equivalent:
rc /V /X SAMPLE.RC
rc -vx sample.rc
Resources 7

17.6 Loading an Icon from the resource table

The LoadIcon function loads the specified icon resource from the executable (.exe) file
associated with an application instance.
HICON LoadIcon(
HINSTANCE hInstance, /*handle to the instance*/
LPCTSTR lpIconName /*string to the icon data*/
);
hInstance: Handle to an instance of the module whose executable file contains the icon to
be loaded. This parameter must be NULL when a standard icon is being loaded.
lpIconName:
Pointer to a null-terminated string that contains the name of the icon resource to be
loaded. Alternatively, this parameter can contain the resource identifier in the low-order
word and zero in the high-order word. Use the MAKEINTRESOURCE macro to create
this value.
To use one of the predefined icons, set the hInstance parameter to NULL and the
lpIconName parameter to one of the following values.
IDI_APPLICATION: Default application icon.
IDI_ASTERISK: Same as IDI_INFORMATION.
IDI_ERROR: Hand-shaped icon.
IDI_EXCLAMATION: Same as IDI_WARNING.
IDI_HAND: Same as IDI_ERROR.
IDI_INFORMATION: Asterisk icon.
IDI_QUESTION: Question mark icon.
IDI_WARNING: Exclamation point icon.
IDI_WINLOGO: Windows logo icon.
Return Value:
If the function succeeds, the return value is a handle to the newly loaded icon.
If the function fails, the return value is NULL. To get extended error information,
use GetLastError.
LoadIcon loads the icon resource only if it has not been loaded; otherwise, it retrieves a
handle to the existing resource. The function searches the icon resource for the icon most
appropriate for the current display. The icon resource can be a color or monochrome
bitmap.
Resources 8
LoadIcon can only load an icon whose size conforms to the SM_CXICON and
SM_CYICON system metric values. Use the LoadImage function to load icons of other
sizes.

17.7 String table in a resource file

#include “resource.h”
STRINGTABLE DISCARDABLE
BEGIN
IDS_STRING1 “This is Virtual University"
IDS_STRING2 "MyWindowClass"
IDS_STRING3 “My Novel Programme"
END

17.8 Loading String

The LoadString function loads a string resource from the executable file associated with
a specified module, copies the string into a buffer, and appends a terminating null
character.
int LoadString(
HINSTANCE hInstance,//handle to application instance*/
UINT uID, /*//id of the string*/
LPTSTR lpBuffer, /*buffer to receive string data*/
int nBufferMax /*maximum buffer size is available
for the string data to store*/
);
hInstance: Handle to an instance of the module whose executable file contains the string
resource. To get the handle for the application itself, use GetModuleHandle(NULL).
uID: Specifies the integer identifier of the string to be loaded.
lpBuffer: Pointer to the buffer to receive the string.
nBufferMax: Specifies the size of the buffer, in TCHARs. This refers to bytes for
versions of the function or WCHARs for Unicode versions. The string is truncated and
null terminated if it is longer than the number of characters specified.
Resources 9
Return Value:If the function succeeds, the return value is the number of TCHARs copied
into the buffer, not including the null-terminating character, or zero if the string resource
does not exist. To get extended error information, call GetLastError.

17.9 Keyboard Accelerator

A keyboard accelerator, also known as a shortcut key, is a keystroke or combination of
keystrokes that generates a WM_COMMAND message. Keyboard accelerators are often
used as shortcuts for commonly used menu commands, but you can also use them to
generate commands that have no equivalent menu items. Include keyboard accelerators
for any common or frequent actions, and provide support for the common shortcut keys
where they apply.
You can use an ASCII character code or a virtual-key code to define the accelerator. A
virtual key is a device-independent value that identifies the purpose of a keystroke as
interpreted by the Windows keyboard device driver. An ASCII character code makes the
accelerator case-sensitive. The ASCII "C" character can define the accelerator as ALT+c
rather than ALT+C. Because accelerators do not need to be case-sensitive, most
applications use virtual-key codes for accelerators rather than ASCII character codes.

To create an accelerator table

1. Use a resource compiler to define an accelerator table resource and add it to your
executable file.
An accelerator table consists of an array of ACCEL data structures, each of
which defines an individual accelerator.
2. Call the LoadAccelerators function at run time to load the accelerator table and
to retrieve the handle of the accelerator table.
3. Pass a handle to the accelerator table to the TranslateAccelerator function to
activate the accelerator table.
Resources 10

17.10 Defining an Accelerator

#define ID_DO_BACK 1001
#define ID_ACC2 1002
#define ID_DRAWSTRING 1003
ACCELERATOR ACCELERATORS DISCARDABLE
BEGIN
VK_BACK, ID_DO_BACK, VIRTKEY, ALT, NOINVERT
VK_DELETE, ID_ACC2, VIRTKEY, ALT, NOINVERT … …… .. .. .
. .. . . .. “^S", ID_DRAWSTRING, ASCII, NOINVERT
END
Labelling: Virtual Key or ASCII ID
Options(VIRTKEY, ASCII, ALT, CONTROL)

17.11 Loading Accelerator Resource

The LoadAccelerators function loads the specified accelerator table.
HACCEL LoadAccelerators(
HINSTANCE hInstance, /*//handle to the application
instance*/
LPCTSTR lpTableName /*string to the table name*/
);
hInstance: Handle to the module whose executable file contains the accelerator table to
load.
lpTableName: Pointer to a null-terminated string that contains the name of the accelerator
table to load. Alternatively, this parameter can specify the resource identifier of an
accelerator-table resource in the low-order word and zero in the high-order word. To
create this value, use the MAKEINTRESOURCE macro.
Return Value: If the function succeeds, the return value is a handle to the loaded
accelerator table.
If the function fails, the return value is NULL. To get extended error information, call
GetLastError.
Resources 11

17.12 Translate Accelerator

The TranslateAccelerator function processes accelerator keys for menu commands. The
function translates a WM_KEYDOWN or WM_SYSKEYDOWN message to a
WM_COMMAND or WM_SYSCOMMAND message (if there is an entry for the key in
the specified accelerator table) and then sends the WM_COMMAND or
WM_SYSCOMMAND message directly to the appropriate window procedure.
TranslateAccelerator does not return until the window procedure has processed the
message.
int TranslateAccelerator(
HWND hWnd, /*handle to the window to whom
accelerator attached*/
HACCEL hAccTable, /*accelerate table*/
LPMSG lpMsg /*MSG structure*/
);
hWnd: Handle to the window whose messages are to be translated.
hAccTable: Handle to the accelerator table. The accelerator table must have been loaded
by a call to the LoadAccelerators function or created by a call to the
CreateAcceleratorTable function.
lpMsg: Pointer to an MSG structure that contains message information retrieved from the
calling thread's message queue using the GetMessage or PeekMessage function.
Return Value: If the function succeeds, the return value is nonzero.If the function fails,
the return value is zero.
To differentiate the message that this function sends from messages sent by menus or
controls, the high-order word of the wParam parameter of the WM_COMMAND or
WM_SYSCOMMAND message contains the value 1.
Accelerator key combinations used to select items from the window menu are translated
into WM_SYSCOMMAND messages; all other accelerator key combinations are
translated into WM_COMMAND messages.
An accelerator need not correspond to a menu command.
If the accelerator command corresponds to a menu item, the application is sent
WM_INITMENU and WM_INITMENUPOPUP messages, as if the user were trying to
display the menu. However, these messages are not sent if any of the following
conditions exist:
Resources 12
The window is disabled.
The accelerator key combination does not correspond to an item on the windowmenu and the window is minimized.
A mouse capture is in effect. For information about mouse capture, see the
SetCapture function.
If the specified window is the active window and no window has the keyboard focus
(which is generally the case if the window is minimized), TranslateAccelerator

translates WM_SYSKEYUP and WM_SYSKEYDOWN messages instead of
WM_KEYUP and WM_KEYDOWN messages.
If an accelerator keystroke occurs that corresponds to a menu item when the window that
owns the menu is minimized, TranslateAccelerator does not send a WM_COMMAND

message. However, if an accelerator keystroke occurs that does not match any of the
items in the window’s menu or in the window menu, the function sends a

WM_COMMAND message, even if the window is minimized.

17.13 Translate Accelerator at Work

VK_BACK, ID_DO_BACK, VIRTKEY, ALT, NOINVERT

Figure 2

Alt + Backspace is pressed
WM_COMMAND message
With
wParam=low-word: ID_DO_BACK
Translate Accelerator sends a
WM_COMMAND message.
Resources
13

17.14 Handling Accelerator Keys

HACCEL hAccel;
//Load the accelerator table
hAccel = LoadAccelerator(hInstance, MAKEINTRESOURCE(ACCELERATOR))
While(GetMessage(&msg, .. .,. . . , .. . ))
{
//Call translateAccelerator to test if accelerator is pressed
If( !TranslateAccelerator(msg.hwnd, hAccel, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

17.14.1 Windows Procedure

case WM_COMMAND:
if(LOWORD(wParam) == ID_DO_BACK)
{
// accelerator is pressed
}

Summary

In this lecture, we have been studying about resources. Resources are also very
much important subject in Windows executable files. Resources are separately compiled
using resource compiler. Resource compiler (RC) compiles them to binary resource and
these binary resource are then become the part of final executable file. Resource files are
simply text script files. Resource can be loaded from any DLL and EXE module. For
loading the resource, we have useful resource functions like LoadString that loads a
string from resource table and Load Icon etc. that loads an Icon data from resource data.
Resources 14

Exercises

Practise to design your own resource including menus, bitmaps, dialogs, etc in
Visual Studio Resource Developer.

<Previous Lesson

Visual Programming

Next Lesson>

Home

Lesson Plan

Topics

Go to Top

Next Lesson
Previous Lesson
Lesson Plan
Topics
Home
Go to Top