![]() |
|
Menus and IconsExample: menu_one ![]() This is just a small section to show how to add basic menus to your window. We use the resource file created in the previous chapter. For this example you can start with the window code from simple_window and add some new code. By means of the following directive we instruct the environment to link the resource file with our program. <*/Resource:menu_one.res*> We also import the identifiers from the resource definition file: FROM resource IMPORT IDR_MYMENU,IDI_MYICON,ID_FILE_EXIT,ID_STUFF_GO; The easiest way to attach the menu and icon to your window is to specify them when you register the window class, like this: wc.lpszMenuName := MAKEINTRESOURCE(IDR_MYMENU); wc.hIcon := LoadIconId(Instance,IDI_MYICON); (* LoadIconId from WINX *)
The Now compile the program and see what happens. Your window should now have a File and Stuff menu with the respective items underneath. That is assuming your .res file was properly compiled and linked into your program (again, see compiler notes). The icon in the top left of the window and on the task bar should now display the custom icon that we specified. Example: menu_twoAn alternative to using a menu resource is to create one on the fly (or when your program runs). This is a bit more work programming wise, but adds flexibility and is sometimes necessary. You can also use icons that aren't stored as resources, you could choose to store your icon as a seperate file and load it at runtime. This would also give you the option of allowing the user to select an icon of their choice with the common dialogs discussed later, or something to that effect.
Start again from simple_window without the .h or .rc added. Now we will
handle the CONST ID_FILE_EXIT = 9001; ID_STUFF_GO = 9002; Next we add the following code into our VAR hMenu, hSubMenu : HMENU; hIcon : HICON; | WM_CREATE : hMenu := CreateMenu(); hSubMenu := CreatePopupMenu(); FUNC AppendMenu(hSubMenu, MF_STRING, ID_FILE_EXIT, "E&xit"); FUNC AppendMenu(hMenu, MF_STRING BOR MF_POPUP, CAST(UINT,hSubMenu), "&File"); hSubMenu := CreatePopupMenu(); FUNC AppendMenu(hSubMenu, MF_STRING, ID_STUFF_GO, "&Go"); FUNC AppendMenu(hMenu, MF_STRING BOR MF_POPUP, CAST(UINT,hSubMenu), "&Stuff"); FUNC SetMenu(hwnd, hMenu); hIcon := LoadImage(NIL, "menu_two.ico", IMAGE_ICON, 32, 32, LR_LOADFROMFILE); IF hIcon#NIL THEN FUNC SendMessage(hwnd, WM_SETICON, ICON_BIG, CAST(LPARAM,hIcon)); ELSE FUNC MessageBox(hwnd, "Could not load icon!", "Error", MB_OK BOR MB_ICONERROR); END;
This creates a menu almost the same as the one we had in the resource and
attaches it to our window. A menu that is assigned to a window is automatically
removed when the program terminates, so we don't need to worry about getting rid
of it later. If we did though, we could use
The code for the icons is pretty simple, we call
If each call succeeds we assign the icon handle to our window with
NOTE: that the
OK now that we have our menu, we need to make it do something. This is
pretty simple, all we need to do is handle the PROCEDURE WndProc(hwnd : HWND; msg : UINT; wParam : WPARAM; lParam : LPARAM): LRESULT [EXPORT, OSCall]; VAR hMenu, hSubMenu : HMENU; hIcon : HICON; BEGIN CASE msg OF | WM_CREATE : hMenu := CreateMenu(); hSubMenu := CreatePopupMenu(); FUNC AppendMenu(hSubMenu, MF_STRING, ID_FILE_EXIT, "E&xit"); FUNC AppendMenu(hMenu, MF_STRING BOR MF_POPUP, CAST(UINT,hSubMenu), "&File"); hSubMenu := CreatePopupMenu(); FUNC AppendMenu(hSubMenu, MF_STRING, ID_STUFF_GO, "&Go"); FUNC AppendMenu(hMenu, MF_STRING BOR MF_POPUP, CAST(UINT,hSubMenu), "&Stuff"); FUNC SetMenu(hwnd, hMenu); hIcon := LoadImage(NIL, "menu_two.ico", IMAGE_ICON, 32, 32, LR_LOADFROMFILE); IF hIcon#NIL THEN FUNC SendMessage(hwnd, WM_SETICON, ICON_BIG, CAST(LPARAM,hIcon)); ELSE FUNC MessageBox(hwnd, "Could not load icon!", "Error", MB_OK BOR MB_ICONERROR); END; | WM_CLOSE : FUNC DestroyWindow(hwnd); | WM_DESTROY : PostQuitMessage(0); | WM_COMMAND : CASE LOWORD(wParam) OF | ID_FILE_EXIT : | ID_STUFF_GO : ELSE RETURN DefWindowProc(hwnd, msg, wParam, lParam); END; ELSE RETURN DefWindowProc(hwnd, msg, wParam, lParam); END; RETURN 0; END WndProc; As you can see we've got our
We obviously want the Exit menu item to close the program. So in the FUNC PostMessage(hwnd, WM_CLOSE, 0, 0); And we want the menu command FUNC MessageBox(hwnd, "You clicked Go!", "Woo!", MB_OK); Your WM_COMMAND handler should now look like this: | WM_COMMAND : CASE LOWORD(wParam) OF | ID_FILE_EXIT : FUNC PostMessage(hwnd, WM_CLOSE, 0, 0); | ID_STUFF_GO : FUNC MessageBox(hwnd, "You clicked Go!", "Woo!", MB_OK); ELSE RETURN DefWindowProc(hwnd, msg, wParam, lParam); END; The program file iconYou may have noticed that the
Copyright © 1998-2011, Brook Miles. All rights reserved. Adapted for Modula-2 by Frank Schoonjans, with permission. |