Click here to Skip to main content
15,892,298 members
Articles / Desktop Programming / MFC

A Read-Only Combo with Selectable Text

Rate me:
Please Sign up or sign in to vote.
4.52/5 (14 votes)
30 Nov 20057 min read 102.5K   2.7K   32  
A combo box which behaves as a fully disabled combo box except that it allows you to select the text.
#ifndef ReadOnlyComboHandler_h
#define ReadOnlyComboHandler_h 1

/*-----------------------------------------------------------------------
| ReadOnlyComboHandler.h: a plug-in handler to make a combo read-only.
|
| Version 1.1 20 November 2004. 
| - Converted combo class into handler, added support for drop-list type
| Version 1.2 24 November 2004. 
| - Subsumed combo edit class functionality into handler.
| - Added notification for parent of changed HWND
| - Fixed minor bug with border in XP
| Version 1.3
| - Altered combo arrow drawing to simply mask arrow when read-only.
| - Figured out a much easier (and more accurate) way to get the arrow
|	 dimensions (duh!).
| - Added "drop-list" mode to implement drop-list combos with drop-down 
|   combos and thus allow selection.
|
| Copyright David Pritchard 2005. davidpritchard@ctv.es
|
| You can use this class freely, but please keep my ego happy 
| by leaving this comment in place.
|
------------------------------------------------------------------------*/

#define NEW_READONLY_COMBO

static LRESULT CALLBACK		NewComboProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam);
static LRESULT CALLBACK		NewComboEditProc ( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );


class CReadOnlyComboHandler 
{
  public:
      CReadOnlyComboHandler();

      virtual ~CReadOnlyComboHandler();

      bool Init (IN const HWND hWndCombo);
      void SetReadOnly (IN const bool bReadOnly);
      void OnNcDestroy (WPARAM wParam, LPARAM lParam);
      void OnPaint (WPARAM wParam, LPARAM lParam);
      void OnPrint (WPARAM wParam, LPARAM lParam);
      WNDPROC GetOldWndProc ();
      WNDPROC GetOldEditWndProc ();
      void OnNcDestroyComboEdit (WPARAM wParam, LPARAM lParam);
      void OnKeyDownInEdit (UINT message,WPARAM wParam, LPARAM lParam);
      void OnClipboardEdit (UINT message,WPARAM wParam, LPARAM lParam);
      bool IsReadOnly ();
      bool SubclassComboEdit (IN CEdit* pWndEdit);
      bool UnsubclassComboEdit (IN CEdit* pWndEdit);
      bool UnhookEditProc ();
      bool HookEditProc ();
		int  GetEditHeight();
		void SetDropListMode(IN const bool bSet);

	  void OnLeftClick(UINT message,WPARAM wParam,LPARAM lParam);

  protected:

      void DrawDisabledDropArrow (CDC* pDC);
      bool FindAndStoreComboEdit ();
      HWND GetComboEditWindow ();
      HWND GetComboChildWindowFromClass (IN LPCTSTR sClassName);
      bool HasEditControl ();
      bool RemoveCustomEditWndProc ();
      bool RemoveCustomComboWndProc ();
      bool AttachCustomEditWndProc ();
      bool AttachCustomComboWndProc ();
      CRect GetComboArrowRect ();
      void MaskComboArrow (CDC& dc);

  private:
//      CComboBox* m_pCombo;
      HWND		m_hWndCombo;
      WNDPROC	m_wndProcOld;
      WNDPROC	m_wndProcEditOld;
      HWND		m_hWndEdit;
      bool		m_bReadOnly;
		bool		m_bDropListMode;

  private:
};

#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
Spain Spain
I'm originally from Leek, Staffordshire in the UK, but I now work as a C++/MFC developer in Madrid, Spain.

I followed an erratic study/career path from German to a PhD in something resembling political science and linguistics, eventually ending up in IT.

I'm still finding bustling streets, warm nights, beer and vitamin D a pretty heady combination.

Comments and Discussions