Annotation of q_a/samples/owncombo/owncombo.c, revision 1.1.1.1

1.1       root        1: 
                      2: /******************************************************************************\
                      3: *       This is a part of the Microsoft Source Code Samples. 
                      4: *       Copyright (C) 1993 Microsoft Corporation.
                      5: *       All rights reserved. 
                      6: *       This source code is only intended as a supplement to 
                      7: *       Microsoft Development Tools and/or WinHelp documentation.
                      8: *       See these sources for detailed information regarding the 
                      9: *       Microsoft samples programs.
                     10: \******************************************************************************/
                     11: 
                     12: /***************************************************************************
                     13:  *                                                                         *
                     14:  *  PROGRAM     : OwnCombo.c                                               *
                     15:  *                                                                         *
                     16:  *  PURPOSE     : Illustrates the use of functions and messages for        *
                     17:  *                combo boxes and owner-draw control styles.               *
                     18:  *                                                                         *
                     19:  *  FUNCTIONS   : WinMain                - Creates the app. window and     *
                     20:  *                                         enters the message loop.        *
                     21:  *                                                                         *
                     22:  *                OwnComboInit           - Registers the main window class *
                     23:  *                                                                         *
                     24:  *                About                  - Dialog function for the About   *
                     25:  *                                         dialog.                         *
                     26:  *                                                                         *
                     27:  *                OwnComboWndProc        - Window function for app. It     *
                     28:  *                                         handles the menu selections     *
                     29:  *                                         and processes the other window  *
                     30:  *                                         messages.                       *
                     31:  *                                                                         *
                     32:  *                DrawEntireItem         - Handles the drawing of a list   *
                     33:  *                                         list box or combo box item.     *
                     34:  *                                                                         *
                     35:  *                HandleSelectionState   - Handles the selecting/deselect- *
                     36:  *                                         ing of a list box or combo box  *
                     37:  *                                         item.                           *
                     38:  *                                                                         *
                     39:  *                HandleFocusState       - Handles the getting/losing of   *
                     40:  *                                         the input focus by a list box   *
                     41:  *                                                                         *
                     42:  *                ListBoxExample         - Dialog function for the         *
                     43:  *                                         owner-draw list box example.    *
                     44:  *                                                                         *
                     45:  *                ComboBoxExample        - Dialog function for the text    *
                     46:  *                                         combo dialog.                   *
                     47:  *                                                                         *
                     48:  *                OwnerComboBoxExample   - Dialog fubction for the drop-   *
                     49:  *                                         down-list combobox with         *
                     50:  *                                         ownerdraw.                      *
                     51:  *                                                                         *
                     52:  ***************************************************************************/
                     53: #include "windows.h"
                     54: #include "owncombo.h"
                     55: 
                     56: HANDLE  hInst;
                     57: 
                     58: /****************************************************************************
                     59:  *                                                                          *
                     60:  *  FUNCTION   : WinMain(HANDLE, HANDLE, LPSTR, int)                        *
                     61:  *                                                                          *
                     62:  *  PURPOSE    : Creates the app. window and enters the message loop.       *
                     63:  *                                                                          *
                     64:  ****************************************************************************/
                     65: 
                     66: int APIENTRY WinMain(
                     67:     HANDLE hInstance,
                     68:     HANDLE hPrevInstance,
                     69:     LPSTR lpCmdLine,
                     70:     int nCmdShow
                     71:     )
                     72: {
                     73: 
                     74:     HWND  hWnd;
                     75:     MSG   msg;
                     76: 
                     77:     UNREFERENCED_PARAMETER( lpCmdLine );
                     78: 
                     79:     if (!hPrevInstance)
                     80:         if (!OwnComboInit (hInstance))
                     81:             return (0);
                     82: 
                     83:     hInst = hInstance;
                     84: 
                     85:     /* Create the app. window */
                     86:     hWnd = CreateWindow ("owncombo",
                     87:                          "Owner-draw & Combo Box Example",
                     88:                          WS_OVERLAPPEDWINDOW,
                     89:                          CW_USEDEFAULT,
                     90:                          CW_USEDEFAULT,
                     91:                          CW_USEDEFAULT,
                     92:                          CW_USEDEFAULT,
                     93:                          (HWND) NULL,
                     94:                          NULL,
                     95:                          hInstance,
                     96:                          (LPSTR) NULL);
                     97: 
                     98:     if (!hWnd)
                     99:         return (0);
                    100: 
                    101:     ShowWindow (hWnd, nCmdShow);
                    102: 
                    103:     while (GetMessage (&msg, NULL, 0, 0)){
                    104:         TranslateMessage (&msg);
                    105:         DispatchMessage (&msg);
                    106:     }
                    107: 
                    108:     return(msg.wParam);
                    109: }
                    110: 
                    111: 
                    112: /****************************************************************************
                    113:  *                                                                          *
                    114:  *  FUNCTION   : OwnComboInit (hInstance)                                   *
                    115:  *                                                                          *
                    116:  *  PURPOSE    : Registers the main window class.                           *
                    117:  *                                                                          *
                    118:  *  RETURNS    : TRUE  - if RegisterClass () succeeds.                      *
                    119:  *               FALSE - if RegisterClass () fails.                         *
                    120:  *                                                                          *
                    121:  ****************************************************************************/
                    122: BOOL NEAR PASCAL OwnComboInit (HANDLE hInstance)
                    123: {
                    124:     HANDLE     hMemory;
                    125:     PWNDCLASS  pWndClass;
                    126:     BOOL       bSuccess;
                    127: 
                    128:     /* Allocate for and fill class structure. */
                    129:     hMemory = LocalAlloc (LPTR, sizeof (WNDCLASS));
                    130:     if(!hMemory){
                    131:         MessageBox(NULL, "<OwnComboInit> Not enough memory.", NULL, MB_OK | MB_ICONHAND);
                    132:         return(FALSE);
                    133:     }
                    134: 
                    135:     pWndClass = (PWNDCLASS) LocalLock (hMemory);
                    136: 
                    137:     pWndClass->style         = 0;
                    138:     pWndClass->lpfnWndProc   = (WNDPROC) OwnComboWndProc;
                    139:     pWndClass->hInstance    = hInstance;
                    140:     pWndClass->hIcon         = LoadIcon (hInstance, "owncombo");
                    141:     pWndClass->hCursor       = LoadCursor (NULL, IDC_ARROW);
                    142:     pWndClass->hbrBackground = GetStockObject (WHITE_BRUSH);
                    143:     pWndClass->lpszMenuName  = (LPSTR) "OwnComboMenu",
                    144:     pWndClass->lpszClassName = (LPSTR) "owncombo";
                    145: 
                    146:     bSuccess = RegisterClass (pWndClass);
                    147:     LocalUnlock (hMemory);
                    148:     LocalFree (hMemory);
                    149: 
                    150:     return (bSuccess);
                    151: }
                    152: 
                    153: /****************************************************************************
                    154:  *                                                                          *
                    155:  *  FUNCTION   : About (hDlg,message, wParam, lParam)                       *
                    156:  *                                                                          *
                    157:  *  PURPOSE    : Dialog function for the About... dialog.                   *
                    158:  *                                                                          *
                    159:  ****************************************************************************/
                    160: BOOL APIENTRY About (
                    161:     HWND         hDlg,
                    162:     UINT     message,
                    163:     UINT         wParam,
                    164:     LONG         lParam)
                    165: {
                    166: 
                    167:         UNREFERENCED_PARAMETER(lParam);
                    168:     switch (message){
                    169:         case WM_INITDIALOG:
                    170:             return(TRUE);
                    171: 
                    172:         case WM_COMMAND:
                    173:             if (LOWORD(wParam) == IDOK){
                    174:                 EndDialog (hDlg,0);
                    175:                 return(FALSE);
                    176:             }
                    177:             break;
                    178: 
                    179:         default:
                    180:             break;
                    181:     }
                    182:   return(FALSE);
                    183: }
                    184: 
                    185: 
                    186: /****************************************************************************
                    187:  *                                                                          *
                    188:  *  FUNCTION   : OwnComboWndProc(hWnd, message, wParam, lParam)             *
                    189:  *                                                                          *
                    190:  *  PURPOSE    : Window function for the app. It handles menu selections    *
                    191:  *               and processes window WM_ messages.                         *
                    192:  *                                                                          *
                    193:  ****************************************************************************/
                    194: LONG APIENTRY OwnComboWndProc (
                    195:     HWND         hWnd,
                    196:     UINT     message,
                    197:     UINT         wParam,
                    198:     LONG         lParam)
                    199: {
                    200:     FARPROC          lpProc;
                    201: 
                    202:     switch (message){
                    203:         case WM_COMMAND:
                    204:             switch (LOWORD(wParam)){
                    205:                 case IDM_EXIT:
                    206:                     DestroyWindow (hWnd);
                    207:                     break;
                    208: 
                    209:                 case IDM_ABOUT:
                    210:                     /* Bring up the about box */
                    211:                     lpProc = MakeProcInstance ((FARPROC)About, hInst);
                    212:                     DialogBox (hInst,
                    213:                                "AboutBox",
                    214:                                hWnd,
                    215:                                lpProc);
                    216: 
                    217:                     FreeProcInstance (lpProc);
                    218:                     break;
                    219: 
                    220:                 case IDM_LISTBOX:
                    221:                     /* Bring up the list box example */
                    222:                     lpProc = MakeProcInstance ((FARPROC)ListBoxExample, hInst);
                    223:                     DialogBox (hInst,
                    224:                                "ListBoxDialog",
                    225:                                hWnd,
                    226:                                lpProc);
                    227:                     FreeProcInstance (lpProc);
                    228:                     break;
                    229: 
                    230:                 case IDM_MULTILISTBOX:
                    231:                     /* Bring up the multiple selection list box example */
                    232:                     lpProc = MakeProcInstance ((FARPROC)ListBoxExample, hInst);
                    233:                     DialogBox (hInst,
                    234:                                "MultiListBoxDialog",
                    235:                                hWnd,
                    236:                                lpProc);
                    237:                     FreeProcInstance (lpProc);
                    238:                     break;
                    239: 
                    240:                 case IDM_COMBOBOX:
                    241:                     /* Bring up the combo box example */
                    242:                     lpProc = MakeProcInstance ((FARPROC)ComboBoxExample, hInst);
                    243:                     DialogBox (hInst,
                    244:                                "ComboBoxDialog",
                    245:                                hWnd,
                    246:                                lpProc);
                    247:                     FreeProcInstance (lpProc);
                    248:                     break;
                    249: 
                    250:                 case IDM_OWNERCOMBOBOX:
                    251:                     /* Bring up the owner-draw dropdown list box example */
                    252:                     lpProc = MakeProcInstance ((FARPROC)OwnerComboBoxExample, hInst);
                    253:                     DialogBox (hInst,
                    254:                                "OwnerComboBoxDialog",
                    255:                                hWnd,
                    256:                                lpProc);
                    257:                     FreeProcInstance (lpProc);
                    258:                     break;
                    259:             }
                    260:             break;
                    261: 
                    262:         case WM_DESTROY:
                    263:             PostQuitMessage (0);
                    264:             break;
                    265: 
                    266:         default:
                    267:             return(DefWindowProc(hWnd, message, wParam, lParam));
                    268:     }
                    269:     return(0);
                    270: }
                    271: 
                    272: /****************************************************************************
                    273:  *                                                                          *
                    274:  *  FUNCTION   : HandleSelectionState(LPDRAWITEMSTRUCT, int)                *
                    275:  *                                                                          *
                    276:  *  PURPOSE    : Handles a change in an item selection state. If an item is *
                    277:  *               selected, a black rectangular frame is drawn around that   *
                    278:  *               item; if an item is de-selected, the frame is removed.     *
                    279:  *                                                                          *
                    280:  *  COMMENT    : The black selection frame is slightly larger than the gray *
                    281:  *               focus frame so they won't paint over each other.           *
                    282:  *                                                                          *
                    283:  ****************************************************************************/
                    284: VOID APIENTRY HandleSelectionState(
                    285:         LPDRAWITEMSTRUCT        lpdis,
                    286:         INT                     inflate)
                    287: {
                    288:         RECT    rc;
                    289:         HBRUSH  hbr;
                    290: 
                    291:         /* Resize rectangle to place selection frame outside of the focus
                    292:          * frame and the item.
                    293:          */
                    294:         CopyRect ((LPRECT)&rc, (LPRECT)&lpdis->rcItem);
                    295:         InflateRect ((LPRECT)&rc, inflate, inflate);
                    296: 
                    297:         if (lpdis->itemState & ODS_SELECTED)
                    298:         {
                    299:                 /* selecting item -- paint a black frame */
                    300:                 hbr = GetStockObject(BLACK_BRUSH);
                    301:         }
                    302:         else
                    303:         {
                    304:                 /* de-selecting item -- remove frame */
                    305:                 hbr = CreateSolidBrush(GetSysColor(COLOR_WINDOW));
                    306:         }
                    307:         FrameRect(lpdis->hDC, (LPRECT)&rc, hbr);
                    308:         DeleteObject (hbr);
                    309: }
                    310: 
                    311: /****************************************************************************
                    312:  *                                                                          *
                    313:  *  FUNCTION   : HandleFocusState(LPDRAWITEMSTRUCT, int)                    *
                    314:  *                                                                          *
                    315:  *  PURPOSE    : Handle a change in item focus state. If an item gains the  *
                    316:  *               input focus, a gray rectangular frame is drawn around that *
                    317:  *               item; if an item loses the input focus, the gray frame is  *
                    318:  *               removed.                                                   *
                    319:  *                                                                          *
                    320:  *  COMMENT    : The gray focus frame is slightly smaller than the black    *
                    321:  *               selection frame so they won't paint over each other.       *
                    322:  *                                                                          *
                    323:  ****************************************************************************/
                    324: VOID APIENTRY HandleFocusState(
                    325:         LPDRAWITEMSTRUCT        lpdis,
                    326:         INT                     inflate)
                    327: {
                    328:         RECT    rc;
                    329:         HBRUSH  hbr;
                    330: 
                    331:         /* Resize rectangle to place focus frame between the selection
                    332:          * frame and the item.
                    333:          */
                    334:         CopyRect ((LPRECT)&rc, (LPRECT)&lpdis->rcItem);
                    335:         InflateRect ((LPRECT)&rc, inflate, inflate);
                    336: 
                    337:         if (lpdis->itemState & ODS_FOCUS)
                    338:         {
                    339:                 /* gaining input focus -- paint a gray frame */
                    340:                 hbr = GetStockObject(GRAY_BRUSH);
                    341:         }
                    342:         else
                    343:         {
                    344:                 /* losing input focus -- remove (paint over) frame */
                    345:                 hbr = CreateSolidBrush(GetSysColor(COLOR_WINDOW));
                    346:         }
                    347:         FrameRect(lpdis->hDC, (LPRECT)&rc, hbr);
                    348:         DeleteObject (hbr);
                    349: }
                    350: 
                    351: /****************************************************************************
                    352:  *                                                                          *
                    353:  *  FUNCTION   : DrawEntireItem(LPDRAWITEMSTRUCT, int)                      *
                    354:  *                                                                          *
                    355:  *  PURPOSE    : Draws an item and frames it with a selection frame and/or  *
                    356:  *               a focus frame when appropriate.                            *
                    357:  *                                                                          *
                    358:  ****************************************************************************/
                    359: VOID APIENTRY DrawEntireItem(
                    360:         LPDRAWITEMSTRUCT        lpdis,
                    361:         INT                     inflate)
                    362: {
                    363:         RECT    rc;
                    364:         HBRUSH  hbr;
                    365: 
                    366:         /* Resize rectangle to leave space for frames */
                    367:         CopyRect ((LPRECT)&rc, (LPRECT)&lpdis->rcItem);
                    368:         InflateRect ((LPRECT)&rc, inflate, inflate);
                    369: 
                    370:         /* Create a brush using the value in the item data field (this value
                    371:          * was initialized when we added the item to the list/combo box using
                    372:          * LB_ADDSTRING/CB_ADDSTRING) and draw the color in the list/combo box.
                    373:          */
                    374:         hbr = CreateSolidBrush (lpdis->itemData);
                    375:         FillRect (lpdis->hDC, (LPRECT)&rc, hbr);
                    376:         DeleteObject (hbr);
                    377: 
                    378:         /* Draw or erase appropriate frames */
                    379:         HandleSelectionState(lpdis, inflate + 4);
                    380:         HandleFocusState(lpdis, inflate + 2);
                    381: }
                    382: 
                    383: /****************************************************************************
                    384:  *                                                                          *
                    385:  *  FUNCTION   : ListBoxExample (hDlg, message, wParam, lParam)             *
                    386:  *                                                                          *
                    387:  *  PURPOSE    : Dialog function for the owner-draw list box example.       *
                    388:  *               It sets up the example dialog with the owner-draw list box,*
                    389:  *               adds the colors to the list box, and handles setting the   *
                    390:  *               selection and focus for the items.                         *
                    391:  *                                                                          *
                    392:  ****************************************************************************/
                    393: BOOL APIENTRY ListBoxExample (
                    394:     HWND hDlg,
                    395:     UINT message,
                    396:     UINT wParam,
                    397:     LONG lParam)
                    398: {
                    399:     LPDRAWITEMSTRUCT    lpdis;
                    400:     LPMEASUREITEMSTRUCT lpmis;
                    401: 
                    402:     /* Vars for WM_DRAWITEM */
                    403: 
                    404:     switch (message){
                    405:         case WM_COMMAND:
                    406:             switch (LOWORD(wParam)){
                    407:                 case IDOK:
                    408:                    EndDialog (hDlg, 0);
                    409:                    return (TRUE);
                    410:                    break;
                    411: 
                    412:                 /* Clicking any of these buttons adds the corresponding color
                    413:                  * to the list box. The application-supplied data is the RGB
                    414:                  * value for the color to be drawn in the listbox.
                    415:                  */
                    416:                 case ID_BLACK:
                    417:                     SendMessage (GetDlgItem (hDlg, ID_LISTBOX),
                    418:                                  LB_ADDSTRING,
                    419:                                  0,
                    420:                                  RGB (0,0,0));
                    421:                     return(TRUE);
                    422:                     break;
                    423:                 case ID_RED:
                    424:                     SendMessage (GetDlgItem (hDlg, ID_LISTBOX),
                    425:                                  LB_ADDSTRING,
                    426:                                  0,
                    427:                                  RGB (255,0,0));
                    428:                     return(TRUE);
                    429:                     break;
                    430: 
                    431:                 case ID_BLUE:
                    432:                     SendMessage (GetDlgItem (hDlg, ID_LISTBOX),
                    433:                                  LB_ADDSTRING,
                    434:                                  0,
                    435:                                  RGB (0,0,255));
                    436:                     return(TRUE);
                    437:                     break;
                    438: 
                    439:                 case ID_GREEN:
                    440:                     SendMessage (GetDlgItem (hDlg, ID_LISTBOX),
                    441:                                  LB_ADDSTRING,
                    442:                                  0,
                    443:                                  RGB (0,255,0));
                    444:                     return(TRUE);
                    445:                     break;
                    446: 
                    447:                 default:
                    448:                     return(FALSE);
                    449:                     break;
                    450:             }
                    451: 
                    452:         case WM_DRAWITEM:
                    453:             /* Get pointer to the DRAWITEMSTRUCT */
                    454:             lpdis = (LPDRAWITEMSTRUCT)lParam;
                    455: 
                    456:             if (lpdis->itemID == -1)
                    457:             {
                    458:                 /* We have a request to draw an item in the list box, yet there
                    459:                  * are no list box items. This is sent when the user TABS into
                    460:                  * an empty list box or an empty list box gets the focus. We
                    461:                  * have to indicate (somehow) that this owner-draw list box has
                    462:                  * the focus. We do it in response to this message. Note that
                    463:                  * lpdis->itemData field would be invalid in this instance so
                    464:                  * we can't allow it to fall into our standard routines.
                    465:                  */
                    466:                 HandleFocusState(lpdis, -5);
                    467:             }
                    468:             else
                    469:             {
                    470:                 switch (lpdis->itemAction)
                    471:                 {
                    472:                         case ODA_DRAWENTIRE:
                    473:                                 DrawEntireItem(lpdis, -7);
                    474:                                 break;
                    475: 
                    476:                         case ODA_SELECT:
                    477:                                 HandleSelectionState(lpdis, -3);
                    478:                                 break;
                    479: 
                    480:                         case ODA_FOCUS:
                    481:                                 HandleFocusState(lpdis, -5);
                    482:                                 break;
                    483:                 }
                    484:             }
                    485: 
                    486:             /* Return TRUE meaning that we processed this message. */
                    487:             return(TRUE);
                    488:             break;
                    489: 
                    490:         case WM_MEASUREITEM:
                    491:             lpmis = (LPMEASUREITEMSTRUCT)lParam;
                    492: 
                    493:             /* All the items are the same height since the list box style is
                    494:              * LBS_OWNERDRAWFIXED
                    495:              */
                    496:             lpmis->itemHeight = 30;
                    497:             break;
                    498: 
                    499:         case WM_CLOSE:
                    500:             EndDialog(hDlg, 0);
                    501:             return(TRUE);
                    502:             break;
                    503: 
                    504:         default:
                    505:             return(FALSE);
                    506:     }
                    507: 
                    508:     return(TRUE);
                    509: }
                    510: 
                    511: /****************************************************************************
                    512:  *                                                                          *
                    513:  *  FUNCTION   : ComboBoxExample(hWnd, message, wParam, lParam)             *
                    514:  *                                                                          *
                    515:  *  PURPOSE    : Dialog function for the text combo dialog. The push buttons*
                    516:  *               send various messages to the combo box and the edit control*
                    517:  *               when selected. They allow the user to vary data sent with  *
                    518:  *               each message.                                              *
                    519:  *                                                                          *
                    520:  ****************************************************************************/
                    521: BOOL APIENTRY ComboBoxExample(
                    522:     HWND hDlg,
                    523:     UINT message,
                    524:     UINT wParam,
                    525:     LONG lParam)
                    526: {
                    527: 
                    528:     HWND hWndCombo;                  /* Handle to the combo box control */
                    529:                                      /* in the dialog box window        */
                    530:     HWND hWndCheckBox;               /* Handle to the Auto Check Box    */
                    531:     CHAR strSingleEditLine[255];     /* Single line edit control input  */
                    532:     INT  wIndex, wCount;
                    533: 
                    534:     /* Get handles to the Combo box and the Check box */
                    535:     hWndCombo     = GetDlgItem(hDlg, ID_COMBOBOX);
                    536:     hWndCheckBox  = GetDlgItem(hDlg, ID_STEPSBOX);
                    537: 
                    538:     switch (message){
                    539:         case WM_COMMAND:
                    540:             switch (LOWORD(wParam)){
                    541:                 case IDOK:
                    542:                     EndDialog (hDlg,0);
                    543:                     return(TRUE);
                    544: 
                    545:                 case ID_UNSLBUTTON:
                    546:                     /* Selecting this button unselects any selection in the
                    547:                      * combo box.
                    548:                      */
                    549:                     SetDlgItemText (hDlg, ID_TEXT1, "");
                    550:                     SetDlgItemText (hDlg, ID_TEXT2, "");
                    551:                     wIndex = (WORD) SendMessage( hWndCombo, CB_GETCURSEL, 0, 0L);
                    552:                     if (wIndex == CB_ERR)
                    553:                         MessageBox (hDlg, (LPSTR)"No Selection", NULL, MB_OK);
                    554:                     else
                    555:                         SendMessage (hWndCombo, CB_SETCURSEL, (WPARAM)-1, 0L);
                    556:                     SetFocus (GetDlgItem (hDlg, ID_SINGLEEDIT));
                    557:                     break;
                    558: 
                    559:                 case ID_NUMSELBUTTON:
                    560:                     /* An integer value is taken from the edit control and an
                    561:                      * attempt is made to select a combo box entry having this
                    562:                      * index.
                    563:                      */
                    564:                     SetDlgItemText (hDlg, ID_TEXT1, "");
                    565:                     SetDlgItemText (hDlg, ID_TEXT2, "");
                    566:                     wCount = (WORD) SendMessage (hWndCombo, CB_GETCOUNT, 0, 0L);
                    567:                     wIndex = (INT) GetDlgItemInt (hDlg, ID_SINGLEEDIT, NULL, TRUE);
                    568:                     if (wIndex >= wCount)
                    569:                         MessageBox (hDlg, (LPSTR)"Bad Selection", NULL, MB_OK);
                    570:                     else
                    571:                         SendMessage(hWndCombo, CB_SETCURSEL, wIndex, 0L);
                    572:                     SetFocus (GetDlgItem (hDlg, ID_SINGLEEDIT));
                    573:                     break;
                    574: 
                    575:                 case ID_TXTSELBUTTON:
                    576:                     /* A text string is taken from the edit control and an
                    577:                      * attempt is made to select a combo box entry having the
                    578:                      * string as a prefix.
                    579:                      */
                    580:                     SetDlgItemText (hDlg, ID_TEXT1, "");
                    581:                     SetDlgItemText (hDlg, ID_TEXT2, "");
                    582:                     GetDlgItemText (hDlg, ID_SINGLEEDIT,
                    583:                                  (LPSTR)strSingleEditLine, 255);
                    584:                     wIndex = (WORD) SendMessage (hWndCombo,
                    585:                                                 CB_SELECTSTRING,
                    586:                                                 (WPARAM)-1,
                    587:                                                 (LONG)(LPSTR)strSingleEditLine);
                    588:                     if (wIndex == CB_ERR)
                    589:                       MessageBox (hDlg, (LPSTR)"Bad Selection", NULL, MB_OK);
                    590:                     SetFocus (GetDlgItem (hDlg, ID_SINGLEEDIT));
                    591:                     break;
                    592: 
                    593:                 case ID_FNDSELBUTTON:
                    594:                     /* Searches for the text specified in the list of combo
                    595:                      * entries and returns the index (in combo box) of the
                    596:                      * first match. The index is displayed in the "Text1"
                    597:                      * field of the dialog.
                    598:                      */
                    599:                     SetDlgItemText (hDlg, ID_TEXT1, "");
                    600:                     SetDlgItemText (hDlg, ID_TEXT2, "");
                    601:                     GetDlgItemText (hDlg,
                    602:                                     ID_SINGLEEDIT,
                    603:                                     (LPSTR)strSingleEditLine,
                    604:                                     255);
                    605:                     wIndex = (WORD)SendMessage (hWndCombo,
                    606:                                                CB_FINDSTRING,(WPARAM)-1,
                    607:                                                (LONG)(LPSTR)strSingleEditLine);
                    608:                     if (wIndex == CB_ERR)
                    609:                         MessageBox (hDlg, (LPSTR)"Bad Selection", NULL, MB_OK);
                    610:                     else
                    611:                         SetDlgItemInt (hDlg, ID_TEXT1, wIndex, FALSE);
                    612:                     SetFocus (GetDlgItem (hDlg, ID_SINGLEEDIT));
                    613:                     break;
                    614: 
                    615:                 case ID_CLRBUTTON:
                    616:                     /* Clears the combo box of all it's entries */
                    617:                     SetDlgItemText (hDlg, ID_TEXT1, "");
                    618:                     SetDlgItemText (hDlg, ID_TEXT2, "");
                    619:                     wCount = (WORD) SendMessage (hWndCombo, CB_GETCOUNT, 0, 0L);
                    620:                     if (!wCount)
                    621:                         MessageBox (hDlg, (LPSTR)"Already clear", NULL, MB_OK);
                    622:                     else{
                    623:                         SetDlgItemInt (hDlg, ID_TEXT1, wCount, TRUE);
                    624:                         SetDlgItemText (hDlg, ID_TEXT2, "Items cleared");
                    625:                         SendMessage (hWndCombo,CB_RESETCONTENT, 0, 0L);
                    626:                     }
                    627:                     SetFocus (GetDlgItem (hDlg,ID_SINGLEEDIT));
                    628:                     break;
                    629: 
                    630:                 case ID_ADDBUTTON:
                    631:                     /* Takes the string specified in the edit control and
                    632:                      * adds it to the combo box.
                    633:                      */
                    634:                     SetDlgItemText (hDlg, ID_TEXT1, "");
                    635:                     SetDlgItemText (hDlg, ID_TEXT2, "");
                    636:                     GetDlgItemText (hDlg, ID_SINGLEEDIT, strSingleEditLine, 255);
                    637:                     SendMessage (hWndCombo,
                    638:                                  CB_ADDSTRING,
                    639:                                  0,
                    640:                                  (LONG)(LPSTR) strSingleEditLine);
                    641:                     SetFocus (GetDlgItem (hDlg, ID_SINGLEEDIT));
                    642:                     break;
                    643: 
                    644:                 case ID_DELETEBUTTON:
                    645:                     /* Delete the currently selected item from the combo box. */
                    646:                     SetDlgItemText (hDlg, ID_TEXT1, "");
                    647:                     SetDlgItemText (hDlg, ID_TEXT2, "");
                    648:                     wIndex = (WORD) SendMessage (hWndCombo, CB_GETCURSEL, 0, 0L);
                    649:                     if (SendMessage (hWndCombo, CB_DELETESTRING, wIndex, 0L) == CB_ERR)
                    650:                         MessageBox (hDlg, (LPSTR)"No Selection", NULL, MB_OK);
                    651:                     else{
                    652:                         SetDlgItemText (hDlg, ID_TEXT1, "deleted index #");
                    653:                         SetDlgItemInt  (hDlg, ID_TEXT2, wIndex, TRUE);
                    654:                     }
                    655:                     SetFocus (GetDlgItem (hDlg, ID_SINGLEEDIT));
                    656:                     break;
                    657: 
                    658:                 case ID_CBDIRBUTTON:
                    659:                     /* Appends a directory listing of the current directory
                    660:                      * to the combo box entries.
                    661:                      */
                    662:                     SetDlgItemText (hDlg, ID_TEXT1, "");
                    663:                     SetDlgItemText (hDlg, ID_TEXT2, "");
                    664:                     wIndex = (WORD)SendMessage (hWndCombo,
                    665:                                                CB_DIR,
                    666:                                                0x10|0x4000,
                    667:                                                (LONG)(LPSTR)"*.*");
                    668:                     SetFocus (GetDlgItem (hDlg, ID_SINGLEEDIT));
                    669:                     break;
                    670: 
                    671: 
                    672:                 case ID_CPYBUTTON:
                    673:                     /* Copies the currently selected item in the combo box to
                    674:                      * the edit control.
                    675:                      */
                    676:                     SetDlgItemText (hDlg, ID_TEXT1, "");
                    677:                     SetDlgItemText (hDlg, ID_TEXT2, "");
                    678:                     wIndex = (WORD) SendMessage (hWndCombo, CB_GETCURSEL, 0, 0L);
                    679:                     if (wIndex == CB_ERR)
                    680:                         MessageBox(hDlg, (LPSTR)"No Selection", NULL, MB_OK);
                    681:                     else{
                    682:                         wCount = SendMessage (hWndCombo, CB_GETLBTEXTLEN, wIndex, 0L);
                    683:                         SendMessage (hWndCombo,
                    684:                                      CB_GETLBTEXT,
                    685:                                      wIndex,
                    686:                                      (LONG)(LPSTR)strSingleEditLine);
                    687:                         SetDlgItemText(hDlg, ID_SINGLEEDIT,
                    688:                                        (LPSTR)strSingleEditLine);
                    689:                         SetDlgItemText(hDlg, ID_TEXT1, "copied index #");
                    690:                         SetDlgItemInt(hDlg, ID_TEXT2, wIndex, TRUE);
                    691:                     }
                    692:                     SetFocus (GetDlgItem (hDlg, ID_SINGLEEDIT));
                    693:                     break;
                    694: 
                    695:                 /* When the combo notification box is checked, a message box
                    696:                  * is flashed showing what notification codes the combo box is
                    697:                  * returning to the app. in response to the messages sent by
                    698:                  * the buttons.
                    699:                  */
                    700:                 case ID_COMBOBOX:
                    701:                     if (SendMessage (hWndCheckBox, BM_GETCHECK, 0, 0L)){
                    702:                         switch (HIWORD(lParam)){
                    703:                             case (WORD)CBN_ERRSPACE:
                    704:                               MessageBox (hDlg, (LPSTR)"CB Out of Space",
                    705:                                          "CB MSG", MB_OK);
                    706:                               break;
                    707: 
                    708:                             case CBN_SELCHANGE:
                    709:                               MessageBox (hDlg, (LPSTR)"CB Sel Change",
                    710:                                          "CB MSG", MB_OK);
                    711:                               break;
                    712: 
                    713:                             case CBN_DBLCLK:
                    714:                               MessageBox(hDlg, (LPSTR)"CB Double Click",
                    715:                                          "CB MSG", MB_OK);
                    716:                               break;
                    717: 
                    718:                             case CBN_SETFOCUS:
                    719:                               SetDlgItemText(hDlg, ID_TEXT1, "CB SetFocus");
                    720:                               break;
                    721: 
                    722:                             case CBN_KILLFOCUS:
                    723:                               SetDlgItemText(hDlg, ID_TEXT1, "CB KillFocus");
                    724:                               break;
                    725:                         }
                    726:                     }
                    727:                     break;
                    728: 
                    729:                 default:
                    730:                     return(FALSE);
                    731:             }
                    732:             break;
                    733: 
                    734:         case WM_CLOSE:
                    735:             EndDialog(hDlg, 0);
                    736:             return(TRUE);
                    737:             break;
                    738: 
                    739:         default:
                    740:             return(FALSE);
                    741:     }
                    742:     return(TRUE);
                    743: }
                    744: 
                    745: 
                    746: /****************************************************************************
                    747:  *                                                                          *
                    748:  *  FUNCTION   : OwnerComboBoxExample(hWnd, message, wParam, lParam)        *
                    749:  *                                                                          *
                    750:  *  PURPOSE    : Dialog function for the dropdown list combo box with       *
                    751:  *               owner-draw.                                                *
                    752:  *                                                                          *
                    753:  ****************************************************************************/
                    754: BOOL APIENTRY OwnerComboBoxExample (
                    755:     HWND hDlg,
                    756:     UINT message,
                    757:     UINT wParam,
                    758:     LONG lParam)
                    759: {
                    760:     LPDRAWITEMSTRUCT    lpdis;
                    761:     LPMEASUREITEMSTRUCT lpmis;
                    762: 
                    763:     switch (message){
                    764:         case WM_COMMAND:
                    765:             switch (LOWORD(wParam)){
                    766:                 case IDOK:
                    767:                    EndDialog (hDlg, 0);
                    768:                    return(TRUE);
                    769:                    break;
                    770: 
                    771:                 /* Clicking any of these buttons adds the corresponding color
                    772:                  * to the combo box. The application-supplied data is the RGB
                    773:                  * value for the color to be drawn in the listbox.
                    774:                  */
                    775:                 case ID_BLACK:
                    776:                    SendMessage (GetDlgItem(hDlg, ID_LISTBOX),
                    777:                                 CB_ADDSTRING,
                    778:                                 0,
                    779:                                 RGB (0,0,0));
                    780:                    return(TRUE);
                    781:                    break;
                    782: 
                    783:                 case ID_RED:
                    784:                    SendMessage (GetDlgItem (hDlg, ID_LISTBOX),
                    785:                                 CB_ADDSTRING,
                    786:                                 0,
                    787:                                 RGB (255,0,0));
                    788:                    return(TRUE);
                    789:                    break;
                    790: 
                    791:                 case ID_BLUE:
                    792:                    SendMessage (GetDlgItem(hDlg, ID_LISTBOX),
                    793:                                 CB_ADDSTRING,
                    794:                                 0,
                    795:                                 RGB (0,0,255));
                    796:                    return(TRUE);
                    797:                    break;
                    798: 
                    799:                 case ID_GREEN:
                    800:                    SendMessage (GetDlgItem(hDlg, ID_LISTBOX),
                    801:                                 CB_ADDSTRING,
                    802:                                 0,
                    803:                                 RGB (0,255,0));
                    804:                    return(TRUE);
                    805:                    break;
                    806: 
                    807:                 default:
                    808:                    return(TRUE);
                    809:                    break;
                    810:             }
                    811: 
                    812:         case WM_DRAWITEM:
                    813:             /* Get pointer to the DRAWITEMSTRUCT */
                    814:             lpdis = (LPDRAWITEMSTRUCT)lParam;
                    815: 
                    816:             if (lpdis->itemID == -1){
                    817:                 /* We have a request to draw an item in the combo box, yet there
                    818:                  * are no combo box items. This is sent when the user TABS into
                    819:                  * an empty combo box or an empty combo box gets the focus. We
                    820:                  * have to indicate (somehow) that this owner-draw combo box has
                    821:                  * the focus. We do it in response to this message. Note that
                    822:                  * lpdis->itemData field would be invalid in this instance so
                    823:                  * we can't allow it to fall into our standard routines.
                    824:                  */
                    825:                 HandleFocusState(lpdis, -2);
                    826:             }
                    827:             else
                    828:             {
                    829:                 switch (lpdis->itemAction)
                    830:                 {
                    831:                         case ODA_DRAWENTIRE:
                    832:                                 DrawEntireItem(lpdis, -4);
                    833:                                 break;
                    834: 
                    835:                         case ODA_SELECT:
                    836:                                 HandleSelectionState(lpdis, 0);
                    837:                                 break;
                    838: 
                    839:                         case ODA_FOCUS:
                    840:                                 HandleFocusState(lpdis, -2);
                    841:                                 break;
                    842:                 }
                    843:             }
                    844: 
                    845:             /* Return TRUE meaning that we processed this message. */
                    846:             return(TRUE);
                    847:             break;
                    848: 
                    849:         case WM_MEASUREITEM:
                    850:             lpmis = (LPMEASUREITEMSTRUCT)lParam;
                    851: 
                    852:             /* All the items are the same height since the combo box is
                    853:              * CBS_OWNERDRAWFIXED
                    854:              */
                    855:             if (lpmis->itemID == -1){
                    856:                 /* If -1 for item, then we are setting the height of the
                    857:                  * always visible static item part of the dropdown combo box.
                    858:                  */
                    859:                 lpmis->itemHeight = 25;
                    860:                 return(TRUE);
                    861:             }
                    862:             lpmis->itemHeight = 30;
                    863:             break;
                    864: 
                    865:         case WM_CLOSE:
                    866:             EndDialog(hDlg, 0);
                    867:             return(TRUE);
                    868:             break;
                    869: 
                    870:         default:
                    871:             return(FALSE);
                    872:     }
                    873:     return(TRUE);
                    874: }

unix.superglobalmegacorp.com

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