#pragma once
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();
afx_msg void OnDelete();
afx_msg void OnNMRClick(NMHDR *pNMHDR, LRESULT *pResult);
afx_msg void Onrlist();
afx_msg void Ondlist();
};
#include "stdafx.h"
#include "MFC.h"
#include "MyListView.h"
#include "Add.h"
#include "Delete.h"
#include "MessageDao.h"
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()
#ifdef _DEBUG
void CMyListView::AssertValid() const
{
CListView::AssertValid();
}
#ifndef _WIN32_WCE
void CMyListView::Dump(CDumpContext& dc) const
{
CListView::Dump(dc);
}
#endif
#endif
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);
}
}
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);
}
}
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;
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;
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);
}