先看效果图
自定义类继承CComboBox头文件
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CCheckComboBox : public CComboBox
{
public:
CCheckComboBox();
virtual ~CCheckComboBox();
BOOL Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID);
// Selects all/unselects the specified item
INT SetCheck(INT nIndex, BOOL bFlag);
// Returns checked state
BOOL GetCheck(INT nIndex);
// Selects all/unselects all
void SelectAll(BOOL bCheck = TRUE);
protected:
// ClassWizard generated virtual function overrides
//{
{AFX_VIRTUAL(CCheckComboBox)
protected:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
virtual void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct);
//}}AFX_VIRTUAL
//{
{AFX_MSG(CCheckComboBox)
afx_msg LRESULT OnCtlColorListBox(WPARAM wParam, LPARAM lParam);
afx_msg LRESULT OnGetText(WPARAM wParam, LPARAM lParam);
afx_msg LRESULT OnGetTextLength(WPARAM wParam, LPARAM lParam);
afx_msg void OnDropDown();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
protected:
// Routine to update the text
void RecalcText();
// The subclassed COMBOLBOX window (notice the 'L')
HWND m_hListBox;
// The string containing the text to display
CString m_strText;
BOOL m_bTextUpdated;
// A flag used in MeasureItem, see comments there
BOOL m_bItemHeightSet;
};
实现
#include "pch.h"
#include "CCheckComboBox.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
static WNDPROC m_pWndProc = 0;
static CCheckComboBox* m_pComboBox = 0;
BEGIN_MESSAGE_MAP(CCheckComboBox, CComboBox)
//{
{AFX_MSG_MAP(CCheckComboBox)
ON_MESSAGE(WM_CTLCOLORLISTBOX, OnCtlColorListBox)
ON_MESSAGE(WM_GETTEXT, OnGetText)
ON_MESSAGE(WM_GETTEXTLENGTH, OnGetTextLength)
ON_CONTROL_REFLECT(CBN_DROPDOWN, OnDropDown)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
//
// The subclassed COMBOLBOX message handler
//
extern "C" LRESULT FAR PASCAL ComboBoxListBoxProc(HWND hWnd, UINT nMsg, WPAR