SAMPLE: MENUITEMTEMPLATE Structure Is Documented IncorrectlyLast reviewed: February 15, 1996Article ID: Q66247 |
The information in this article applies to:
SUMMARYThe MENUITEMTEMPLATE structure is found in WINDOWS.H, which declares the structure as follows:
typedef struct { WORD mtOption; WORD mtID; char mtString[1]; } MENUITEMTEMPLATE;Single-item arrays, such as mtString, provide a named field to use to access memory. The actual text of the string is stored in the structure, not a pointer to text stored elsewhere.
MORE INFORMATIONMENUTEMP is a sample program in the Microsoft Software Library that demonstrates using the MENUITEMTEMPLATE structure and the LoadMenuIndirect() function. Download MENUTEMP.EXE, a self-extracting file, from the Microsoft Software Library (MSL) on the following services:
MENUITEMTEMPLATE mit; LPSTR lpch; ... mit.mtString = lpch; ...The mtString field is a 1-byte placeholder for the array. Because a LPSTR is 4 bytes long, it cannot be assigned to a 1-byte quantity. The mtString[1] declaration in the structure serves as a placeholder for an arbitrary number of characters. An application that uses the MENUITEMTEMPLATE structure must allocate memory both for the template itself and the string that is copied into mtString. The following code sample demonstrates how an application might create a MENUITEMTEMPLATE structure for a checked menu item having an ID value of 100 and "&Menuitem" as its text:
HANDLE hMem; LPMENUITEMTEMPLATE lpmit; static char szMenuItem[] = "&Menuitem"; ... // Note that the single char in the MENUITEMTEMPLATE structure // provides space for the null terminator on the string. hMem = LocalAlloc(LMEM_MOVEABLE, sizeof(MENUITEMTEMPLATE) + lstrlen(szMenuItem)); // LocalLock function returns a near pointer; // no problem casting to a far pointer lpmit = (LPMENUITEMTEMPLATE)LocalLock(hMem); // Set the ID and the checked flag. lpmit->mtOption = MF_CHECKED; lpmit->mtID = 100; // Copy the menu item text. lstrcpy(lpmit->mtString, szMenuItem); ... // Make the following call, when a pointer is no longer needed. LocalUnlock(hMem); ... // Make the following call, when the MENUITEMTEMPLATE // is no longer needed. LocalFree(hMem); ... |
Additional reference words: 3.00 3.10 softlib docerr MENUTEMP.EXE
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |