home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / wpj_mag / wpjv1n4.zip / ODLBOX.ZIP / ODLBOX.C next >
Text File  |  1993-03-28  |  5KB  |  149 lines

  1. // Function to handle an owner-drawn list box
  2. #include <windows.h>
  3. #include "odlbox.h"
  4.  
  5. BOOL FAR PASCAL ODListBox(HWND hDlg, unsigned message, WORD wParam,
  6. LONG lParam) {
  7.  
  8. // define some colors for the selected text and background
  9. #define GREEN  RGB(0,255,0)
  10. #define BLUE   RGB(0,0,255)
  11.  
  12. HWND hLst;                    // handle to the list box
  13. HDC  hDC;                     // device context handle
  14. LPDRAWITEMSTRUCT lpDIS;       // long pointer to DRAWITEMSTRUCT
  15. LPMEASUREITEMSTRUCT lpMIS;    // long pointer to MEASUREITEMSTRUCT
  16. TEXTMETRIC tm;                // text info structure
  17.  
  18. HBRUSH hBrush;                // draws text background
  19. DWORD OldTextColor;           // saves old text color
  20. DWORD OldBkColor;             // saves old background color
  21.  
  22. int NumItems;                 // number of items in list box
  23. int i;                        // temporary variable
  24.  
  25. char textStr[80];             // list box item string buffer
  26. char *textItems[] =
  27. {                             // strings going into the list box
  28.   "One",
  29.   "Two",
  30.   "Three",
  31.   "Four",
  32.   "Five",
  33.   "Six",
  34.   "Seven",
  35.   "Eight",
  36.   "Nine",
  37.   "Ten"
  38. };
  39.  
  40. NumItems= 10;
  41.  
  42. switch (message) {
  43.  
  44.      case WM_INITDIALOG:
  45.           hLst= GetDlgItem(hDlg, IDB_LISTBOX);
  46.           for(i = 0; i < NumItems; ++i)
  47.                SendMessage(hLst, LB_ADDSTRING, 0, (LONG)
  48.                     (LPSTR) textItems[i]);
  49.           SetFocus(GetDlgItem(hDlg, IDB_OK));
  50.           break;
  51.  
  52.  
  53.      case WM_COMMAND:
  54.  
  55.           if(wParam == IDB_OK) {
  56.                EndDialog(hDlg, NULL);
  57.                return TRUE;
  58.           }
  59.           break;
  60.  
  61.  
  62.      case WM_DRAWITEM:
  63.  
  64.           // copy the DRAWITEMSTRUCT pointer from lParam
  65.           lpDIS= (LPDRAWITEMSTRUCT)lParam;
  66.  
  67.           // if no items in the list box yet, set focus
  68.           if(lpDIS->itemID == -1) {
  69.             DrawFocusRect(lpDIS->hDC, (LPRECT)&lpDIS->rcItem);
  70.             return TRUE;
  71.           }
  72.  
  73.           // draw items in list box and check for selection
  74.           if((lpDIS->itemAction & ODA_DRAWENTIRE) ||
  75.                (lpDIS->itemAction & ODA_SELECT)) {
  76.  
  77.                // Get the text string and save in textStr
  78.                SendMessage(hLst, LB_GETTEXT, (WORD)lpDIS->itemID,
  79.                     (LONG) (LPSTR) textStr);
  80.  
  81.                // Handle the selection state
  82.                if (lpDIS->itemState & ODS_SELECTED) {
  83.  
  84.                     // text was selected, so make it red on blue
  85.                     // first, draw and fill background
  86.                     hBrush= CreateSolidBrush(BLUE);
  87.                     FillRect(lpDIS->hDC, (LPRECT)&lpDIS->rcItem,
  88.                          hBrush);
  89.                     DeleteObject(hBrush);
  90.  
  91.                     // set colors and output the text
  92.                     OldTextColor= SetTextColor(lpDIS->hDC, GREEN);
  93.                     OldBkColor= SetBkColor(lpDIS->hDC, BLUE);
  94.                     TextOut(lpDIS->hDC, (int)(lpDIS->rcItem.left),
  95.                          (int)(lpDIS->rcItem.top), (LPSTR)textStr,
  96.                          lstrlen(textStr));
  97.  
  98.                     // restore old colors
  99.                     SetTextColor(lpDIS->hDC, OldTextColor);
  100.                     SetBkColor(lpDIS->hDC, OldBkColor);
  101.  
  102.                }
  103.                else {
  104.  
  105.                     // item not selected, so make it black on white
  106.                     // first, draw and fill background
  107.                     hBrush= GetStockObject(WHITE_BRUSH);
  108.                     FillRect(lpDIS->hDC, (LPRECT)&lpDIS->rcItem,
  109.                          hBrush);
  110.  
  111.                     // next, draw the text
  112.                     TextOut(lpDIS->hDC, (int)(lpDIS->rcItem.left),
  113.                          (int)(lpDIS->rcItem.top), (LPSTR)textStr,
  114.                          lstrlen(textStr));
  115.  
  116.                }
  117.  
  118.                // Check for focus state
  119.                if(lpDIS->itemState & ODS_FOCUS)
  120.                  DrawFocusRect(lpDIS->hDC, (LPRECT)&lpDIS->rcItem);
  121.  
  122.                return TRUE;
  123.  
  124.           }
  125.  
  126.           if(lpDIS->itemAction & ODA_FOCUS) {
  127.  
  128.                DrawFocusRect(lpDIS->hDC, (LPRECT)&lpDIS->rcItem);
  129.                return TRUE;
  130.  
  131.           }
  132.           break;
  133.  
  134.  
  135.      case WM_MEASUREITEM:
  136.  
  137.           /* get and use height of current font */
  138.           lpMIS= (LPMEASUREITEMSTRUCT)lParam;    // copy info pointer
  139.           hDC= GetDC(hDlg);                  // create a DC
  140.           GetTextMetrics(hDC, &tm);          // get text info
  141.           lpMIS->itemHeight= tm.tmHeight;    // get text height
  142.           ReleaseDC(hDlg, hDC);              // free the DC
  143.           return TRUE;
  144.  
  145. } /* switch(message) - end */
  146. return FALSE;
  147.  
  148. } /* ODListBox() - end */
  149.