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
田园的拾荒者 阅读(1568)
评论(3) 编辑 收藏 引用 所属分类:
翻译