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

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

unix.superglobalmegacorp.com

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