ComboBox: Switch between drop down '& drop list mode

I took the hint of Moshe Stolar to display black text in a disabled ComboBox to develope a small class whith more color control. In addition it can act like a drop down list an You may switch between drop down box mode and drop list mode with a simple call to SetReadOnly().
//////////////////////////////////////////////////////////////////////////////////////
#if !defined(AFX_MYCOMBOBOX_H__75106EA1_6649_11D1_95E5_0060971F6E1E__INCLUDED_)
#define AFX_MYCOMBOBOX_H__75106EA1_6649_11D1_95E5_0060971F6E1E__INCLUDED_

#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000

// MyComboBox.h : header file
//

// CMyComboBox window

// Author: Robert Cremer

// A ComboBox with easy to switch between edit mode and non edit mode.
// (i. e. works like a drop down or a drop list)
// The ComboBox has real coloring, working in drop down and drop list mode.
// !!! The Box must not be a drop list (CBS_DROPDOWNLIST) !!!
// Working as a drop list can be done with a call to SetReadOnly()
// or with the CTor param false.
class CMyComboBox : public CComboBox
{
// Attributes
private:
   // default colors
   enum
   {
      ENABLED_FG = RGB(0,0,0), // black
      ENABLED_BG = RGB(255,255,255), // white
      DISABLED_FG = RGB(0,0,0), // black
      DISABLED_BG = RGB(192,192,192), // light grey
   };

   // edit mode
   bool m_bEditable; 

   // the actual colors
   COLORREF m_crFGEnabled;
   COLORREF m_crBGEnabled;
   COLORREF m_crFGDisabled;
   COLORREF m_crBGDisabled;

   // Background brush
   CBrush *m_pbrushDisabled;
   // Foreground brush
   CBrush *m_pbrushEnabled;

// Operations
public:
   void SetReadOnly(bool bReadOnly = true);
   void SetEnabledColor(COLORREF crFG = ENABLED_FG, COLORREF crBG = ENABLED_BG);
   void SetDisabledColor(COLORREF crFG = DISABLED_FG, COLORREF crBG =
DISABLED_BG);

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMyComboBox)
//}}AFX_VIRTUAL

// Implementation
public:
   // Construction
   CMyComboBox(bool bEditable = true);
   virtual ~CMyComboBox();

   // Generated message map functions
protected:
   //{{AFX_MSG(CMyComboBox)
   afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
   afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
   afx_msg void OnEnable(BOOL bEnable);
   //}}AFX_MSG

   DECLARE_MESSAGE_MAP()
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately
before the previous line.

#endif //
!defined(AFX_MYCOMBOBOX_H__75106EA1_6649_11D1_95E5_0060971F6E1E__INCLUDED_)

//////////////////////////////////////////////////////////////////////////////////////
// MyComboBox.cpp : implementation file
//

#include "MyComboBox.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

// CMyComboBox

CMyComboBox::CMyComboBox(bool bEditable)
{
   // defaults
   m_bEditable = bEditable;
   m_crFGEnabled = ENABLED_FG;
   m_crBGEnabled = ENABLED_BG;
   m_crFGDisabled = DISABLED_FG;
   m_crBGDisabled = DISABLED_BG;

   // to changes the colors dynamic
   m_pbrushEnabled = new CBrush;
   m_pbrushDisabled = new CBrush;
   m_pbrushEnabled->CreateSolidBrush(m_crBGEnabled);
   m_pbrushDisabled->CreateSolidBrush(m_crBGDisabled);
}

CMyComboBox::~CMyComboBox()
{
   delete m_pbrushEnabled;
   delete m_pbrushDisabled;
}

BEGIN_MESSAGE_MAP(CMyComboBox, CComboBox)
   //{{AFX_MSG_MAP(CMyComboBox)
   ON_WM_CTLCOLOR()
   ON_WM_DESTROY()
   ON_WM_CTLCOLOR_REFLECT()
   ON_WM_ENABLE()
   //}}AFX_MSG_MAP
END_MESSAGE_MAP()

// CMyComboBox message handlers

HBRUSH CMyComboBox::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
   // Setting color in en- disabled mode
   if(IsWindowEnabled())
   {
      pDC->SetTextColor(m_crFGEnabled);
      pDC->SetBkColor(m_crBGEnabled);
      return *m_pbrushEnabled;
   }
   else
   {
      pDC->SetTextColor(m_crFGDisabled);
      pDC->SetBkColor(m_crBGDisabled);
      return *m_pbrushDisabled;
   }
}

