读书人

【从绘ListBox之一】带Icon的ListBox控

发布时间: 2012-11-23 22:54:33 作者: rapoo

【自绘ListBox之一】带Icon的ListBox控件

参考:CListBoxST源码Demo源程序:CIconListBox_demoDemo程序图片:【从绘ListBox之一】带Icon的ListBox控件使用示例:手动更改ListBox控件的属性如下,因为以下特性不能通过代码动态修改。【从绘ListBox之一】带Icon的ListBox控件
//// nIcon为ICON的ID// 注意:CListBox::AddString中会调用MeasureItem确定加入项的高度,所以//      先LoadImage确定ICON的高度,并赋值给类的私有变量m_nIconHeight,//      这样在MeasureItem中就可以用m_nIconHeight确定Icon的高度//int CIconListBox::AddString(LPCTSTR lpszItem, int nIcon){LBDATA *pLbData = NULL;m_nIconHeight = 0;// 加载IconHICON hIcon = (HICON)::LoadImage(::GetModuleHandle(NULL), MAKEINTRESOURCE(nIcon),   IMAGE_ICON, 0, 0, 0);if (hIcon != NULL){pLbData = new LBDATA();ICONINFO ici;  ::GetIconInfo(hIcon, &ici);  BITMAP bm;  ::GetObject(ici.hbmColor, sizeof(BITMAP), &bm);::DeleteObject(ici.hbmColor);  ::DeleteObject(ici.hbmMask);pLbData->hIcon = hIcon;pLbData->nIconHeight = bm.bmHeight;pLbData->nIconWidth = bm.bmWidth;m_nIconHeight = bm.bmHeight;}// Add string and lbdataint nIndex = CListBox::AddString(lpszItem);if (LB_ERR == nIndex || LB_ERRSPACE == nIndex){if (NULL != pLbData){delete pLbData;::DestroyIcon(hIcon);}}else{CListBox::SetItemDataPtr(nIndex, pLbData);}return nIndex;}int CIconListBox::InsertString(int nIndex, LPCTSTR lpszItem, int nIcon){LBDATA *pLbData = NULL;m_nIconHeight = 0;// 加载IconHICON hIcon = (HICON)::LoadImage(::GetModuleHandle(NULL), MAKEINTRESOURCE(nIcon),   IMAGE_ICON, 0, 0, 0);if (hIcon != NULL){pLbData = new LBDATA();ICONINFO ici;  ::GetIconInfo(hIcon, &ici);  BITMAP bm;  ::GetObject(ici.hbmColor, sizeof(BITMAP), &bm);::DeleteObject(ici.hbmColor);  ::DeleteObject(ici.hbmMask);pLbData->hIcon = hIcon;pLbData->nIconHeight = bm.bmHeight;pLbData->nIconWidth = bm.bmWidth;m_nIconHeight = bm.bmHeight;}// Insert string and lbdatanIndex = CListBox::InsertString(nIndex, lpszItem);if (LB_ERR == nIndex || LB_ERRSPACE == nIndex){if (NULL != pLbData){::DestroyIcon(hIcon);delete pLbData;}}else{CListBox::SetItemDataPtr(nIndex, pLbData);}return nIndex;}int CIconListBox::DeleteString(UINT nIndex){DeleteItemData(nIndex);return CListBox::DeleteString(nIndex);}void CIconListBox::DeleteItemData(UINT nIndex){LBDATA *pLbData = (LBDATA*) CListBox::GetItemDataPtr(nIndex);if ((LBDATA*)-1 != pLbData && NULL != pLbData){if (pLbData->hIcon)::DestroyIcon(pLbData->hIcon);delete pLbData;}}void CIconListBox::ResetContent(){int nCount = GetCount();for (int i=0; i<nCount; ++i){DeleteItemData(i);}CListBox::ResetContent();}

读书人网 >编程

热点推荐