版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、 用自定义的CCheckComboBox类做带复选框的下拉列表控件 1、添加Combo Box2.、改属性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.AddStrin
2、g(_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_6
3、6750D93_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 specif
4、ied 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(LPDRAWITEMSTRU
5、CT 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, LPAR
6、AM 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
7、 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 */ / CheckCom
8、boBox.cpp / / Written by Magnus Egelberg (magnus.egelberglundalogik.se) / / Copyright (C) 1999, Lundalogik AB, Sweden. All rights reserved. / / #include stdafx.h / #include CheckCombo.h #include CheckComboBox.h #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE = _FILE_; #end
9、if 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
10、_DROPDOWN, OnDropDown) /AFX_MSG_MAP END_MESSAGE_MAP() / / The subclassed COMBOLBOX message handler / extern C LRESULT FAR PASCAL ComboBoxListBoxProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam) switch (nMsg) case WM_RBUTTONDOWN: / If you want to select all/unselect all using the / right butto
11、n, remove this ifdef. Personally, I dont really like it #if FALSE if (m_pComboBox != 0) INT nCount = m_pComboBox-GetCount(); INT nSelCount = 0; for (INT i = 0; i GetCheck(i) nSelCount+; m_pComboBox-SelectAll(nSelCount != nCount); / Make sure to invalidate this window as well InvalidateRect(hWnd, 0,
12、FALSE); m_pComboBox-GetParent()-SendMessage(WM_COMMAND, MAKELONG(GetWindowLong(m_pComboBox-m_hWnd, GWL_ID), CBN_SELCHANGE), (LPARAM)m_pComboBox-m_hWnd); #endif break; / Make the combobox always return -1 as the current selection. This / causes the lpDrawItemStruct-itemID in DrawItem() to be -1 / whe
13、n the always-visible-portion of the combo is drawn case LB_GETCURSEL: return -1; case WM_CHAR: if (wParam = VK_SPACE) / Get the current selection INT nIndex = CallWindowProcA(m_pWndProc, hWnd, LB_GETCURSEL, wParam, lParam); CRect rcItem; SendMessage(hWnd, LB_GETITEMRECT, nIndex, (LONG)(VOID *)&rcIte
14、m); InvalidateRect(hWnd, rcItem, FALSE); / Invert the check mark m_pComboBox-SetCheck(nIndex, !m_pComboBox-GetCheck(nIndex); / Notify that selection has changed m_pComboBox-GetParent()-SendMessage(WM_COMMAND, MAKELONG(GetWindowLong(m_pComboBox-m_hWnd, GWL_ID), CBN_SELCHANGE), (LPARAM)m_pComboBox-m_h
15、Wnd); return 0; break; case WM_LBUTTONDOWN: CRect rcClient; GetClientRect(hWnd, rcClient); CPoint pt; pt.x = LOWORD(lParam); pt.y = HIWORD(lParam); if (PtInRect(rcClient, pt) INT nItemHeight = SendMessage(hWnd, LB_GETITEMHEIGHT, 0, 0); INT nTopIndex = SendMessage(hWnd, LB_GETTOPINDEX, 0, 0); / Compu
16、te which index to check/uncheck INT nIndex = nTopIndex + pt.y / nItemHeight; CRect rcItem; SendMessage(hWnd, LB_GETITEMRECT, nIndex, (LONG)(VOID *)&rcItem); if (PtInRect(rcItem, pt) / Invalidate this window InvalidateRect(hWnd, rcItem, FALSE); m_pComboBox-SetCheck(nIndex, !m_pComboBox-GetCheck(nInde
17、x); / Notify that selection has changed m_pComboBox-GetParent()-SendMessage(WM_COMMAND, MAKELONG(GetWindowLong(m_pComboBox-m_hWnd, GWL_ID), CBN_SELCHANGE), (LPARAM)m_pComboBox-m_hWnd); / Do the default handling now (such as close the popup / window when clicked outside) break; case WM_LBUTTONUP: / D
18、ont do anything here. This causes the combobox popup / windows to remain open after a selection has been made return 0; return CallWindowProc(m_pWndProc, hWnd, nMsg, wParam, lParam); CCheckComboBox:CCheckComboBox() m_hListBox = 0; m_bTextUpdated = FALSE; m_bItemHeightSet = FALSE; CCheckComboBox:CChe
19、ckComboBox() BOOL CCheckComboBox:Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID) / Remove the CBS_SIMPLE and CBS_DROPDOWN styles and add the one Im designed for dwStyle &= 0xF; dwStyle |= CBS_DROPDOWNLIST; / Make sure to use the CBS_OWNERDRAWVARIABLE style dwStyle |= CBS_OWNERDRA
20、WVARIABLE; / Use default strings. We need the itemdata to store checkmarks dwStyle |= CBS_HASSTRINGS; return CComboBox:Create(dwStyle, rect, pParentWnd, nID); LRESULT CCheckComboBox:OnCtlColorListBox(WPARAM wParam, LPARAM lParam) / If the listbox hasnt been subclassed yet, do so. if (m_hListBox = 0)
21、 HWND hWnd = (HWND)lParam; if (hWnd != 0 & hWnd != m_hWnd) / Save the listbox handle m_hListBox = hWnd; / Do the subclassing m_pWndProc = (WNDPROC)GetWindowLong(m_hListBox, GWL_WNDPROC); SetWindowLong(m_hListBox, GWL_WNDPROC, (LONG)ComboBoxListBoxProc); return DefWindowProc(WM_CTLCOLORLISTBOX, wPara
22、m, lParam); void CCheckComboBox:DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) HDC dc = lpDrawItemStruct-hDC; CRect rcBitmap = lpDrawItemStruct-rcItem; CRect rcText = lpDrawItemStruct-rcItem; CString strText; / 0 - No check, 1 - Empty check, 2 - Checked INT nCheck = 0; / Check if we are drawing the sta
23、tic portion of the combobox if (LONG)lpDrawItemStruct-itemID itemID, strText); nCheck = 1 + (GetItemData(lpDrawItemStruct-itemID) != 0); TEXTMETRIC metrics; GetTextMetrics(dc, &metrics); rcBitmap.left = 0; rcBitmap.right = rcBitmap.left + metrics.tmHeight + metrics.tmExternalLeading + 6; rcBitmap.to
24、p += 1; rcBitmap.bottom -= 1; rcText.left = rcBitmap.right; if (nCheck 0) SetBkColor(dc, GetSysColor(COLOR_WINDOW); SetTextColor(dc, GetSysColor(COLOR_WINDOWTEXT); UINT nState = DFCS_BUTTONCHECK; if (nCheck 1) nState |= DFCS_CHECKED; / Draw the checkmark using DrawFrameControl DrawFrameControl(dc, r
25、cBitmap, 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); / Erase and draw ExtTextOut(dc,
26、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); void CCheckComboBox:MeasureItem(LPMEASUREITEMSTRU
27、CT lpMeasureItemStruct) CClientDC dc(this); CFont *pFont = dc.SelectObject(GetFont(); if (pFont != 0) TEXTMETRIC metrics; dc.GetTextMetrics(&metrics); lpMeasureItemStruct-itemHeight = metrics.tmHeight + metrics.tmExternalLeading; / An extra height of 2 looks good I think. / Otherwise the list looks
28、a bit crowded. lpMeasureItemStruct-itemHeight += 2; / This is needed since the WM_MEASUREITEM message is sent before / MFC hooks everything up if used in i dialog. So adjust the / static portion of the combo box now if (!m_bItemHeightSet) m_bItemHeightSet = TRUE; SetItemHeight(-1, lpMeasureItemStruc
29、t-itemHeight); dc.SelectObject(pFont); / / Make sure the combobox window handle is updated since / there may be many CCheckComboBox windows active / void CCheckComboBox:OnDropDown() m_pComboBox = this; / / Selects/unselects all items in the list / void CCheckComboBox:SelectAll(BOOL bCheck) INT nCoun
30、t = GetCount(); for (INT i = 0; i nCount; i+) SetCheck(i, bCheck); / / By adding this message handler, we may use CWnd:GetText() / LRESULT CCheckComboBox:OnGetText(WPARAM wParam, LPARAM lParam) / Make sure the text is updated RecalcText(); if (lParam = 0) return 0; / Copy the fake window text lstrcpyn(LPWSTR)lParam, m_strText, (INT)wParam); return m_strText.GetLength(); / / By adding this message handler, we may use CWnd:GetTextLength() / LRESULT CCheckComboBox:OnGetTextLength(WPARAM, LPARAM) / Make sure the text is updated RecalcText(); return m_strText.GetLength(); / / This routine
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 专业加盟合作协议(2024版)细则版
- 2025年茶园租赁合同示范文本8篇
- 2024版轿车租借合同:全面保障合同条款版
- 2025年度柴油发电机及配件全球采购合同范本4篇
- 2024年04月陕西西安银行金融市场及资产管理业务人才招考笔试历年参考题库附带答案详解
- 专业空气能热泵热水器安装工程协议规范文本版B版
- 专业设备采购销售协议:2024版细则版A版
- 2025年度绿色建筑场调研与投资评估服务合同4篇
- 二零二五年度瓷砖行业供应链管理合同3篇
- 2025年环保设备产品区域代理合同4篇
- GB/T 18476-2001流体输送用聚烯烃管材耐裂纹扩展的测定切口管材裂纹慢速增长的试验方法(切口试验)
- GA 1551.5-2019石油石化系统治安反恐防范要求第5部分:运输企业
- 拘留所教育课件02
- 冲压生产的品质保障
- 《肾脏的结构和功能》课件
- 2023年湖南联通校园招聘笔试题库及答案解析
- 上海市徐汇区、金山区、松江区2023届高一上数学期末统考试题含解析
- 护士事业单位工作人员年度考核登记表
- 天津市新版就业、劳动合同登记名册
- 产科操作技术规范范本
- 人教版八年级上册地理全册单元测试卷(含期中期末试卷及答案)
评论
0/150
提交评论