This shows you the differences between two versions of the page.
allocating_20item_20ids_20automatically [2018/03/31 13:19] 127.0.0.1 external edit |
allocating_20item_20ids_20automatically [2018/04/15 17:13] richardrussell Added syntax highlighting |
||
---|---|---|---|
Line 1: | Line 1: | ||
=====Allocating item IDs automatically===== | =====Allocating item IDs automatically===== | ||
- | //by Michael Hutton, June 2010; amended by Richard Russell//\\ \\ Whenever menu items are created (and this also applies to controls within a dialog box for example) you must assign each of them a unique ID. For example:\\ | + | //by Michael Hutton, June 2010; amended by Richard Russell//\\ \\ Whenever menu items are created (and this also applies to controls within a dialog box for example) you must assign each of them a unique ID. For example: |
+ | |||
+ | <code bb4w> | ||
SYS "AppendMenu", hmenu%, 0, 100, "Menu Item&1" | SYS "AppendMenu", hmenu%, 0, 100, "Menu Item&1" | ||
SYS "AppendMenu", hmenu%, 0, 101, "Menu Item&2" | SYS "AppendMenu", hmenu%, 0, 101, "Menu Item&2" | ||
- | Later in your program you will probably need to know the ID numbers of the menu items, for instance in a routine that responds to WM_COMMAND messages. It can be tedious and confusing to use a numeric value in this situation, and if you want to expand the menu things can get worse when the numbers are not in a logical order.\\ \\ The traditional solution to this is to use named constants rather than numeric values, for example:\\ | + | </code> |
+ | |||
+ | Later in your program you will probably need to know the ID numbers of the menu items, for instance in a routine that responds to WM_COMMAND messages. It can be tedious and confusing to use a numeric value in this situation, and if you want to expand the menu things can get worse when the numbers are not in a logical order.\\ \\ The traditional solution to this is to use named constants rather than numeric values, for example: | ||
+ | |||
+ | <code bb4w> | ||
MENUITEM1 = 100 | MENUITEM1 = 100 | ||
MENUITEM2 = 101 | MENUITEM2 = 101 | ||
Line 10: | Line 16: | ||
SYS "AppendMenu", hmenu%, 0, MENUITEM1, "Menu Item&1" | SYS "AppendMenu", hmenu%, 0, MENUITEM1, "Menu Item&1" | ||
SYS "AppendMenu", hmenu%, 0, MENUITEM2, "Menu Item&2" | SYS "AppendMenu", hmenu%, 0, MENUITEM2, "Menu Item&2" | ||
- | So long as the names of the constants are related to the functions of the menu items they will be easily remembered, and you can add extra items in any position without concern for the numeric sequence.\\ \\ However if you have a large menu with many items, or a dialogue box with many controls, it may be helpful to automate the process of allocating ID values to each item/control. One way in which this may be done is to create a structure to store all the IDs for you and then use a 'trick' to automatically assign values to the structure elements.\\ \\ To do this first create a structure, we will call it MENUID{}, although you can call it anything you want. It is important to make sure that every member is an integer (% suffix):\\ | + | </code> |
+ | |||
+ | So long as the names of the constants are related to the functions of the menu items they will be easily remembered, and you can add extra items in any position without concern for the numeric sequence.\\ \\ However if you have a large menu with many items, or a dialogue box with many controls, it may be helpful to automate the process of allocating ID values to each item/control. One way in which this may be done is to create a structure to store all the IDs for you and then use a 'trick' to automatically assign values to the structure elements.\\ \\ To do this first create a structure, we will call it MENUID{}, although you can call it anything you want. It is important to make sure that every member is an integer (% suffix): | ||
+ | |||
+ | <code bb4w> | ||
DIM MENUID{ \ | DIM MENUID{ \ | ||
\ First{ \ | \ First{ \ | ||
Line 24: | Line 34: | ||
\ SecondItem%, \ | \ SecondItem%, \ | ||
\ ThirdItem% } } | \ ThirdItem% } } | ||
- | Now, we use a few lines of code to iterate through the structure and assign a unique value to each member, in this case starting at 100:\\ | + | </code> |
+ | |||
+ | Now, we use a few lines of code to iterate through the structure and assign a unique value to each member, in this case starting at 100: | ||
+ | |||
+ | <code bb4w> | ||
FOR I% = 0 TO DIM(MENUID{})-4 STEP 4 | FOR I% = 0 TO DIM(MENUID{})-4 STEP 4 | ||
!(MENUID{}+I%) = 100 + I%/4 | !(MENUID{}+I%) = 100 + I%/4 | ||
NEXT | NEXT | ||
- | Now we create our 'popup' menus and the main menu..\\ | + | </code> |
+ | |||
+ | Now we create our 'popup' menus and the main menu.. | ||
+ | |||
+ | <code bb4w> | ||
MF_POPUP = &10 | MF_POPUP = &10 | ||
WM_COMMAND = &111 | WM_COMMAND = &111 | ||
Line 54: | Line 72: | ||
SYS "DrawMenuBar", @hwnd% | SYS "DrawMenuBar", @hwnd% | ||
VDU 26 | VDU 26 | ||
- | And next we add our ON SYS handler and our main program loop:\\ | + | </code> |
+ | |||
+ | And next we add our ON SYS handler and our main program loop: | ||
+ | |||
+ | <code bb4w> | ||
DIM Click%(2), click%(2) | DIM Click%(2), click%(2) | ||
ON SYS Click%() = @msg%, @wparam%, @lparam% : RETURN | ON SYS Click%() = @msg%, @wparam%, @lparam% : RETURN | ||
Line 92: | Line 114: | ||
WAIT 1 | WAIT 1 | ||
UNTIL FALSE | UNTIL FALSE | ||
- | You can now easily add menus or menu items by just adding to the MENUID structure. You could also add item IDs of any Dialog boxes you want. Here is an example program you can copy and paste into a BB4W IDE:\\ \\ | + | </code> |
+ | |||
+ | You can now easily add menus or menu items by just adding to the MENUID structure. You could also add item IDs of any Dialog boxes you want. Here is an example program you can copy and paste into a BB4W IDE: | ||
+ | |||
+ | <code bb4w> | ||
REM Install our libraries | REM Install our libraries | ||
INSTALL @lib$+"WINLIB2" | INSTALL @lib$+"WINLIB2" | ||
Line 218: | Line 244: | ||
END | END | ||
+ | </code> |