复选框的下拉列表.docx
- 文档编号:15370096
- 上传时间:2023-07-03
- 格式:DOCX
- 页数:13
- 大小:31.61KB
复选框的下拉列表.docx
《复选框的下拉列表.docx》由会员分享,可在线阅读,更多相关《复选框的下拉列表.docx(13页珍藏版)》请在冰点文库上搜索。
复选框的下拉列表
用自定义的CCheckComboBox类做带复选框的下拉列表
1、添加ComboBox控件
2.、改属性Style:
DropList ; OwnerDraw:
Fixed; Hasstring:
true;Sort:
false
3、头文件中添加:
CCheckComboBoxm_comboBox;
4、DoDataExchange(CDataExchange*pDX)中添加:
DDX_Control(pDX,IDC_COMBO1,m_comboBox);
5、对话框初始化时添加
m_comboBox.AddString(_T("1"));//加入项目
m_comboBox.AddString(_T("2"));
m_comboBox.AddString(_T("3"));
m_comboBox.AddString(_T("4"));
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_)
#defineAFX_CHECKCOMBOBOX_H__66750D93_95DB_11D3_9325_444553540000__INCLUDED_
#if_MSC_VER>1000
#pragmaonce
#endif//_MSC_VER>1000
classCCheckComboBox:
publicCComboBox
{
public:
CCheckComboBox();
virtual~CCheckComboBox();
BOOLCreate(DWORDdwStyle,constRECT&rect,CWnd*pParentWnd,UINTnID);
//Selectsall/unselectsthespecifieditem
INTSetCheck(INTnIndex,BOOLbFlag);
//Returnscheckedstate
BOOLGetCheck(INTnIndex);
//Selectsall/unselectsall
voidSelectAll(BOOLbCheck=TRUE);
protected:
//ClassWizardgeneratedvirtualfunctionoverrides
//{{AFX_VIRTUAL(CCheckComboBox)
protected:
virtualvoidDrawItem(LPDRAWITEMSTRUCTlpDrawItemStruct);
virtualvoidMeasureItem(LPMEASUREITEMSTRUCTlpMeasureItemStruct);
//}}AFX_VIRTUAL
//{{AFX_MSG(CCheckComboBox)
afx_msgLRESULTOnCtlColorListBox(WPARAMwParam,LPARAMlParam);
afx_msgLRESULTOnGetText(WPARAMwParam,LPARAMlParam);
afx_msgLRESULTOnGetTextLength(WPARAMwParam,LPARAMlParam);
afx_msgvoidOnDropDown();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
public:
CStringm_strText;
protected:
//Routinetoupdatethetext
voidRecalcText();
//ThesubclassedCOMBOLBOXwindow(noticethe'L')
HWNDm_hListBox;
//Thestringcontainingthetexttodisplay
BOOLm_bTextUpdated;
//AflagusedinMeasureItem,seecommentsthere
BOOLm_bItemHeightSet;
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
//MicrosoftVisualC++willinsertadditionaldeclarationsimmediatelybeforethepreviousline.
#endif//!
defined(AFX_CHECKCOMBOBOX_H__66750D93_95DB_11D3_9325_444553540000__INCLUDED_)
/** CheckComboBox.cpp **/
//CheckComboBox.cpp
//
//WrittenbyMagnusEgelberg(magnus.egelberg@lundalogik.se)
//
//Copyright(C)1999,LundalogikAB,Sweden.Allrightsreserved.
//
//
#include"stdafx.h"
//#include"CheckCombo.h"
#include"CheckComboBox.h"
#ifdef_DEBUG
#definenewDEBUG_NEW
#undefTHIS_FILE
staticcharTHIS_FILE[]=__FILE__;
#endif
staticWNDPROCm_pWndProc=0;
staticCCheckComboBox*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()
//
//ThesubclassedCOMBOLBOXmessagehandler
//
extern"C"LRESULTFARPASCALComboBoxListBoxProc(HWNDhWnd,UINTnMsg,WPARAMwParam,LPARAMlParam)
{
switch(nMsg){
caseWM_RBUTTONDOWN:
{
//Ifyouwanttoselectall/unselectallusingthe
//rightbutton,removethisifdef.Personally,Idon'treallylikeit
#ifFALSE
if(m_pComboBox!
=0){
INTnCount=m_pComboBox->GetCount();
INTnSelCount=0;
for(INTi=0;i if(m_pComboBox->GetCheck(i)) nSelCount++; } m_pComboBox->SelectAll(nSelCount! =nCount); //Makesuretoinvalidatethiswindowaswell InvalidateRect(hWnd,0,FALSE); m_pComboBox->GetParent()->SendMessage(WM_COMMAND,MAKELONG(GetWindowLong(m_pComboBox->m_hWnd,GWL_ID),CBN_SELCHANGE),(LPARAM)m_pComboBox->m_hWnd); } #endif break; } //Makethecomboboxalwaysreturn-1asthecurrentselection.This //causesthelpDrawItemStruct->itemIDinDrawItem()tobe-1 //whenthealways-visible-portionofthecomboisdrawn caseLB_GETCURSEL: { return-1; } caseWM_CHAR: { if(wParam==VK_SPACE){ //Getthecurrentselection INTnIndex=CallWindowProcA(m_pWndProc,hWnd,LB_GETCURSEL,wParam,lParam); CRectrcItem; SendMessage(hWnd,LB_GETITEMRECT,nIndex,(LONG)(VOID*)&rcItem); InvalidateRect(hWnd,rcItem,FALSE); //Invertthecheckmark m_pComboBox->SetCheck(nIndex,! m_pComboBox->GetCheck(nIndex)); //Notifythatselectionhaschanged m_pComboBox->GetParent()->SendMessage(WM_COMMAND,MAKELONG(GetWindowLong(m_pComboBox->m_hWnd,GWL_ID),CBN_SELCHANGE),(LPARAM)m_pComboBox->m_hWnd); return0; } break; } caseWM_LBUTTONDOWN: { CRectrcClient; GetClientRect(hWnd,rcClient); CPointpt; pt.x=LOWORD(lParam); pt.y=HIWORD(lParam); if(PtInRect(rcClient,pt)){ INTnItemHeight=SendMessage(hWnd,LB_GETITEMHEIGHT,0,0); INTnTopIndex =SendMessage(hWnd,LB_GETTOPINDEX,0,0); //Computewhichindextocheck/uncheck INTnIndex=nTopIndex+pt.y/nItemHeight; CRectrcItem; SendMessage(hWnd,LB_GETITEMRECT,nIndex,(LONG)(VOID*)&rcItem); if(PtInRect(rcItem,pt)){ //Invalidatethiswindow InvalidateRect(hWnd,rcItem,FALSE); m_pComboBox->SetCheck(nIndex,! m_pComboBox->GetCheck(nIndex)); //Notifythatselectionhaschanged m_pComboBox->GetParent()->SendMessage(WM_COMMAND,MAKELONG(GetWindowLong(m_pComboBox->m_hWnd,GWL_ID),CBN_SELCHANGE),(LPARAM)m_pComboBox->m_hWnd); } } //Dothedefaulthandlingnow(suchasclosethepopup //windowwhenclickedoutside) break; } caseWM_LBUTTONUP: { //Don'tdoanythinghere.Thiscausesthecomboboxpopup //windowstoremainopenafteraselectionhasbeenmade return0; } } returnCallWindowProc(m_pWndProc,hWnd,nMsg,wParam,lParam); } CCheckComboBox: : CCheckComboBox() { m_hListBox =0; m_bTextUpdated =FALSE; m_bItemHeightSet=FALSE; } CCheckComboBox: : ~CCheckComboBox() { } BOOLCCheckComboBox: : Create(DWORDdwStyle,constRECT&rect,CWnd*pParentWnd,UINTnID) { //RemovetheCBS_SIMPLEandCBS_DROPDOWNstylesandaddtheoneI'mdesignedfor dwStyle&=~0xF; dwStyle|=CBS_DROPDOWNLIST; //MakesuretousetheCBS_OWNERDRAWVARIABLEstyle dwStyle|=CBS_OWNERDRAWVARIABLE; //Usedefaultstrings.Weneedtheitemdatatostorecheckmarks dwStyle|=CBS_HASSTRINGS; returnCComboBox: : Create(dwStyle,rect,pParentWnd,nID); } LRESULTCCheckComboBox: : OnCtlColorListBox(WPARAMwParam,LPARAMlParam) { //Ifthelistboxhasn'tbeensubclassedyet,doso... if(m_hListBox==0){ HWNDhWnd=(HWND)lParam; if(hWnd! =0&&hWnd! =m_hWnd){ //Savethelistboxhandle m_hListBox=hWnd; //Dothesubclassing m_pWndProc=(WNDPROC)GetWindowLong(m_hListBox,GWL_WNDPROC); SetWindowLong(m_hListBox,GWL_WNDPROC,(LONG)ComboBoxListBoxProc); } } returnDefWindowProc(WM_CTLCOLORLISTBOX,wParam,lParam); } voidCCheckComboBox: : DrawItem(LPDRAWITEMSTRUCTlpDrawItemStruct) { HDCdc=lpDrawItemStruct->hDC; CRectrcBitmap=lpDrawItemStruct->rcItem; CRectrcText =lpDrawItemStruct->rcItem; CStringstrText; //0-Nocheck,1-Emptycheck,2-Checked INTnCheck=0; //Checkifwearedrawingthestaticportionofthecombobox if((LONG)lpDrawItemStruct->itemID<0){ //Makesurethem_strTextmemberisupdated RecalcText(); //Getthetext strText=m_strText; //Don'tdrawanyboxesonthisitem nCheck=0; } //Otherwiseitisoneoftheitems else{ GetLBText(lpDrawItemStruct->itemID,strText); nCheck=1+(GetItemData(lpDrawItemStruct->itemID)! =0); TEXTMETRICmetrics; GetTextMetrics(dc,&metrics); rcBitmap.left =0; rcBitmap.right =rcBitmap.left+metrics.tmHeight+metrics.tmExternalLeading+6; rcBitmap.top +=1; rcBitmap.bottom-=1; rcText.left=rcBitmap.right; } if(nCheck>0){ SetBkColor(dc,GetSysColor(COLOR_WINDOW)); SetTextColor(dc,GetSysColor(COLOR_WINDOWTEXT)); UINTnState=DFCS_BUTTONCHECK; if(nCheck>1) nState|=DFCS_CHECKED; //DrawthecheckmarkusingDrawFrameControl DrawFrameControl(dc,rcBitmap,DFC_BUTTON,nState); } if(lpDrawItemStruct->itemState&ODS_SELECTED){ SetBkColor(dc,GetSysColor(COLOR_HIGHLIGHT)); SetTextColor(dc,GetSysColor(COLOR_HIGHLIGHTTEXT)); } else{ SetBkColor(dc,GetSysColor(COLOR_WINDOW)); SetTextColor(dc,GetSysColor(COLOR_WINDOWTEXT)); } //Eraseanddraw ExtTextOut(dc,0,0,ETO_OPAQUE,&rcText,0,0,0); DrawText(dc,''+strText,strText.GetLength()+1,&rcText,DT_SINGLELINE|DT_VCENTER|DT_END_ELLIPSIS); if((lpDrawItemStruct->itemState&(ODS_FOCUS|ODS_SELECTED))==(ODS_FOCUS|ODS_SELECTED)) DrawFocusRect(dc,&rcText); } voidCCheckComboBox: : MeasureItem(LPMEASUREITEMSTRUCTlpMeasureItemStruct) { CClientDCdc(this); CFont*pFont=dc.SelectObject(GetFont()); if(pFont! =0){ TEXTMETRICmetrics; dc.GetTextMetrics(&metrics); lpMeasureItemStruct->itemHeight=metrics.tmHeight+metrics.tmExternalLeading; //Anextraheightof2looksgoodIthink. //Otherwisethelistlooksabitcrowded... lpMeasureItemStruct->itemHeight+=2; //ThisisneededsincetheWM_MEASUREITEMmessageissentbefore //MFChookseverythingupifusedinidialog.Soadjustthe //staticportionofthecomboboxnow if(! m_bItemHeightSet){ m_bItemHeightSet=TRUE; SetItemHeight(-1,lpMeasureItemStruct->itemHeight); } dc.SelectObject(pFont); } } // //Makesurethecomboboxwin
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 复选 下拉 列表