BOOL CTestView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
//  the CREATESTRUCT cs
DWORD ListStyle; ListStyle=LVS_REPORT; cs.style|=ListStyle|LVS_SINGLESEL|LVS_SHOWSELALWAYS| WS_VISIBLE; return CListView::PreCreateWindow(cs);
}void CTestView::OnInitialUpdate()
{
CListView::OnInitialUpdate();

::SendMessage(GetListCtrl().m_hWnd,LVM_SETEXTENDEDLISTVIEWSTYLE,LVS_EX_FULLROWSELECT, LVS_EX_FULLROWSELECT);

GetListCtrl().DeleteAllItems();

while(GetListCtrl().DeleteColumn(0)); TEXTMETRIC tm;
CClientDC dc(this); CTime temptime;
DWORD  dwPAid; CFont* pOldFont=dc.SelectObject(GetFont());
dc.GetTextMetrics(&tm);
dc.SelectObject(pOldFont); LV_COLUMN lvc;
lvc.mask=LVCF_FMT|LVCF_WIDTH|LVCF_TEXT|LVCF_SUBITEM;
lvc.iSubItem=0;
lvc.pszText="日期";
lvc.cx=tm.tmAveCharWidth*(2+15);
lvc.fmt=LVCFMT_LEFT;
GetListCtrl().InsertColumn(0,&lvc); lvc.iSubItem=1;
lvc.pszText="上班";
lvc.cx=tm.tmAveCharWidth*(45);
lvc.fmt=LVCFMT_LEFT;
GetListCtrl().InsertColumn(1,&lvc);
lvc.iSubItem=2;
lvc.pszText="下班";
lvc.cx=tm.tmAveCharWidth*(45);
lvc.fmt=LVCFMT_LEFT;
GetListCtrl().InsertColumn(2,&lvc);
// TODO: You may populate your ListView with items by directly accessing
//  its list control through a call to GetListCtrl().
int  NUM_ITEMS=5;
int n;
LV_ITEM lvi;
ZeroMemory(&lvi,sizeof(LV_ITEM));
lvi.mask=LVIF_TEXT;
CString tempstr;
  
for(n=0; n<NUM_ITEMS; n++)
{
lvi.iItem=n;
lvi.iSubItem=0; tempstr="sadfasdfasdfs";
(LPCTSTR&)lvi.pszText=(LPCTSTR)tempstr;
lvi.cchTextMax=strlen(lvi.pszText);
GetListCtrl().InsertItem(&lvi);
         
lvi.iItem=n;
lvi.iSubItem=1; (LPCTSTR&)lvi.pszText=(LPCTSTR)tempstr;
lvi.cchTextMax=strlen(lvi.pszText);
GetListCtrl().SetItem(&lvi);
//m_pRS->MoveNext();
lvi.iItem=n;
lvi.iSubItem=2;
(LPCTSTR&)lvi.pszText=(LPCTSTR)tempstr;
lvi.cchTextMax=strlen(lvi.pszText);
GetListCtrl().SetItem(&lvi);
} }
void CTestView::OnLButtonUp(UINT nFlags, CPoint point) 
{
// TODO: Add your message handler code here and/or call default
AfxMessageBox("");
CListView::OnLButtonUp(nFlags, point);
}

解决方案 »

  1.   

    /*class CTestView : public CListView*/
    我的代码,那位大侠帮我看看哪里错了?
      

  2.   

    你应该这样获得单击事件
    相应的消息为NM_RCLICK和NM_CLICKvoid CTestView::OnClickList(NMHDR* pNMHDR, LRESULT* pResult) 
    {
    // TODO: Add your control notification handler code here
    NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR; if(pNMListView&&pNMListView->uOldState!=pNMListView->uNewState)
    {
    CString str;
    str.Format("%d",pNMListView->iItem);
    AfxMessageBox(str); }

    *pResult = 0;
    }或者
    void CTestView::OnRclickList(NMHDR* pNMHDR, LRESULT* pResult) 
    {
    // TODO: Add your control notification handler code here
    NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR; if(pNMListView&&pNMListView->uOldState!=pNMListView->uNewState)
    {
    LVITEM item;
    m_ListCtrl.GetItem(&item);
    CString str;
    str.Format("%d",pNMListView->iItem);
    // str.Format("%d",item.iItem);
    AfxMessageBox(str.GetBuffer(255)); }
    *pResult = 0;
    }