CListView操作

#pragma once

// CMyListView 视图

class CMyListView : public CListView
{
    DECLARE_DYNCREATE(CMyListView)

protected:
    CMyListView();           // 动态创建所使用的受保护的构造函数
    virtual ~CMyListView();

public:
    int selectedLine;//行
    int selectedRow;//列

public:
#ifdef _DEBUG
    virtual void AssertValid() const;
#ifndef _WIN32_WCE
    virtual void Dump(CDumpContext& dc) const;
#endif
#endif

protected:
    DECLARE_MESSAGE_MAP()
public:
    afx_msg void OnOperator();
    virtual void OnInitialUpdate();
//  virtual BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext = NULL);
    afx_msg void OnDelete();
    afx_msg void OnNMRClick(NMHDR *pNMHDR, LRESULT *pResult);
    afx_msg void Onrlist();
    afx_msg void Ondlist();
};
// MyListView.cpp : 实现文件
//

#include "stdafx.h"
#include "MFC.h"
#include "MyListView.h"

#include "Add.h"
#include "Delete.h"

#include "MessageDao.h"

// CMyListView

IMPLEMENT_DYNCREATE(CMyListView, CListView)

CMyListView::CMyListView()
{
    selectedRow=0;
    selectedLine=0;
}


CMyListView::~CMyListView()
{
}

BEGIN_MESSAGE_MAP(CMyListView, CListView)
    ON_COMMAND(ID_Operator, &CMyListView::OnOperator)
    ON_COMMAND(ID_Delete, &CMyListView::OnDelete)
    ON_NOTIFY_REFLECT(NM_RCLICK, &CMyListView::OnNMRClick)
    ON_COMMAND(ID_rList, &CMyListView::Onrlist)
    ON_COMMAND(ID_dList, &CMyListView::Ondlist)
END_MESSAGE_MAP()


// CMyListView 诊断

#ifdef _DEBUG
void CMyListView::AssertValid() const
{
    CListView::AssertValid();
}

#ifndef _WIN32_WCE
void CMyListView::Dump(CDumpContext& dc) const
{
    CListView::Dump(dc);
}
#endif
#endif //_DEBUG


// CMyListView 消息处理程序

//菜单点击事件添加
void CMyListView::OnOperator()
{
    CAdd myAdd;
    if (myAdd.DoModal()==IDOK)
    {
        CListCtrl &m_list=GetListCtrl();
        int n_item=m_list.GetItemCount();
        int n_index=m_list.InsertItem(n_item,myAdd.myTime);
        m_list.SetItemText(n_index,1,myAdd.myEvent);
        CMessageDao *msgDao=new CMessageDao();
        msgDao->insertMSG(myAdd.myTime,myAdd.myEvent);
    }

}

//菜单点击事件删除
void CMyListView::OnDelete()
{
    CDelete myDelete;
    if (myDelete.DoModal()==IDOK)
    {
        CListCtrl &m_list=GetListCtrl();
        int n_item=_ttoi(myDelete.s_item);
        m_list.DeleteItem(n_item-1);
    }

}

//初始化ListView
void CMyListView::OnInitialUpdate()
{
    CListView::OnInitialUpdate();
    CListCtrl &m_list=GetListCtrl();

    LONG lStyle;
    lStyle=GetWindowLong(m_list.m_hWnd,GWL_STYLE);
    lStyle&=~LVS_TYPEMASK;
    lStyle|=LVS_REPORT;
    SetWindowLong(m_list.m_hWnd,GWL_STYLE,lStyle);
    DWORD dwStyle=m_list.GetExtendedStyle();
    dwStyle|=LVS_EX_FULLROWSELECT;
    dwStyle|=LVS_EX_GRIDLINES;
    m_list.SetExtendedStyle(dwStyle);
    m_list.SetBkColor(RGB(200,200,200));
    m_list.SetTextBkColor(RGB(200,200,200));
    m_list.SetTextColor(RGB(10,10,80));
    m_list.InsertColumn(0,_T("Time"),LVCFMT_CENTER,100);
    m_list.InsertColumn(1,_T("Message"),LVCFMT_CENTER,600);

    CMessageDao *msgDao=new CMessageDao();
    CList<MyMSG> *msgList=msgDao->getMSG();

    MyMSG msg;
    for (POSITION pos=msgList->GetHeadPosition();pos;msgList->GetNext(pos)){
        msg=msgList->GetAt(pos);

        int n_item=m_list.GetItemCount();
        int n_index=m_list.InsertItem(n_item,msg.s_time);
        m_list.SetItemText(n_index,1,msg.s_message);
        //MessageBox(msg.s_time,msg.s_message,MB_OK);
    }

}

