VC中listview控件怎么使用(C语言)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了VC中listview控件怎么使用(C语言)相关的知识,希望对你有一定的参考价值。

在VC中listvuew这个控件怎么用啊,比如说怎么添加专栏上去....谢谢解答!!!

参考技术A 在VC中listvuew这个控件怎么用啊,
是listview
ListView 控件可使用四种不同视图显示项目。通过此控件,可将项目组成带有或不带有列标头的列,并显示伴随的图标和文本。 可使用 ListView 控件将称作 ListItem 对象的列表条目组织成下列四种不同的视图之一:1.大(标准)图标2.小图标3.列表4.报表 View 属性决定在列表中控件使用何种视图显示项目。还可用 LabelWrap 属性控制列表中与项目关联的标签是否可换行显示。另外,还可管理列表中项目的排序方法和选定项目的外观。

vc怎么实现列表控件各项的图标拖动

参考技术A 拖动图标通过实现OnBeginDrag等消息函数完成:
void CMyListCtrl::OnBeginDrag(LPNMHDR pnmhdr, LRESULT *pResult)

CPoint ptItem, ptAction, ptImage;
NM_LISTVIEW *pnmListView = (NM_LISTVIEW *)pnmhdr;

ASSERT(!m_bDragging);
m_bDragging = TRUE;
m_iItemDrag = pnmListView->iItem;
ptAction = pnmListView->ptAction;
GetItemPosition(m_iItemDrag, &ptItem); // ptItem is relative to (0,0) and not the view origin
GetOrigin(&m_ptOrigin);

ASSERT(m_pimageListDrag == NULL);
m_pimageListDrag = CreateDragImage(m_iItemDrag, &ptImage);
m_sizeDelta = ptAction - ptImage; // difference between cursor pos and image pos
m_ptHotSpot = ptAction - ptItem + m_ptOrigin; // calculate hotspot for the cursor
m_pimageListDrag->DragShowNolock(TRUE); // lock updates and show drag image
m_pimageListDrag->SetDragCursorImage(0, m_ptHotSpot); // define the hot spot for the new cursor image
m_pimageListDrag->BeginDrag(0, CPoint(0, 0));
ptAction -= m_sizeDelta;
m_pimageListDrag->DragEnter(this, ptAction);
m_pimageListDrag->DragMove(ptAction); // move image to overlap original icon
SetCapture();


void CMyListCtrl::OnMouseMove(UINT nFlags, CPoint point)

long lStyle;
int iItem;
LV_ITEM lvitem;

lStyle = GetWindowLong(m_hWnd, GWL_STYLE);
lStyle &= LVS_TYPEMASK; // drag will do different things in list and report mode
if (m_bDragging)

ASSERT(m_pimageListDrag != NULL);
m_pimageListDrag->DragMove(point - m_sizeDelta); // move the image
if ((iItem = HitTest(point)) != -1)

m_iItemDrop = iItem;
m_pimageListDrag->DragLeave(this); // unlock the window and hide drag image
if (lStyle == LVS_REPORT || lStyle == LVS_LIST)

lvitem.iItem = iItem;
lvitem.iSubItem = 0;
lvitem.mask = LVIF_STATE;
lvitem.stateMask = LVIS_DROPHILITED; // highlight the drop target
SetItem(&lvitem);


point -= m_sizeDelta;
m_pimageListDrag->DragEnter(this, point); // lock updates and show drag image



CListCtrl::OnMouseMove(nFlags, point);


void CMyListCtrl::OnButtonUp(CPoint point)

if (m_bDragging) // end of the drag operation

long lStyle;
CString cstr;

lStyle = GetWindowLong(m_hWnd, GWL_STYLE) & LVS_TYPEMASK;
m_bDragging = FALSE;

ASSERT(m_pimageListDrag != NULL);
m_pimageListDrag->DragLeave(this);
m_pimageListDrag->EndDrag();
delete m_pimageListDrag;
m_pimageListDrag = NULL;

// The drop target's sub-item text is replaced by the dragged item's
// main text
if (lStyle == LVS_REPORT && m_iItemDrop != m_iItemDrag)

cstr = GetItemText(m_iItemDrag, 0);
SetItemText(m_iItemDrop, 1, cstr);


//the character string "**" is added to the end of the drop target's main text
if (lStyle == LVS_LIST && m_iItemDrop != m_iItemDrag)

cstr = GetItemText(m_iItemDrop, 0);
cstr += _T("**");
SetItemText(m_iItemDrop, 0, cstr);


// move the icon
if (lStyle == LVS_ICON || lStyle == LVS_SMALLICON)

point -= m_ptHotSpot; // the icon should be drawn exactly where the image is
point += m_ptOrigin;
SetItemPosition(m_iItemDrag, point); // just move the dragged item


::ReleaseCapture();



void CMyListCtrl::OnLButtonUp(UINT nFlags, CPoint point)

OnButtonUp(point);
CListCtrl::OnLButtonUp(nFlags, point);

在图标间划线可以确定图标的位置然后用Pen自画。
右键菜单相应OnRclick函数
void CMyListView::OnRclick(NMHDR* pNMHDR, LRESULT* pResult)

// TODO: Add your control notification handler code here
CListCtrl &theCtrl = GetListCtrl();
CPoint pt;
::GetCursorPos(&pt);

theCtrl.ScreenToClient(&pt);
LVHITTESTINFO lvinfo;
lvinfo.pt = pt;
lvinfo.flags = LVHT_ABOVE;

m_nItem = theCtrl.SubItemHitTest(&lvinfo);

if(m_nItem != -1)


CMenu menu ,* pSubMenu;

menu.LoadMenu(IDR_MENU1);
pSubMenu = menu.GetSubMenu(0);
theCtrl.ClientToScreen(&pt);
if(pSubMenu)
pSubMenu->TrackPopupMenu (TPM_LEFTALIGN, pt.x, pt.y, this);

*pResult = 0;

以上是关于VC中listview控件怎么使用(C语言)的主要内容,如果未能解决你的问题,请参考以下文章

如何在ScrollView中嵌套ListView

C# winform编程 开发环境VS2010 listview控件问题

C语言(VC++6.0)获取cpu编码

listview控件用法?

vc++ 工具栏 高度如何改变

C语言中的64位(VC)