опубликован 11-04-2001 21:31 MSK
вот пример на ATL (в MFC тоже)контрола, который содержит такой ListViewCListView():m_ctlSysListView32(_T("SysListView32"), this, 1){}
LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
RECT rc;
GetWindowRect(&rc);
rc.right -= rc.left;
rc.bottom -= rc.top;
rc.top = rc.left = 0;
InitCommonControls();
m_ctlSysListView32.Create(m_hWnd, rc,NULL,WS_CHILD|WS_VISIBLE | LVS_REPORT | LVS_SHOWSELALWAYS );
//Use Extended ListView Styles!
SendMessage (m_ctlSysListView32.m_hWnd, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_SUBITEMIMAGES, LVS_EX_SUBITEMIMAGES);
//Create the Table ImageList
HIMAGELIST hTableImageList = ImageList_Create(16, 16, ILC_MASK, 2, 2 );
HICON hIcon = LoadIcon(_Module.m_hInst , MAKEINTRESOURCE(IDI_EMPTY));
ImageList_AddIcon(hTableImageList, hIcon);
hIcon = LoadIcon(_Module.m_hInst , MAKEINTRESOURCE(IDI_CHECKED));
ImageList_AddIcon(hTableImageList, hIcon);
hIcon = LoadIcon(_Module.m_hInst , MAKEINTRESOURCE(IDI_UNCHECKED));
ImageList_AddIcon(hTableImageList, hIcon);
hIcon = LoadIcon(_Module.m_hInst , MAKEINTRESOURCE(IDI_CHECKEDSHADOW));
ImageList_AddIcon(hTableImageList, hIcon);
hIcon = LoadIcon(_Module.m_hInst , MAKEINTRESOURCE(IDI_UNCHECKEDSHADOW));
ImageList_AddIcon(hTableImageList, hIcon);
ListView_SetImageList(m_ctlSysListView32.m_hWnd, hTableImageList, LVSIL_SMALL);
return 0;
}
STDMETHODIMP CListView::InsertRow(long nRowID, long* nRowIndex)
{
LV_ITEM lvi;
long i;
long nCount=0;
get_Count(&nCount);
lvi.mask = LVIF_TEXT | LVIF_PARAM ;
lvi.iItem = nCount;
lvi.state = 0;
lvi.iSubItem = 0;
lvi.stateMask = 0;
lvi.cchTextMax = 0;
lvi.iImage = 1;
lvi.lParam = nRowID;
lvi.iIndent = 0;
lvi.pszText = TEXT("");
int iNewItem = ListView_InsertItem( m_ctlSysListView32.m_hWnd, &lvi);
if( -1 == iNewItem)
return E_FAIL;
for (i=0;i<m_nColCount;i++)
lvi.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_STATE;
lvi.iItem = iNewItem;
lvi.state = 0;
lvi.iSubItem = i;
lvi.iImage = 2; // Index of uncheked icon
lvi.pszText = TEXT("");
ListView_SetItem( m_ctlSysListView32.m_hWnd, &lvi);
}
*nRowIndex=iNewItem;
return S_OK;
}
// Ну а здесь меняем иконки с Checked на Uncheked и наоборот
LRESULT CListView::OnLButtonUP(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
//This call has to be made to select node before firing the event.
POINTS pnt = MAKEPOINTS( lParam);
LV_HITTESTINFO hitinfo;
int iHitItem;
hitinfo.pt.x = pnt.x;
hitinfo.pt.y = pnt.y;
hitinfo.flags = LVHT_ONITEM;
hitinfo.iItem = 0;
iHitItem = ListView_SubItemHitTest( m_ctlSysListView32.m_hWnd, &hitinfo);
if (hitinfo.flags & LVHT_ONITEMICON)
ChangeItemState(hitinfo.iItem,hitinfo.iSubItem);