home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / LISTBOX.ZIP / LSTBOX.DOC
Text File  |  1991-09-07  |  6KB  |  135 lines

  1.  
  2. Here is an extract from my dialog box procedure that produces a two-column
  3. list box.
  4.  
  5. In this example there is only one list box, so I don't have to worry about
  6. which control is involved.  In this example, a blank is used to separate
  7. the first and second column.  You could use tabs or any other sort of
  8. separator character.  You could also draw anything you wanted in the
  9. list box item, including bit maps, colors, other fonts, etc.
  10.  
  11. This is not a complete program, of course, but does show the details
  12. of handling a multi-column list box.
  13.  
  14. Guy 
  15.  
  16. ----------------------------------------------------------------------
  17.  
  18. Guy Scharf is an independent consultant specializing in developing
  19. custom software products for OS/2 using C and data base management
  20. systems.  He is sysop of the Computer Consultant's Forum on
  21. CompuServe and an assistant Sysop of the IBMOS2 Forum.
  22.  
  23. For consulting services or custom software development, please
  24. contact us.
  25.  
  26.     Guy Scharf, President
  27.     Software Architects, Inc.
  28.     2163 Jardin Drive
  29.     Mountain View, CA  94040
  30.     (415) 948-9186
  31.     (415) 948-1620 (fax)
  32.     CompuServe: 76702,557
  33.     Internet: 76702.557@compuserve.com
  34.  
  35.  
  36. /******************** Dialog Box Procedure ******************************/
  37.                       
  38. MRESULT EXPENTRY SelectDlgProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  39. {
  40.     HPS     hPS;                       /* Handle to the presentation space */
  41.     FONTMETRICS FontMetrics;           /* Metrics of default font */
  42.     CHAR    pszItemText[MAX_ITEM_TEXT_LENGTH];
  43.     CHAR    *s;
  44.     OWNERITEM FAR *poi;                /* Pointer to owner item structure */
  45.     RECTL   rcl;                       /* Rectangle for writing */
  46.     COLOR   clrForeGround;
  47.     COLOR   clrBackGround;
  48.  
  49.     switch (msg)
  50.         {
  51.         case WM_INITDLG:               /* Initialize the list box */
  52.  
  53.             FillCfgListBox (hwnd);     /* Fill the list box */
  54.  
  55.             return (FALSE);
  56.  
  57.  
  58.         case WM_MEASUREITEM:           /* Measure text height */
  59.  
  60.             hPS = WinGetPS (hwnd);     /* Get handle to presentation space */
  61.             GpiQueryFontMetrics (hPS, (LONG) sizeof (FONTMETRICS), 
  62.                                                               &FontMetrics);
  63.             WinReleasePS (hPS);        /* Release the presentation space */
  64.  
  65.             return (FontMetrics.lMaxBaselineExt);
  66.  
  67.  
  68.         case WM_DRAWITEM:              /* Draw a list box entry */
  69.  
  70.             poi = mp2;                 /* Get address of owner item */
  71.  
  72.             if (poi->fsState == TRUE)  /* Is this cell to be highlighted? */
  73.             {                          /* Yes, use highlight attributes */
  74.                 clrForeGround = SYSCLR_HILITEFOREGROUND;
  75.                 clrBackGround = SYSCLR_HILITEBACKGROUND;
  76.             }
  77.             else                       /* No, use normal attributes */
  78.             {
  79.                 clrForeGround = CLR_NEUTRAL;
  80.                 clrBackGround = CLR_BACKGROUND;
  81.             }
  82.  
  83.             WinSendMsg (poi->hwnd,     /* Get item text */
  84.                         LM_QUERYITEMTEXT, 
  85.                         (MPARAM) MAKEULONG (poi->idItem, 
  86.                                             MAX_ITEM_TEXT_LENGTH),
  87.                         (MPARAM) pszItemText);
  88.  
  89.             rcl.xLeft   = poi->rclItem.xLeft;  /* Set co-ordinates */
  90.             rcl.xRight  = poi->rclItem.xRight; /* of rectangle */
  91.             rcl.yTop    = poi->rclItem.yTop;
  92.             rcl.yBottom = poi->rclItem.yBottom;
  93.  
  94.             s = strchr (pszItemText, ' ');  /* Find first blank */
  95.             if (s)
  96.                 *s = '\0';             /* Terminate first column here */
  97.  
  98.             WinDrawText (poi->hps,     /* Draw the first column */
  99.                          -1,           /* Null-terminated string */
  100.                          pszItemText,  /* File name is here */
  101.                          &rcl,         /* Rectangle to draw in */
  102.                          clrForeGround,/* Foreground color */
  103.                          clrBackGround,/* Background color */
  104.                          DT_LEFT | DT_VCENTER | DT_ERASERECT);
  105.  
  106.             if (s)                     /* If there is a second column */
  107.             {
  108.                 rcl.xLeft = 100;       /* It starts out here */
  109.                                        /* Spacing calculations could be */
  110.                                        /* much cleverer than this very */
  111.                                        /* crude use of an absolute position */
  112.                                        /* (which is not transportable */
  113.                                        /* to different display types, as */
  114.                                        /* between 8514 and VGA) */
  115.                 s++;                   /* Point to beginning of text */
  116.                 WinDrawText (poi->hps, /* Draw the second column */
  117.                              -1,       /* Also a null-terminated string */
  118.                              s,        /* File Description */
  119.                              &rcl,     /* Rectangle to draw in */
  120.                              clrForeGround,  /* Colors are same as */
  121.                              clrBackGround,  /* before */
  122.                              DT_LEFT | DT_VCENTER);
  123.             }
  124.  
  125.                 /* If fsState || fsStateOld and return TRUE, then */
  126.                 /* control will invert the rectangle -- not what */
  127.                 /* we want.  Tell control not do do anything like that! */
  128.  
  129.             poi->fsState = poi->fsStateOld = FALSE;
  130.  
  131.             return (TRUE);         /* Say we did it */
  132.  
  133.  
  134. ... case statements for rest of program ...
  135.