随笔 - 14  文章 - 1 评论 - 13 
<2009年3月>
22232425262728
1234567
891011121314
15161718192021
22232425262728
2930311234

常用链接

留言簿(1)

随笔分类

随笔档案

相册

搜索

  •  

最新评论

阅读排行榜

评论排行榜

HOWTO: Change Icon or Bitmap of CListCtrl Item When Selected
HOWTO:当你选中一个CListCtrl项目的时候更改其ICON或者BITMAP
                                                                                  translated by zhkza99c
最后修改: July 7, 1997
文章 ID: Q141834
这篇文章里的内容应用于:
  • Microsoft Foundation Classes (MFC) 包括: - Microsoft Visual C++, 32-bit Edition, versions 4.0, 5.0

SUMMARY 简介

This article shows how to change the icon or bitmap of a CListCtrl item when it is selected.
这篇文章展示了当你选中一个CListCtrl项目的时候如何更改其ICON或者BITMAP

MORE INFORMATION 更多信息

When you initialize the CListCtrl by calling CListCtrl::InsertItem(), you can pass in a value of I_IMAGECALLBACK for the index of the image. This means that the system expects you to fill in the image index when you get an LVN_GETDISPINFO notification. Inside of the handler for LVN_GETDISPINFO, you can check if the item is selected and set the appropriate image index.

当你使用CListCtrl::InsertItem()初始化CListCtrl的时候,你可以传入一个I_IMAGECALLBACK 的值,用来表示图像的索引号。这就意味着系统希望你在得到LVN_GETDISPINFO通知的时候能够填充图像的索引,在 LVN_GETDISPINFO的内部实现中,你可以检测一个项目是否被选中并且设置适当的图像索引。

Sample Code例子代码

   BEGIN_MESSAGE_MAP(CTestView, CView)
//{{AFX_MSG_MAP(CTestView)
ON_WM_CREATE()
//}}AFX_MSG_MAP
ON_NOTIFY (LVN_GETDISPINFO, IDI_LIST, OnGetDispInfo)
END_MESSAGE_MAP()
int CTestView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// m_pImage is a CTestView's member variable of type CImageList*
// create the CImageList with 16x15 images
m_pImage = new CImageList();
VERIFY (m_pImage->Create (16, 15, TRUE, 0, 1));
CBitmap bm;
// IDR_MAINFRAME is the toolbar bitmap in a default AppWizard
// project.
bm.LoadBitmap (IDR_MAINFRAME);
// This will automatically parse the bitmap into nine images.
m_pImage->Add (&bm, RGB (192, 192, 192));
// m_pList is  CTestView's member variable of type CListCtrl*
// create the CListCtrl.
m_pList = new CListCtrl();
VERIFY (m_pList->Create (WS_VISIBLE | WS_CHILD | LVS_REPORT |
LVS_EDITLABELS, CRect (0, 0, 400, 400), this, IDI_LIST));
// Create column.
m_pList->InsertColumn (0, "Button Number", LVCFMT_LEFT, 100);
// Associate CImageList with CListCtrl.
m_pList->SetImageList (m_pImage, LVSIL_SMALL);
char szTemp[10];
for (int iCntr = 0; iCntr < 9; iCntr++)
{
wsprintf (szTemp, "%d", iCntr);
m_pList->InsertItem (LVIF_IMAGE | LVIF_TEXT,
iCntr, szTemp, 0, 0, I_IMAGECALLBACK, 0L);
}
return 0;
}
void CTestView::OnGetDispInfo (NMHDR* pnmhdr, LRESULT* pResult)
{
LV_DISPINFO* pdi = (LV_DISPINFO *) pnmhdr;
// Fill in the LV_ITEM structure with the image info.
// When an item is selected, the image is set to the first
// image (the new bitmap on the toolbar).
// When it is not selected, the image index is equal to the
// item number (that is, 0=new, 1=open, 2=save, and so on.)
if (LVIS_SELECTED == m_pList->GetItemState (pdi->item.iItem,
LVIS_SELECTED))
pdi->item.iImage = 0;
else
pdi->item.iImage = pdi->item.iItem;
}
CTestView::~CTestView()
{
// Clean up.
delete m_pimagelist;
delete m_pimagelistSmall;
}

Keywords : kbcode kbprg MfcUI
Technology : kbMfc
Version : 4.0 5.0
Platform : NT WINDOWS
Issue type : kbhowto

posted on 2008-03-12 14:17 田园的拾荒者 阅读(1538) 评论(3)  编辑 收藏 引用 所属分类: 翻译

FeedBack:
# re: HOWTO: Change Icon or Bitmap of CListCtrl Item When Selected 2008-07-24 22:23 飞鸽传书
MSDN上抄下来的。  回复  更多评论
  
# re: HOWTO: Change Icon or Bitmap of CListCtrl Item When Selected 2008-07-24 22:24 企业即时通讯
哈哈,刚好可以用在我程序上,谢谢。  回复  更多评论
  
# re: HOWTO: Change Icon or Bitmap of CListCtrl Item When Selected 2009-03-10 22:14 飞鸽传书
3个月后,我再次读到这篇文章。  回复  更多评论
  
只有注册用户登录后才能发表评论。