Annotation of mstools/samples/menu/menu.c, revision 1.1.1.2

1.1       root        1: /***************************************************************************
                      2:  *                                                                        *
                      3:  *  PROGRAM    : Menu.c                                                   *
                      4:  *                                                                        *
                      5:  *  PURPOSE    : To give a demonstration of the use of popup menus, user  *
                      6:  *               defined menus and menu functions.                        *
                      7:  *                                                                        *
                      8:  *  FUNCTIONS  : WinMain()           - Calls the initialization function  *
                      9:  *                                     and enters the message loop.       *
                     10:  *                                                                        *
                     11:  *               MenuInit()          - Registers the app. window class.   *
                     12:  *                                                                        *
                     13:  *               About()             - Dialog function for the About..    *
                     14:  *                                     dialog.                            *
                     15:  *                                                                        *
                     16:  *               ShrinkBitmap()      - Shrinks a 64x64 bitmap to a size   *
                     17:  *                                     useable for a user-defined menu    *
                     18:  *                                     checkmark.                         *
                     19:  *                                                                        *
                     20:  *               HandleCreate()      - Creates a new menu and appends it  *
                     21:  *                                     to the main menu                   *
                     22:  *                                                                        *
                     23:  *               HandlePaint()       - Handles repainting the app's client*
                     24:  *                                     area                               *
                     25:  *                                                                        *
                     26:  *               HandleChangeColors()- Changes the state of the "colors"  *
                     27:  *                                     menu item.                         *
                     28:  *                                                                        *
                     29:  *               HandleDrawItem()    - Redraws the menu items in the      *
                     30:  *                                     "colors" menu                      *
                     31:  *                                                                        *
                     32:  *               HandlePopupMenu()   - handles display of the "floating"  *
                     33:  *                                     popup.                             *
                     34:  *                                                                        *
                     35:  *               MenuWndProc()       - Window function for the app.       *
                     36:  *                                                                        *
                     37:  *                                                                        *
                     38:  ***************************************************************************/
                     39: #include "windows.h"
                     40: #include "menu.h"
                     41: 
                     42: 
                     43: HANDLE  hInst;
                     44: HMENU   hBigMenu;
                     45: HBITMAP  hbmCheckOn;
                     46: HBITMAP  hbmCheckOff;
                     47: 
                     48: /****************************************************************************
                     49:  *                                                                         *
                     50:  *  FUNCTION   : WinMain(HANDLE, HANDLE, LPSTR, int)                       *
                     51:  *                                                                         *
                     52:  *  PURPOSE    : Creates the main app. window, calls an initialization     *
                     53:  *              function and enters the message loop.                      *
                     54:  *                                                                         *
                     55:  ****************************************************************************/
                     56: int APIENTRY WinMain(
                     57:     HANDLE hInstance,
                     58:     HANDLE hPrevInstance,
                     59:     LPSTR lpCmdLine,
                     60:     int nCmdShow
                     61:     )
                     62: {
                     63: 
                     64:     HWND  hWnd;
                     65:     MSG msg;                                /* message                      */
                     66: 
                     67:     UNREFERENCED_PARAMETER( lpCmdLine );
                     68: 
                     69:     /* Register main window class if this is the first instance of the app. */
                     70:     if (!hPrevInstance)
                     71:        if (!MenuInit (hInstance))
                     72:            return NULL;
                     73: 
                     74:     hInst = hInstance;
                     75: 
                     76:     /* Create the app. window */
                     77:     hWnd = CreateWindow ("menu",
                     78:                         "Menu Example",
                     79:                         WS_OVERLAPPEDWINDOW,
                     80:                         CW_USEDEFAULT,
                     81:                         CW_USEDEFAULT,
                     82:                         CW_USEDEFAULT,
                     83:                         CW_USEDEFAULT,
                     84:                         (HWND) NULL,
                     85:                         NULL,
                     86:                         hInstance,
                     87:                         (LPSTR) NULL);
                     88: 
                     89:     if (!hWnd)
                     90:        return NULL;
                     91: 
                     92:     ShowWindow (hWnd, nCmdShow);
                     93:     UpdateWindow (hWnd);
                     94: 
                     95:     while (GetMessage (&msg, NULL, NULL, NULL)){
                     96:        /* Since we have no accelerators, no need to call
                     97:         * TranslateAccelerator here.
                     98:         */
                     99:        TranslateMessage (&msg);
                    100:        DispatchMessage (&msg);
                    101:     }
                    102:     return(msg.wParam);
                    103: }
                    104: 
                    105: 
                    106: /****************************************************************************
                    107:  *                                                                         *
                    108:  *  FUNCTION   : MenuInit (hInstance)                                      *
                    109:  *                                                                         *
                    110:  *  PURPOSE    : Registers the main window class.                          *
                    111:  *                                                                         *
                    112:  *  RETURNS    : TRUE  -  if RegisterClass() went off ok                   *
                    113:  *              FALSE  -  otherwise.                                       *
                    114:  *                                                                         *
                    115:  ****************************************************************************/
                    116: BOOL NEAR PASCAL MenuInit (HANDLE hInstance)
                    117: {
                    118:     HANDLE    hMemory;
                    119:     PWNDCLASS pWndClass;
                    120:     BOOL      bSuccess;
                    121: 
                    122:     /* Initialize the menu window class */
                    123:     hMemory   = LocalAlloc(LPTR, sizeof(WNDCLASS));
                    124:     if(!hMemory){
                    125:         MessageBox(NULL, "<MenuInit> Not enough memory.", NULL, MB_OK | MB_ICONHAND);
                    126:         return(FALSE);
                    127:     }
                    128: 
                    129:     pWndClass = (PWNDCLASS) LocalLock(hMemory);
                    130: 
                    131:     pWndClass->style        = NULL;
                    132:     pWndClass->lpfnWndProc   = (WNDPROC) MenuWndProc;
                    133:     pWndClass->hInstance     = hInstance;
                    134:     pWndClass->hIcon        = LoadIcon (hInstance, "menu");
                    135:     pWndClass->hCursor      = LoadCursor (NULL, IDC_ARROW);
                    136:     pWndClass->hbrBackground = GetStockObject (WHITE_BRUSH);
                    137:     pWndClass->lpszMenuName  = (LPSTR) "MenuMenu",
                    138:     pWndClass->lpszClassName = (LPSTR) "menu";
                    139: 
                    140:     bSuccess = RegisterClass (pWndClass);
                    141:     LocalUnlock (hMemory);
                    142:     LocalFree (hMemory);
                    143: 
                    144:     return bSuccess;
                    145: }
                    146: 
                    147: 
                    148: /****************************************************************************
                    149:  *                                                                         *
                    150:  *  FUNCTION   : About (hDlg, message, wParam, lParam)                     *
                    151:  *                                                                         *
                    152:  *  PURPOSE    : Dialog function for the About menu... dialog.             *
                    153:  *                                                                         *
                    154:  ****************************************************************************/
                    155: BOOL APIENTRY About(
                    156:        HWND hDlg,
                    157:        UINT message,
                    158:        UINT wParam,
                    159:        LONG lParam)
                    160: 
                    161: {
                    162:     switch (message){
                    163:        case WM_INITDIALOG:
                    164:            return(TRUE);
                    165: 
                    166:        case WM_COMMAND:
                    167:            // LOWORD added for portability
                    168:            if (LOWORD(wParam) == IDOK){
                    169:                EndDialog(hDlg,NULL);
                    170:                return(TRUE);
                    171:            }
                    172:            break;
                    173:     }
                    174:     return(FALSE);
                    175:        UNREFERENCED_PARAMETER(lParam);
                    176: }
                    177: 
                    178: 
                    179: /****************************************************************************
                    180:  *                                                                         *
                    181:  *  FUNCTION   : ShrinkBitmap(hwnd, hbm)                                   *
                    182:  *                                                                         *
                    183:  *  PURPOSE    : This function shrinks a 64x64 bitmap into a bitmap useable *
                    184:  *              for the user-defined checkmark for menu items. This can be *
                    185:  *              easily generalized to shrink bitmaps of any size.          *
                    186:  *                                                                         *
                    187:  *  RETURNS    : HBITMAP - A handle to the new bitmap.                     *
                    188:  *                                                                         *
                    189:  ****************************************************************************/
                    190: HBITMAP APIENTRY ShrinkBitmap (
                    191:        HWND hwnd,
                    192:        HBITMAP hbm)
                    193: {
                    194:     HDC     hdc;
                    195:     HDC     hmemorydcNew;
                    196:     HDC     hmemorydcOld;
                    197:     LONG    checkMarkSize;
                    198:     HBITMAP hCheckBitmap;
                    199:     HBITMAP hOldBitmapSave;
                    200:     HBITMAP hNewBitmapSave;
                    201: 
                    202:     hdc = GetDC (hwnd);
                    203: 
                    204:     /* Create DCs for the source (old) and target (new) bitmaps */
                    205:     hmemorydcNew = CreateCompatibleDC (hdc);
                    206:     hmemorydcOld = CreateCompatibleDC (hdc);
                    207: 
                    208:     /* Determine the dimensions of the default menu checkmark and
                    209:      * create a target bitmap of the same dimensions
                    210:      */
                    211:     checkMarkSize = GetMenuCheckMarkDimensions ();
                    212:     hCheckBitmap  = CreateCompatibleBitmap (hdc,
                    213:                                            LOWORD (checkMarkSize),
                    214:                                            HIWORD (checkMarkSize));
                    215: 
                    216:     /* Select the source bitmap and the target bitmap into their
                    217:      * respective DCs.
                    218:      */
                    219:     hOldBitmapSave = SelectObject (hmemorydcNew, hCheckBitmap);
                    220:     hNewBitmapSave = SelectObject (hmemorydcOld, hbm);
                    221: 
                    222:     /* Shrink the source bitmap into the target DC */
                    223:     StretchBlt (hmemorydcNew,
                    224:                0,
                    225:                0,
                    226:                LOWORD(checkMarkSize),
                    227:                HIWORD(checkMarkSize),
                    228:                hmemorydcOld,
                    229:                0,
                    230:                0,
                    231:                64,
                    232:                64,
                    233:                SRCCOPY);
                    234: 
                    235:     /* De-select the bitmaps and clean up .. */
                    236:     SelectObject (hmemorydcNew, hOldBitmapSave);
                    237:     SelectObject (hmemorydcOld, hNewBitmapSave);
                    238:     DeleteDC (hmemorydcNew);
                    239:     DeleteDC (hmemorydcOld);
                    240:     ReleaseDC (hwnd, hdc);
                    241: 
                    242:     /* .. and return a handle to the target bitmap */
                    243:     return hCheckBitmap;
                    244: }
                    245: 
                    246: 
                    247: /****************************************************************************
                    248:  *                                                                         *
                    249:  *  FUNCTION   : HandleCreate ( hwnd )                                     *
                    250:  *                                                                         *
                    251:  *  PURPOSE    : Creates a new (empty) menu and appends to it the "State"   *
                    252:  *              menu items. It sets up the user-defined checkmarks for the *
                    253:  *              menu. It then inserts this menu into the main menu bar.    *
                    254:  *                                                                         *
                    255:  ****************************************************************************/
                    256: VOID APIENTRY HandleCreate (HWND hwnd)
                    257: {
                    258:     HMENU   hMenu;
                    259:     HMENU   hWndMenu;
                    260: 
                    261:     /* Create a new menu into the menubar on the fly */
                    262:     hMenu = CreateMenu ();
                    263:     if (!hMenu)
                    264:        return;
                    265: 
                    266:     /* Append the state menu items to it */
                    267:     AppendMenu (hMenu, MF_STRING, IDM_STATE1, "South Dakota");
                    268:     AppendMenu (hMenu, MF_STRING, IDM_STATE2, "Washington");
                    269:     AppendMenu (hMenu, MF_STRING, IDM_STATE3, "California");
                    270:     if (!AppendMenu (hMenu, MF_STRING, IDM_STATE4, "Oregon")){
                    271:        /* It is unlikely the other appends will fail and this will succeed.
                    272:         * So just check this one. And if it fails, Destroy the menu for
                    273:         * good measure and return.
                    274:         */
                    275:        DestroyMenu(hMenu);
                    276:        return;
                    277:     }
                    278:     hbmCheckOn = ShrinkBitmap (hwnd, LoadBitmap (hInst, "checkon"));
                    279:     hbmCheckOff = ShrinkBitmap (hwnd, LoadBitmap (hInst, "checkoff"));
                    280: 
                    281:     /* Set up the user-defined check marks */
                    282:     SetMenuItemBitmaps (hMenu, 0, MF_BYPOSITION, hbmCheckOff, hbmCheckOn);
                    283:     SetMenuItemBitmaps (hMenu, 1, MF_BYPOSITION, hbmCheckOff, hbmCheckOn);
                    284:     SetMenuItemBitmaps (hMenu, 2, MF_BYPOSITION, hbmCheckOff, hbmCheckOn);
                    285:     SetMenuItemBitmaps (hMenu, 3, MF_BYPOSITION, hbmCheckOff, hbmCheckOn);
                    286: 
                    287:     /* Now insert the menu into the main menu bar. */
                    288:     hWndMenu = GetMenu (hwnd);
                    289:     InsertMenu (hWndMenu, 2, MF_POPUP|MF_BYPOSITION, (DWORD)hMenu, "States");
                    290: 
                    291:     return;
                    292: }
                    293: 
                    294: /****************************************************************************
                    295:  *                                                                         *
                    296:  *  FUNCTION   : HandlePaint ( hwnd )                                      *
                    297:  *                                                                         *
                    298:  *  PURPOSE    : Handles the repainting of the main app's client area.      *
                    299:  *                                                                         *
                    300:  ****************************************************************************/
                    301: VOID APIENTRY HandlePaint (HWND hwnd)
                    302: {
                    303:     HDC        hdc;
                    304:     PAINTSTRUCT ps;
                    305:     RECT       rc;
                    306: 
                    307:     hdc = BeginPaint (hwnd, (LPPAINTSTRUCT)&ps);
                    308: 
                    309:     /* Center the text in the client area */
                    310:     GetClientRect (hwnd, (LPRECT)&rc);
                    311:     DrawText (hdc,
                    312:              "Down click in the window for a popup menu",
                    313:               41,
                    314:               (LPRECT)&rc,
                    315:               DT_CENTER | DT_WORDBREAK);
                    316:     EndPaint(hwnd, (LPPAINTSTRUCT)&ps);
                    317: }
                    318: 
                    319: 
                    320: /****************************************************************************
                    321:  *                                                                         *
                    322:  *  FUNCTION   : HandleChangeColors (hwnd)                                 *
                    323:  *                                                                         *
                    324:  *  PURPOSE    : Toggles the state of the Owner Draw item in the Colors     *
                    325:  *              menu. If it is on, the "Black", "Blue", "Red", and "Green" *
                    326:  *               individual menu text items are modified so that they will  *
                    327:  *              contain bands of color. Otherwise, the colors are replaced *
                    328:  *              by the text.                                               *
                    329:  *                                                                         *
                    330:  ****************************************************************************/
                    331: VOID APIENTRY HandleChangeColors(HWND hwnd)
                    332: {
                    333:     HMENU hMenu;
                    334:     BOOL  fOwnerDraw;
                    335: 
                    336:     /* Get a handle to the Colors menu. This is at position 1. */
                    337:     hMenu = GetSubMenu (GetMenu (hwnd), IDCOLORS_POS);
                    338: 
                    339:     /* Get the current state of the item */
                    340:     fOwnerDraw = GetMenuState ( hMenu,
                    341:                                IDM_COLOROWNERDR, MF_BYCOMMAND) & MF_CHECKED;
                    342: 
                    343:     /* Toggle the state of the item. */
                    344:     CheckMenuItem ( hMenu,
                    345:                    IDM_COLOROWNERDR,
                    346:                    MF_BYCOMMAND | (fOwnerDraw ? MF_UNCHECKED : MF_CHECKED));
                    347: 
                    348:     if (!fOwnerDraw){
                    349:        /* Change the items to owner-draw items. Pass the RGB value for the
                    350:         * color as the application-supplied data. This makes it easier for
                    351:         * us to draw the items.
                    352:         */
                    353:        ModifyMenu(hMenu,
                    354:                   IDM_BLACK,
                    355:                   MF_OWNERDRAW | MF_BYCOMMAND,
                    356:                   IDM_BLACK,
                    357:                   (LPSTR)RGB (0,0,0));
                    358: 
                    359:        ModifyMenu(hMenu,
                    360:                   IDM_BLUE,
                    361:                   MF_OWNERDRAW | MF_BYCOMMAND,
                    362:                   IDM_BLUE,
                    363:                   (LPSTR)RGB (0,0,255));
                    364: 
                    365:        ModifyMenu(hMenu,
                    366:                   IDM_RED,
                    367:                   MF_OWNERDRAW | MF_BYCOMMAND,
                    368:                   IDM_RED,
                    369:                   (LPSTR)RGB (255,0,0));
                    370: 
                    371:        ModifyMenu(hMenu,
                    372:                   IDM_GREEN,
                    373:                   MF_OWNERDRAW | MF_BYCOMMAND,
                    374:                   IDM_GREEN,
                    375:                   (LPSTR)RGB (0,255,0));
                    376:     }
                    377:     else {
                    378:        /* Change the items to normal text items. */
                    379:        ModifyMenu(hMenu, IDM_BLACK, MF_BYCOMMAND, IDM_BLACK, "Black");
                    380: 
                    381:        ModifyMenu(hMenu, IDM_BLUE, MF_BYCOMMAND, IDM_BLUE, "Blue");
                    382: 
                    383:        ModifyMenu(hMenu, IDM_RED, MF_BYCOMMAND, IDM_RED, "Red");
                    384: 
                    385:        ModifyMenu(hMenu, IDM_GREEN, MF_BYCOMMAND, IDM_GREEN, "Green");
                    386:     }
                    387: }
                    388: 
                    389: 
                    390: /****************************************************************************
                    391:  *                                                                         *
                    392:  *  FUNCTION   : HandleDrawItem ( hwnd, lpdis)                             *
                    393:  *                                                                         *
                    394:  *  PURPOSE    : Called in response to a WM_DRAWITEM message, i.e. when the *
                    395:  *              colors menu is being modified to an owner-draw menu, or    *
                    396:  *              one of the items is selected. It sizes the checkmark bitmap*
                    397:  *              to fit next to a color band and draws the color bands and  *
                    398:  *              the checkmark on the popup menu.                           *
                    399:  *                                                                         *
                    400:  ****************************************************************************/
                    401: VOID APIENTRY HandleDrawItem(
                    402:        HWND             hwnd,
                    403:        LPDRAWITEMSTRUCT lpdis)
                    404: 
                    405: {
                    406:     HDC     hdcBitmap;
                    407:     HBITMAP hbmSave;
                    408:     HBRUSH  hbr;
                    409:     RECT    rc;
                    410:     LONG    checkMarkSize;
                    411:     DWORD   textColorSave;
                    412:     DWORD   bkColorSave;
                    413: 
                    414:     /* Get the size of the checkmark so we can leave room for it since we
                    415:      * want to be able to check the selected color.
                    416:      */
                    417:     checkMarkSize = GetMenuCheckMarkDimensions ();
                    418: 
                    419:     if (lpdis->itemAction == ODA_SELECT ||
                    420:        lpdis->itemAction == ODA_DRAWENTIRE){
                    421: 
                    422:        CopyRect ((LPRECT)&rc, (LPRECT)&lpdis->rcItem);
                    423:        InflateRect ((LPRECT)&rc, (-2 - LOWORD(checkMarkSize)), -2);
                    424: 
                    425:        if (lpdis->itemState & ODS_SELECTED)
                    426:        {
                    427:            /* Item has been selected -- hilite with a gray frame */
                    428:            hbr = GetStockObject (GRAY_BRUSH);
                    429:            FrameRect (lpdis->hDC, (LPRECT)&rc, hbr);
                    430:        }
                    431:        else if (lpdis->itemAction == ODA_SELECT)
                    432:        {
                    433:            /* Item has been de-selected -- remove gray frame */
                    434:            hbr = CreateSolidBrush (GetSysColor (COLOR_MENU));
                    435:            FrameRect (lpdis->hDC, (LPRECT)&rc, hbr);
                    436:            DeleteObject (hbr);
                    437:        }
                    438:     }
                    439: 
                    440:     if (lpdis->itemAction == ODA_DRAWENTIRE){
                    441: 
                    442:        /* Paint the color item in the color requested. */
                    443:        hbr = CreateSolidBrush (lpdis->itemData);
                    444:        CopyRect ((LPRECT)&rc, (LPRECT)&lpdis->rcItem);
                    445:        InflateRect ((LPRECT)&rc, -10-LOWORD(checkMarkSize), -10);
                    446:        FillRect (lpdis->hDC, (LPRECT)&rc, hbr);
                    447:        DeleteObject (hbr);
                    448: 
                    449:        if (lpdis->itemState & ODS_CHECKED){
                    450:            /* Draw the check mark if the item is checked. */
                    451:            hdcBitmap = CreateCompatibleDC (lpdis->hDC);
                    452:            hbmSave = SelectObject (hdcBitmap, hbmCheckOn);
                    453: 
                    454:            textColorSave = SetTextColor (lpdis->hDC, 0x00000000L);
                    455:            bkColorSave   = SetBkColor (lpdis->hDC, 0x00FFFFFFL);
                    456: 
                    457:            /* Use Magic bitblt op so that monochrome bitmaps preserve
                    458:               background and foreground colors. */
                    459:            BitBlt (lpdis->hDC,
                    460:                    lpdis->rcItem.left,
                    461:                    lpdis->rcItem.top+
                    462:                           (MEASUREITEMHEIGHT - HIWORD (checkMarkSize)) / 2,
                    463:                    LOWORD (checkMarkSize),
                    464:                    HIWORD (checkMarkSize),
                    465:                    hdcBitmap,
                    466:                    0,
                    467:                    0,
                    468:                    ROP_PSDPxax);
                    469: 
                    470:            /* Restore colors and bitmap and clean up */
                    471:            SetTextColor (lpdis->hDC, textColorSave);
                    472:            SetBkColor (lpdis->hDC, bkColorSave);
                    473:            SelectObject (hdcBitmap, hbmSave);
                    474:            DeleteDC (hdcBitmap);
                    475: 
                    476:        }
                    477:     }
                    478:        UNREFERENCED_PARAMETER(hwnd);
                    479: }
                    480: 
                    481: /****************************************************************************
                    482:  *                                                                         *
                    483:  *  FUNCTION   : HandlePopupMenu (hwnd, point)                             *
                    484:  *                                                                         *
                    485:  *  PURPOSE    : Handles the display of the "floating" popup that appears   *                                                          *
                    486:  *              on a mouse click in the app's client area.                 *
                    487:  *                                                                         *
                    488:  ****************************************************************************/
                    489: VOID APIENTRY HandlePopupMenu (
                    490:        HWND   hwnd,
                    491:        POINT point)
                    492: 
                    493: {
                    494:     HMENU hMenu;
                    495:     HMENU hMenuTrackPopup;
                    496: 
                    497:     /* Get the menu for the popup from the resource file. */
                    498:     hMenu = LoadMenu (hInst, "PopupMenu");
                    499:     if (!hMenu)
                    500:        return;
                    501: 
                    502:     /* Get the first menu in it which we will use for the call to
                    503:      * TrackPopup(). This could also have been created on the fly using
                    504:      * CreatePopupMenu and then we could have used InsertMenu() or
                    505:      * AppendMenu.
                    506:      */
                    507:     hMenuTrackPopup = GetSubMenu (hMenu, 0);
                    508: 
                    509:     /* Convert the mouse point to screen coordinates since that is what
                    510:      * TrackPopup expects.
                    511:      */
                    512:     ClientToScreen (hwnd, (LPPOINT)&point);
                    513: 
                    514:     /* Draw and track the "floating" popup */
                    515:     TrackPopupMenu (hMenuTrackPopup, 0, point.x, point.y, 0, hwnd, NULL);
                    516: 
                    517:     /* Destroy the menu since were are done with it. */
                    518:     DestroyMenu (hMenu);
                    519: }
                    520: 
                    521: /****************************************************************************
                    522:  *                                                                         *
                    523:  *  FUNCTION   : MenuWndProc (hWnd, message, wParam, lParam)               *
                    524:  *                                                                         *
                    525:  *  PURPOSE    : Window function for the main app. window. Processes all the*
                    526:  *              menu selections and oter messages.                         *
                    527:  *                                                                         *
                    528:  ****************************************************************************/
                    529: LONG APIENTRY MenuWndProc (
                    530:        HWND hWnd,
                    531:        UINT message,
                    532:        UINT wParam,
                    533:        LONG lParam)
                    534: 
                    535: {
                    536:     FARPROC lpProc;
                    537:     HMENU hMenu;
                    538:     RECT rc;
                    539:     POINT pt;
                    540: 
                    541: 
                    542:     switch (message){
                    543:        case WM_SYSCOMMAND:
                    544:            /* Show the About ... dialog */
                    545:            if (wParam == ID_ABOUT){
                    546:                lpProc = MakeProcInstance ((FARPROC)About, hInst);
                    547:                DialogBox (hInst,
                    548:                           "AboutBox",
                    549:                           hWnd,
1.1.1.2 ! root      550:                           lpProc);
1.1       root      551: 
                    552:                FreeProcInstance (lpProc);
                    553:                break;
                    554:            }
                    555:            else
                    556:              return DefWindowProc (hWnd, message, wParam, lParam);
                    557: 
                    558:        case WM_COMMAND:
                    559:            // LOWORD added for portability
                    560:            switch (LOWORD(wParam)){
                    561:                 case IDM_EXIT:
                    562:                   DestroyWindow (hWnd);
                    563:                   break;
                    564: 
                    565:                 case IDM_ABOUT:
                    566:                   /* Bring up the About.. dialog box */
                    567:                   lpProc = MakeProcInstance ((FARPROC)About, hInst);
                    568:                   DialogBox (hInst,
                    569:                              "AboutBox",
                    570:                              hWnd,
1.1.1.2 ! root      571:                              lpProc);
1.1       root      572: 
                    573:                   FreeProcInstance (lpProc);
                    574:                   break;
                    575: 
                    576:                 case IDM_COLOROWNERDR:
                    577:                     /* Change colors in color menu depending on state of this
                    578:                        menu item. */
                    579:                     HandleChangeColors (hWnd);
                    580:                     break;
                    581: 
                    582:                 case IDM_STATE1:
                    583:                 case IDM_STATE2:
                    584:                 case IDM_STATE3:
                    585:                 case IDM_STATE4:
                    586:                      /* Get a handle to the states menu... */
                    587:                      hMenu = GetSubMenu (GetMenu (hWnd), IDSTATES_POS);
                    588: 
                    589:                      /* Uncheck all the items. */
                    590:                      CheckMenuItem (hMenu, IDM_STATE1,
                    591:                                     MF_BYCOMMAND | MF_UNCHECKED);
                    592:                      CheckMenuItem (hMenu, IDM_STATE2,
                    593:                                     MF_BYCOMMAND | MF_UNCHECKED);
                    594:                      CheckMenuItem (hMenu, IDM_STATE3,
                    595:                                     MF_BYCOMMAND | MF_UNCHECKED);
                    596:                      CheckMenuItem (hMenu, IDM_STATE4,
                    597:                                     MF_BYCOMMAND | MF_UNCHECKED);
                    598: 
                    599:                      /* ...and just check the selected one.*/
                    600:                      CheckMenuItem (hMenu, (WORD)wParam,
                    601:                                     MF_BYCOMMAND | MF_CHECKED);
                    602:                     break;
                    603: 
                    604:                 case IDM_BLACK:
                    605:                 case IDM_RED:
                    606:                 case IDM_BLUE:
                    607:                 case IDM_GREEN:
                    608:                      /* Get a handle to the Colors menu. */
                    609:                      hMenu = GetSubMenu (GetMenu (hWnd),IDCOLORS_POS);
                    610: 
                    611:                      /* Uncheck all the items. */
                    612:                      CheckMenuItem (hMenu, IDM_BLACK,
                    613:                                     MF_BYCOMMAND | MF_UNCHECKED);
                    614:                      CheckMenuItem (hMenu, IDM_RED,
                    615:                                     MF_BYCOMMAND | MF_UNCHECKED);
                    616:                      CheckMenuItem (hMenu, IDM_BLUE,
                    617:                                     MF_BYCOMMAND | MF_UNCHECKED);
                    618:                      CheckMenuItem (hMenu, IDM_GREEN,
                    619:                                     MF_BYCOMMAND | MF_UNCHECKED);
                    620: 
                    621:                      /* ...and just check the selected one.*/
                    622:                      CheckMenuItem (hMenu, (WORD)wParam,
                    623:                                     MF_BYCOMMAND | MF_CHECKED);
                    624:                      break;
                    625: 
                    626:                 case IDM_FONT:
                    627:                      /* Messages sent to us from TrackPopupMenu when
                    628:                       * items are selected from the "floating" popups
                    629:                       */
                    630:                      MessageBox (hWnd,
                    631:                                  "A font was selected",
                    632:                                  "Popup Menu Alert",
                    633:                                  MB_APPLMODAL|MB_OK);
                    634:                      break;
                    635: 
                    636:                 case IDM_SIZE:
                    637:                      MessageBox (hWnd,
                    638:                                  "A size was selected",
                    639:                                  "Popup Menu Alert",
                    640:                                  MB_APPLMODAL|MB_OK);
                    641:                      break;
                    642: 
                    643:                 case IDM_STYLE:
                    644:                      MessageBox (hWnd,
                    645:                                  "A style was selected",
                    646:                                  "Popup Menu Alert",
                    647:                                  MB_APPLMODAL|MB_OK);
                    648:                      break;
                    649:            }
                    650:            break;
                    651: 
                    652:        case WM_SIZE:
                    653:            if (lParam){
                    654:                /* If window is being sized to a non zero value...
                    655:                 * invalidate it's client area.
                    656:                 */
                    657:                InvalidateRect (hWnd, NULL, TRUE);
                    658:            }
                    659:            break;
                    660: 
                    661:        case WM_PAINT:
                    662:            HandlePaint (hWnd);
                    663:            break;
                    664: 
                    665:        case WM_MEASUREITEM:
                    666:            /* Use the same width for all items. We could examine the item id
                    667:               and use different widths/heights for each item. */
                    668:            ((LPMEASUREITEMSTRUCT)lParam)->itemWidth  = MEASUREITEMWIDTH;
                    669:            ((LPMEASUREITEMSTRUCT)lParam)->itemHeight = MEASUREITEMHEIGHT;
                    670:            return TRUE;
                    671: 
                    672:        case WM_DRAWITEM:
                    673:            /* Redraw the "colors" menu in normal/ownerdrawmode */
                    674:            HandleDrawItem (hWnd,(LPDRAWITEMSTRUCT)lParam);
                    675:            return TRUE;
                    676:            break;
                    677: 
                    678:        case WM_CREATE:
                    679:            /* Create the menu */
                    680:            HandleCreate (hWnd);
                    681:            break;
                    682: 
                    683:        case WM_DESTROY:
                    684:            /* Delete the on/off bitmaps so that they don't waste memory. */
                    685:            DeleteObject (hbmCheckOn);
                    686:            DeleteObject (hbmCheckOff);
                    687: 
                    688:            PostQuitMessage (0);
                    689:            break;
                    690: 
                    691:        case WM_LBUTTONDOWN:
                    692:            /* Draw the "floating" popup in the app's client area */
                    693:            GetClientRect (hWnd, (LPRECT)&rc);
                    694: 
                    695:            // Temporary porting macro
                    696:            LONG2POINT(lParam, pt);
                    697:            if (PtInRect ((LPRECT)&rc, pt))
                    698:                HandlePopupMenu (hWnd, pt);
                    699:            break;
                    700: 
                    701:        default:
                    702:            return DefWindowProc(hWnd, message, wParam, lParam);
                    703:     }
                    704:     return(NULL);
                    705: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.