Select the icon of ComboBox

xiaoxiao2021-03-06  18

Icon Select the combo box, I want everyone to see in the QQ personal settings, there can be a lot of avatars inside, you can choose one anytime. Here is a simple step of building a combination box for selecting icons: 1. This step is clear, you have to add .cpp and .h files to your project. 2. Add a combination of a combo box in your project, the style of the combo box is on the default setting. 3. Define a control variable using the VC's class wizard to the combo box. 4. In the header file, use CCionCombox instead of CCOMBOBOX, so you can truly use the following. After the above steps, you can use the following functions to insert, select, and delete icons. public: virtual int addicon (lpctstr lpsziconfilename); virtual int inserticon (int nindex, lpctstr lpsziconfilename); virtual int selecticon (lpctstr lpsziconfilename); virtual int selecticon (int nindex); virtual int deleteicon (lpctstr lpsziconfilename); virtual int deleteicon (int Nindex); The following function is to display the icon. Protected: Virtual Void onoutputicon (LPDrawItemstruct LPDIS, Bool Bselected); You can also set the size through the following function to accommodate the icon size difference. m_cbmyiconcomboBox.setitemheight (-1, m_cbmyiconcomboBox.m_sizeicon.cy 6); Of course, there are other features waiting for you to discover, if you really like this function, you still go to take all the code, this will Your programming is more beneficial, because I have limited my horizontal, I can't extend it, I hope that readers can extend it.

Here is the .h and .cpp file: // header file // iConcomboBox.h: header file / // # ifndef _iconcomboBox_h_ # define _ICONCOMBOX_H _ //// /// CICONCOMBOX / / / / / / // CopyRight (c) Joel Wahlberg Enator Networks 1997 //// joel.wahlberg@enator.se //// //// Class CCIMBOBOX: PUBLIC CCOMBOX {// Contruction / Destructionpublic: ciconcomboBox (); virtual ~ ciconcomboBox (); / / Attributespublic: CSize m_sizeIcon; // Operationspublic: virtual int AddIcon (LPCTSTR lpszIconFile); virtual int insertIcon (int nIndex, LPCTSTR lpszIconFile); virtual int SelectIcon (LPCTSTR lpszIconFile); virtual int SelectIcon (int nIndex); virtual int DeleteIcon (LPCTSTR Lpsziconfile; Virtual Int deleteicon; // ImplementProtace: Virtual Void onoutputCuticon (LPDrawItemStruct LPDIS, BOOL BSELECTED); // Overr ides virtual int AddString (LPCTSTR lpszString); virtual int InsertString (int nIndex, LPCTSTR lpszString); virtual int DeleteString (int nIndex); virtual void MeasureItem (LPMEASUREITEMSTRUCT lpMIS); virtual void DrawItem (LPDRAWITEMSTRUCT lpDIS);}; # endif // _ICONCOMBOBOX_H _ // The following is a source #include "stdafx.h" #include "IconComboBox.h" #ifdef _DEBUG # define new DEBUG_NEW # undef THIS_FILEstatic char BASED_CODE THIS_FILE [] = __FILE __; # endif //// //// CIconComboBox /// / / // CopyRight (c) Joel Wahlberg Enator Networks 1997 //// joel.wahlberg @

Entor.se //// ////ConConComboBox :: ciconcombobox () {m_sizeicon.cx = :: getSystemMetrics; m_sizeicon.cy = :: getSystemMetrics;} ciconcombobox :: ~ ciconcombox () {} INT CICONCOMBOBOX :: Addicon (LPCTSTR LPSZICONFILE) {HiCon HiCon = :: Extracticon (AfxGetInstanceHandle (), LPSZICONFILE, 0); IF (Hicon == (Hicon) 1 || Hicon == NULL) Return CB_ERR; INT NINDEX = CCOBOX: : AddString (lpszIconFile); if (! nIndex = CB_ERR && nIndex = CB_ERRSPACE!) SetItemData (nIndex, (DWORD) hIcon); return nIndex;} int CIconComboBox :: insertIcon (int nIndex, LPCTSTR lpszIconFile) {HICON hIcon = :: Extracticon (AfxGetInstanceHandle (), LPSZICONFILE, 0); IF (Hicon == (Hicon) 1 || Hicon == NULL) Return CB_ERR; NINDEX = CCOMBOX :: InsertString (Nindex, lpsziconfile; if (NINDEX! = CB_ERR && NINDEX ! = CB_ERRSPACE) SETITEMDATA (NINDEX, (DWORD) HICON; Return Nindex;} int CICONCOMBOBOX :: SELEC tIcon (LPCTSTR lpszIconFile) {int nIndex = CComboBox :: FindStringExact (-1, lpszIconFile); if (nIndex == CB_ERR && CComboBox :: GetCount ()> 0) nIndex = 0; // select the first icon if not found return CComboBox :: SetCurSel (nIndex);} int CIconComboBox :: SelectIcon (int nIndex) {return CComboBox :: SetCurSel (nIndex);} int CIconComboBox :: DeleteIcon (LPCTSTR lpszIconFile) {int nIndex = CComboBox :: FindStringExact (-1, Lpsziconfile; Return CCOMBOBOX :: deleteString (Nindex);} int CCINCOMBOBOX :: Deleteicon (INT NINDEX) {Return CCOMBOBOX :: deleteString (NINDEX);

} Int CIconComboBox :: AddString (LPCTSTR lpszString) {ASSERT (FALSE); // can not add strings return CB_ERR;} int CIconComboBox :: InsertString (int nIndex, LPCTSTR lpszString) {ASSERT (FALSE); // can not insert strings return CB_ERR ;} int CIconComboBox :: DeleteString (int nIndex) {ASSERT (FALSE); // can not delete strings return CB_ERR;} void CIconComboBox :: MeasureItem (LPMEASUREITEMSTRUCT lpMIS) {lpMIS-> itemHeight = (m_sizeIcon.cy 2);} void CIconComboBox :: DrawItem (LPDRAWITEMSTRUCT lpDIS) {CDC * pDC = CDC :: FromHandle (lpDIS-> hDC); if (! IsWindowEnabled ()) {CBrush brDisabled (RGB (192,192,192)); // light gray CBrush * pOldBrush = pDC-> SelectObject (& brDisabled); CPen penDisabled (PS_SOLID, 1, RGB (192,192,192)); CPen * pOldPen = pDC-> SelectObject (& penDisabled); OnOutputIcon (lpDIS, FALSE); pDC-> SelectObject (pOldBrush); pDC- > SelectObject (PoldPen); Return;} // SELECTED IF ((LPDIS-> ItemState & ODS_SELECTED & " lpDIS-> itemAction & (ODA_SELECT | ODA_DRAWENTIRE))) {CBrush brHighlight (:: GetSysColor (COLOR_HIGHLIGHT)); CBrush * pOldBrush = pDC-> SelectObject (& brHighlight); CPen penHighlight (PS_SOLID, 1, :: GetSysColor (COLOR_HIGHLIGHT)) ; CPen * pOldPen = pDC-> SelectObject (& penHighlight); pDC-> Rectangle (& lpDIS-> rcItem); pDC-> SetBkColor (:: GetSysColor (COLOR_HIGHLIGHT)); pDC-> SetTextColor (:: GetSysColor (COLOR_HIGHLIGHTTEXT)); ONOUTPUTICON (LPDIS, TRUE); PDC-> SelectObject (Poldbrush); PDC-> SelectObject (PoldPen);} // de-selected if (! (Lpdis-> itemstate)

转载请注明原文地址:https://www.9cbs.com/read-43898.html

New Post(0)