HBRUSH CMyComboBox::CtlColor(CDC* pDC, UINT nCtlColor) 
{
   // Setting color in en- disabled mode
   if(IsWindowEnabled())
   {
      pDC->SetTextColor(m_crFGEnabled);
      pDC->SetBkColor(m_crBGEnabled);
      return *m_pbrushEnabled;
   }
   else
   {
      pDC->SetTextColor(m_crFGDisabled);
      pDC->SetBkColor(m_crBGDisabled);
      return *m_pbrushDisabled;
   }
   return NULL;
}

void CMyComboBox::OnEnable(BOOL bEnable) 
{
   CComboBox::OnEnable(bEnable);

   // TODO: Add your message handler code here

   // The first child is the CEdit
   CEdit* pComboEdit=(CEdit*)GetWindow(GW_CHILD);

   // Setting the edit ctrl always to enable
   pComboEdit->EnableWindow(TRUE);
   pComboEdit->SetReadOnly(!(m_bEditable && bEnable));
   Invalidate();
}

void CMyComboBox::SetReadOnly(bool bReadOnly)
{
   m_bEditable = !bReadOnly;

   // The first child is the CEdit
   CEdit* pComboEdit=(CEdit*)GetWindow(GW_CHILD);

   // Setting the edit ctrl always to enable
   pComboEdit->EnableWindow(TRUE);
   pComboEdit->SetReadOnly(!(m_bEditable && IsWindowEnabled()));
   Invalidate();
}

void CMyComboBox::SetEnabledColor(COLORREF crFG, COLORREF crBG)
{
   // Setting the colors and the brush
   m_crFGEnabled = crFG;
   m_crBGEnabled = crBG;
   delete m_pbrushEnabled;
   m_pbrushEnabled = new CBrush;
   m_pbrushEnabled->CreateSolidBrush(m_crBGEnabled);
}

void CMyComboBox::SetDisabledColor(COLORREF crFG, COLORREF crBG)
{
   // Setting the colors and the brush
   m_crFGDisabled = crFG;
   m_crBGDisabled = crBG;
   delete m_pbrushDisabled;
   m_pbrushDisabled = new CBrush;
   m_pbrushDisabled->CreateSolidBrush(m_crBGDisabled);
}



Comments

  • Combo Box

    Posted by Legacy on 03/06/2003 12:00am

    Originally posted by: Trivedi Purvesh Niranjan

    Dear Sir/Miss,

    I am a student Studying in SYBSC Computer Science. Actually I needed a code for drop down menu for my project of screensaver. But the important point here is i want in C programming language.

    So if you can help me in getting my code in C language i will be very Thankfull to you.

    Please contact on my Email Adrress is given below

    Email Address: purvesht@hotmail.com

    hehehe

    Your's Sincerly,
    TRIVEDI PURVESH

    Reply
  • good

    Posted by Legacy on 04/30/1999 12:00am

    Originally posted by: leedujin

    good!!!!! A++

    Reply
  • Is there any way so that I can disable the drop down button alone.....

    Posted by Legacy on 02/10/1999 12:00am

    Originally posted by: Ganesh Kumar B V

    Hi,
    
    

    I've a peculiar requirement. Like the Readonly() feature of the editbox of the
    combo box, I need to disable the dropdown button alone. Can any of you tell
    how this can be done?

    I tried using the GetWindow function as follows

    CEdit* edt = (CEdit*) m_listControl.GetWindow(GW_CHILD);
    /* This gets the first child window in the listcontrol */


    /* I presume that the button might also be a child window and tried the following*/

    CButton* btn = (CButton*) ( (CWnd*)edt->GetWindow(GW_HWNDFIRST));
    /* Logically this should return the First sibiling of the edit window */
    /* But this didn't work. I triend with GW_HWNDLAST, GW_HWNDPREV, GW_HWNDNEXT*/
    /* Nothing works out */

    Any help???

    Thanks in advance,
    Ganesh.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: August 14, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Data protection has long been considered "overhead" by many organizations in the past, many chalking it up to an insurance policy or an extended warranty you may never use. The realities of today make data protection a must-have, as we live in a data driven society. The digital assets we create, share, and collaborate with others on must be managed and protected for many purposes. Check out this upcoming eSeminar and join eVault Chief Technology …

  • Protecting business operations means shifting the priorities around availability from disaster recovery to business continuity. Enterprises are shifting their focus from recovery from a disaster to preventing the disaster in the first place. With this change in mindset, disaster recovery is no longer the first line of defense; the organizations with a smarter business continuity practice are less impacted when disasters strike. This SmartSelect will provide insight to help guide your enterprise toward better …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds