<< Return to Main Index

< Return to Class Index

class CGXComboBoxWnd: public CComboBox, public CGXStatic

The CGXComboBoxWnd class implements a combo-box control that can be used to display and select items from a list. The user can select items in the combo box, and can change any style attributes with the CGXStyleSheet dialog.

The list of items that are displayed in the combo box is determined through the SetChoiceList attribute of the style object.

Style attributes specifying the appearance of the cell are:

You can also change some global attributes for the CGXComboBoxWnd. See the section “Attributes” below.

The following control ids are predefined in Objective Grid:

You can apply combo boxes to cells with

   SetStyleRange(range,
      CGXStyle()
         .SetControl(GX_IDS_CTRL_CBS_DROPDOWN)
         .SetValue("Item 1")
         .SetChoiceList("Item 0\nItem 1\nItem 2");
      );

If you want to use an index as value for the combo box, you may register a combo box with a non-zero m_bDispChoice attribute (see also the description for this attribute) and call

   SetStyleRange(range,
      CGXStyle()
         .SetControl(IDS_CTRL_MYDROPDOWNLIST)
         .SetValue(1)
         .SetChoiceList("Item 0\nItem 1\nItem 2");
      );

The cell will display “Item 1” in both examples, but in the first sample, the text is stored as value in the style object, and in the second sample, the index is stored in the style object, so that you could later call

   nIndex = atoi(GetValueRowCol(nRow, nCol));

to determine the index of the entry selected by the user.

See the example in the CGXComboBox class description showing how to override GetStyleRowCol and determine the choice list at runtime. This is very useful if the choice list depends on another cell’s value. It works the same way for both CGXComboBoxWnd and CGXComboBox.

Note: Objective Grid also provides an alternative combo-box control. The difference between CGXComboBoxWnd and CGXComboBox is that CGXComboBoxWnd is derived from the Windows standard combo-box control CComboBox whereas CGXComboBox is a CGXEditControl with a small button at the right side of the input area. CGXComboBox has the advantage of fitting better into cells (needs less row-height), whereas CGXComboBoxWnd offers full CComboBox functionality.

If you want to use an owner-drawn combo box as cell, you can simply subclass CGXComboBox and implement the owner-drawn functionality as you would with the MFC CComboBox (see example).

#include <gxall.h>

Example

This example illustrates an owner-drawn combo box.

/////////////////////////////////////////////////////////////////////
// COwnerDrawnComboBox window

class COwnerDrawnComboBox : public CGXComboBoxWnd
{
   GRID_DECLARE_CONTROL(COwnerDrawnComboBox);

// Construction
public:
   COwnerDrawnComboBox(CGXGridCore* pGrid);

// Attributes
public:

// Operations
public:

// Overrides
   // ClassWizard generated virtual function overrides
   //{{AFX_VIRTUAL(COwnerDrawnComboBox)
   public:
   virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
   virtual void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct);
   //}}AFX_VIRTUAL

// Implementation
public:
   virtual ~COwnerDrawnComboBox();
   virtual BOOL OnCommand(WPARAM wParam, LPARAM lParam);

   // Generated message map functions
protected:
   //{{AFX_MSG(COwnerDrawnComboBox)
      // NOTE - the ClassWizard will add and remove member functions here.
   //}}AFX_MSG

   GRID_DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////
// COwnerDrawnComboBox

static COLORREF VGAColorsArray[20] = {
      RGB(0,0,0),         // Black
      RGB(0,0,255),       // Bright blue
      RGB(0,255,0),       // Bright green
      RGB(0,255,255),     // Cyan
      RGB(255,0,0),       // Bright red
      RGB(255,0,255),     // Magenta
      RGB(255,255,0),     // Bright yellow
      RGB(255,255,255),   // White
      RGB(0,0,128),       // Dark blue
      RGB(0,128,0),       // Dark green
      RGB(0,128,128),     // Blue-green
      RGB(128,0,0),       // Brown
      RGB(128,0,128),     // Dark purple
      RGB(128,128,0),     // Olive
      RGB(128,128,128),   // Dark gray
      RGB(192,192,192),   // Light gray
      RGB(192,220,192),   // Pale green
      RGB(166,202,240),   // Light blue
      RGB(255,251,240),   // Off-white
      RGB(160,160,164),   // Medium gray
   };

COwnerDrawnComboBox::COwnerDrawnComboBox(CGXGridCore* pGrid)
   : CGXComboBoxWnd(pGrid)
{
   m_bFillWithChoiceList = FALSE;  // must be FALSE for ownderdrawn combobox
   m_bWantArrowKeys = FALSE;       // pass arrow keys to Grid
   m_nIndexValue = 0;              // 0 = Item 0, 1 = Item 1, ... n = Item N

   // set m_nIndexValue = 1 if you want to have 1 = Item 0, 2 = Item 1, ... n = Item N+1
}

COwnerDrawnComboBox::~COwnerDrawnComboBox()
{
}

void COwnerDrawnComboBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
   CRect rc = lpDrawItemStruct->rcItem;
   COLORREF rgbColor = lpDrawItemStruct->itemData;
   CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);

   GXPatB(pDC, rc, rgbColor);

   if (lpDrawItemStruct->itemState & ODS_SELECTED)
   {
      CBrush br;
      br.CreateStockObject(BLACK_BRUSH);
      pDC->FrameRect(CRect(rc.left, rc.top, rc.right, rc.bottom), &br);
   }
}

void COwnerDrawnComboBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
   lpMeasureItemStruct->itemHeight = 20;
}

BEGIN_MESSAGE_MAP(COwnerDrawnComboBox, CGXComboBoxWnd)
   //{{AFX_MSG_MAP(COwnerDrawnComboBox)
   //}}AFX_MSG_MAP
END_MESSAGE_MAP()

BOOL COwnerDrawnComboBox::OnCommand(WPARAM wParam, LPARAM lParam)
{
#if _MFC_VER < 0x0300
   UINT nNotification = HIWORD(lParam);
   HWND hCtl = (HWND) LOWORD(lParam);
#else
   UINT nNotification = HIWORD(wParam);
   HWND hCtl = (HWND) lParam;
#endif

   if (hCtl == m_hWnd)     // is it really this wnd?
   {
      if (nNotification == CBN_SELCHANGE
         || nNotification == CBN_EDITCHANGE)
      {
         TRACE("Combobox changed\n");
      }
   }

   return CGXComboBoxWnd::OnCommand(wParam, lParam);
}

See Also

CGXGridCore::RegisterControl CGXStyle::SetControl CGXStyle CGXComboBox

CGXComboBoxWnd

Class Members