当前位置:首页 > 带复选框的下拉列表
用自定义的CCheckComboBox类做带复选框的下拉列表 1、添加Combo Box控件
2.、改属性Style:DropList ; Owner Draw:Fixed ; Has string:true ;Sort:false 3、头文件中添加:
CCheckComboBox m_comboBox;
4、DoDataExchange(CDataExchange* pDX)中添加: DDX_Control(pDX, IDC_COMBO1, m_comboBox); 5、对话框初始化时添加
m_comboBox.AddString(_T(\加入项目 m_comboBox.AddString(_T(\ m_comboBox.AddString(_T(\ m_comboBox.AddString(_T(\
m_comboBox.SetCheck(0, TRUE);//设置选择状态 m_comboBox.SetCheck(1, FALSE); m_comboBox.SetCheck(2, TRUE); m_comboBox.SetCheck(3, TRUE);
#if !defined(AFX_CHECKCOMBOBOX_H__66750D93_95DB_11D3_9325_444553540000__INCLUDED_)
#define AFX_CHECKCOMBOBOX_H__66750D93_95DB_11D3_9325_444553540000__INCLUDED_ #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() public:
CString m_strText; 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 BOOL m_bTextUpdated;
// A flag used in MeasureItem, see comments there BOOL m_bItemHeightSet; };
///////////////////////////////////////////////////////////////////////////// //{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif
// !defined(AFX_CHECKCOMBOBOX_H__66750D93_95DB_11D3_9325_444553540000__INCLUDED_)
/** CheckComboBox.cpp **/
// CheckComboBox.cpp //
// Written by Magnus Egelberg (magnus.egelberg@lundalogik.se) //
// Copyright (C) 1999, Lundalogik AB, Sweden. All rights reserved. // //
#include \
// #include \#include \
#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 \wParam, LPARAM lParam) {
switch (nMsg) {
case WM_RBUTTONDOWN: {
// If you want to select all/unselect all using the
// right button, remove this ifdef. Personally, I don't really like it #if FALSE
if (m_pComboBox != 0) {
INT nCount = m_pComboBox->GetCount(); INT nSelCount = 0;
for (INT i = 0; i < nCount; i++) { if (m_pComboBox->GetCheck(i)) nSelCount++; }
m_pComboBox->SelectAll(nSelCount != nCount); // Make sure to invalidate this window as well InvalidateRect(hWnd, 0, FALSE);
m_pComboBox->GetParent()->SendMessage(WM_COMMAND,
共分享92篇相关文档