//右键列表弹出菜单
void CMyListView::OnNMRClick(NMHDR *pNMHDR, LRESULT *pResult)
{
    LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
    *pResult = 0;

    CListCtrl &m_list=GetListCtrl();
    if (m_list.GetSelectedCount()<=0)
    {
        return;
    }

    NM_LISTVIEW *pN=(NM_LISTVIEW *)pNMHDR;
    //CString str;
    //str.Format(_T("单击的是第%d行第%d列"),pN->iItem, pN->iSubItem);
    //AfxMessageBox(str);
    selectedLine=pN->iItem;
    selectedRow=pN->iSubItem;

    CMenu menu,*pPopup;
    menu.LoadMenuW(IDR_MENU1);
    pPopup=menu.GetSubMenu(0);
    CPoint myPoint;
    ClientToScreen(&myPoint);
    GetCursorPos(&myPoint);
    pPopup->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,myPoint.x,myPoint.y,this);

    *pResult = 0;
}

//右键列表之查看
void CMyListView::Onrlist()
{
    CString str;
    //str.Format(_T("单击的是第%d行第%d列"),selectedLine,selectedRow);
    //AfxMessageBox(str);
    CListCtrl &m_list=GetListCtrl();
    str=m_list.GetItemText(selectedLine,selectedRow);
    AfxMessageBox(str);
}

//右键菜单之删除
void CMyListView::Ondlist()
{
    CListCtrl &m_list=GetListCtrl();
    CString str;
    str=m_list.GetItemText(selectedLine,0);
    m_list.DeleteItem(selectedLine);
    CMessageDao *msgDao=new CMessageDao();
    msgDao->deleteMSG(str);
}
非常好用的CListCtrl加强版。 从CListCtrl继承,完全兼容CListCtrl. 它有以下特性: • The background of the sorted column can be displayed in a different color (like the detailed view of Windows XP Explorer). • The header of the sorted column can display an arrow that indicates the sort direction (like the detailed view of Windows XP Explorer). • You can give the user the opportunity to hide or redisplay selected columns by just clicking on the header control with the right mouse button (see picture below). • You can hide or redisplay a selected column entirely. • In contrast to the original list view control, the first column can have the LVCFMT_CENTER or LVCFMT_RIGHT style, too. For this feature, the list view control must have the LVS_OWNERDRAWFIXED style. • The label attributes state icon, small icon, and selection can always be shown in the leftmost column, independent of the order of the columns. For this feature, the listview control must have the LVS_OWNERDRAWFIXED style. • You can supply tooltips not only for the whole item, but also for the small icon, the state icon, and each subitem label. • The extended styles LVS_EX_CHECKBOXES, LVS_EX_ONECLICKACTIVATE, LVS_EX_SUBITEMIMAGES, LVS_EX_TWOCLICKACTIVATE, and LVS_EX_UNDERLINEHOT will be supported even if the list view control has the LVS_OWNERDRAWFIXED style. • If the LVS_EX_CHECKBOXES style has been applied and a selected item will be checked/unchecked, all other selected items will be checked/unchecked, too. • If the LVS_EX_LABELTIP style has been applied, not only the partially hidden text of the item label will be unfolded but also the partially hidden text of each subitem label. Note: The LVS_EX_LABELTIP style will be supported under all operating systems. • Tooltips and expanded labels can be displayed simultaneously. • The current state of the list view control (column widths, column order, hidden columns, and sort column and direction) can be saved and restored.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值