How do I? | - by Eric Slaats |
Hi and welcome to the next lesson on PM programming. In this column simple Presentation Manager programming problems and philosophies will be discussed. This Column is aimed at people who are interested in PM programming or are simply curious what makes PM programs tick. To understand this column a little programming experience (preferably in C++) is recommended.
Last month we looked at some messages that play an important role in window creation, the WM_CREATE and WM_SIZE messages. In this issue we will focus on something completely different. Since we're still in the process of creating a basic application, we will once again focus on the frame window.
In this issue we will discuss a way to add some basic controls like a menu to the frame window. The basic program we were working on last month will be used as starting point for these new explorations.
If you're wondering what I'm talking about, let me give a simple example. Last month we did a small program that set up one button in a frame window. We did this by simply creating an instance of the button class. The only thing we did ourselves was dictate (or calculate) the size of the button. We could have gone the difficult way and drawn the complete button ourselves; this way we would end up in a mess of graphic functions. The button class that is defined in OS/2 does all that for us.
Besides being lazy, I'm also a fan of the KISS principle (Keep It Simple Stupid). This month we will introduce resources as a possible way to keep it simple and be lazy.
But first let's describe what resources are.
In a resource file (.RC) we can describe controls with a scripting kind of language. This isn't exactly a programming language, but more a language that is used to describe the way things look. Some examples of resources that can be defined in resource files are:
Unfortunately, for the menu resource there usually isn't a graphical building tool. So let's see how a menu is created. The best way to illustrate this is to present an example:
MENU MAIN BEGIN SUBMENU "Menu ~1", IDM_MENU1 BEGIN MENUITEM "Item 1.~1", IDM_ITEM1_1 MENUITEM "Item 1.~2", IDM_ITEM1_2 END SUBMENU "Menu ~2", IDM_MENU2 BEGIN MENUITEM "Item 2.~1", IDM_ITEM2_1 MENUITEM SEPARATOR MENUITEM "Item 2.~2", IDM_ITEM2_2 MENUITEM "Item 2.~3", IDM_ITEM2_3 END MENUITEM "~About", IDM_ABOUT ENDThis is the sample for the menu we will add to the program of last month. (You can find an exact match of this script in the .RC file in this month's example). Let's try to analyze it.
If you take a look at the script, you should be able to get an impression of how the menu will look. This script describes a main menu with three entries named "Menu 1", "Menu 2" and "About". If "Menu 1" or "Menu 2" is activated it will display a submenu, the third menu, "About", is designed to trigger an event directly.
Some Definitions:
#define MAIN 1 #define IDM_MENU1 100 #define IDM_ITEM1_1 101 #define IDM_ITEM1_2 102 #define IDM_MENU2 200 #define IDM_ITEM2_1 201 #define IDM_ITEM2_2 203 #define IDM_ITEM2_3 204 #define IDM_ABOUT 300As you can see, all the menu-item identifiers have a different constant assigned to them. You're relatively free to choose these numbers. Menu items in the same menu must have different numbers. Or, if you want two menu items to trigger the same event, menu items should have the same ID. The first file in which we include this header file is the resource file (.RC). This way the resource compiler knows which ID's to attach to the separate items. We also want to use this include file in the file in which we handle the events triggered by a menu action. To accomplish this, the following line is added to simple3.rc and simple3.cpp.
#include "simple3.h"Why does the .cpp file need to know these ID's? Well, every time a menu item is activated, a WM_COMMAND message is sent to the client window. This means we can let the window procedure we defined for our little program intercept this message. All we have to do in the code that handles this message is determine which menu-item is pressed. (The resource identifier is used for this.) To understand how this works, let's take a look at the WM_COMMAND message.
param1 USHORT uscmd // Command value. param2 USHORT ussource // Source type. USHORT uspointer // Pointer-device indicator. returns ULONG ulReserved // Reserved value, should be 0.The only thing we need to know now is how to detect the right menu-ID. For this we only need the message parameter 1. Message parameter 2 can be used to pass information about the source that triggered the menu event. For now, we're not interested in this. (Once again, this will be addressed in a future article, so keep reading.)
You've noticed that the first message parameter is a USHORT. This means we can't simply treat it as a ULONG (which every message parameter is). We need to extract the SHORT from this ULONG. For this, there is a nice macro called SHORT1FROMMP. Take a look at the following piece of code:
case WM_COMMAND: { switch (SHORT1FROMMP(mpParm1)) { case IDM_ITEM1_1: { // Handling menu item 1.1 } break; case IDM_ITEM1_2: { // Handling menu item 1.2 } break; } } break;What you do in the separate menu item routines is dependant on your application. In our example, we will use the WinMessageBox API to make something happen when a menu item is clicked. (This API displays a popup box in which text can be displayed. See the sample code for this.)
After this is done, we need to tell the frame window upon creation which resource to load. We gave the menu the resource MAIN (1). In this code sample this is added (in previous samples this was set to 0).
WinCreateStdWindow (HWND_DESKTOP, // Parent WS_VISIBLE, // Style (visible) &flFrameFlags, // Creation flags "SampleClass", // Class name "Simple 2 for OS/2 e-Zine!",// Titlebar text 0, // Client style NULLHANDLE, // Resource handle MAIN, // Frame ID <======= This is it &hwndClient); // Client handleIn future articles we'll see that all the controls that a frame window has to load that are controlled by the FCF-flags must have the same ID and the Frame ID.
All changes that are new in the SIMPLE3.CPP (ZIP, 16.7k) program are marked with NEW to make it easier to figure out.
[Our Sponsor: MR/2 ICE Internet Email Client - Delivering the Email features of the future, today.]
Copyright © 1996 - Falcon Networking
This page is maintained by Falcon Networking. We welcome your suggestions.