Types of Windows Resources-Windows Programming-Lecture Notes, Study notes of Windows Programming

This lecture handout is for Windows Programming course. It was provided by Prof. Jaimini Chinmay at Ambedkar University, Delhi. It includes: Type, window, Resources, Controls, Statments, Description, Cursor, Menu, Accelerator

Typology: Study notes

2011/2012

Uploaded on 08/07/2012

anishay
anishay 🇮🇳

4.2

(25)

118 documents

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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 application-
defined 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.)
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Types of Windows Resources-Windows Programming-Lecture Notes and more Study notes Windows Programming in PDF only on Docsity!

R esource 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 application- defined 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.)

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.

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)

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. /d Defines a symbol for the preprocessor that you can test with the #ifdef directive. /fo resname Uses resname for the name of the .RES file. /h Displays a list of RC command-line options. /i Searches the specified directory before searching the directories specified by the INCLUDE environment variable. /l codepage 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

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 TCHAR s. This refers to bytes for versions of the function or WCHAR s for Unicode versions. The string is truncated and null terminated if it is longer than the number of characters specified.

Return Value: If the function succeeds, the return value is the number of TCHAR s 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.

  1. Call the LoadAccelerators function at run time to load the accelerator table and to retrieve the handle of the accelerator table.
  2. Pass a handle to the accelerator table to the TranslateAccelerator function to activate the accelerator table.

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:

  • The window is disabled.
  • The accelerator key combination does not correspond to an item on the window menu 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.

Exercises

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