📄 booklistview.cpp
字号:
// GUI/BookListView.cpp : 实现文件
//
#include "stdafx.h"
#include "KingBook.h"
#include "BookListView.h"
#include <vector>
#include "KingBookDoc.h"
#include "BookTreeView.h"
#include <sstream>
using namespace Library;
using namespace std;
// CBookListView
IMPLEMENT_DYNCREATE(CBookListView, CListView)
CBookListView::CBookListView()
{
}
CBookListView::~CBookListView()
{
}
BEGIN_MESSAGE_MAP(CBookListView, CListView)
ON_WM_CREATE()
ON_NOTIFY_REFLECT(NM_DBLCLK, &CBookListView::OnNMDblclk)
ON_NOTIFY_REFLECT(NM_RCLICK, &CBookListView::OnNMRclick)
ON_NOTIFY_REFLECT(NM_CLICK, &CBookListView::OnNMClick)
// ON_NOTIFY_REFLECT(LVN_ITEMACTIVATE, &CBookListView::OnLvnItemActivate)
// ON_NOTIFY_REFLECT(LVN_GETDISPINFO, &CBookListView::OnLvnGetdispinfo)
ON_WM_KEYUP()
#ifdef USE_SETFONT
ON_MESSAGE(WM_SETFONT, OnSetFont)
ON_WM_MEASUREITEM_REFLECT()
#endif
#ifdef MORE_LISTVIEW_STYTLE
ON_WM_MEASUREITEM_REFLECT()
#endif // MORE_LISTVIEW_STYTLE
END_MESSAGE_MAP()
// CBookListView 诊断
#ifdef _DEBUG
void CBookListView::AssertValid() const
{
CListView::AssertValid();
}
#ifndef _WIN32_WCE
void CBookListView::Dump(CDumpContext& dc) const
{
CListView::Dump(dc);
}
#endif
#endif //_DEBUG
// CBookListView 消息处理程序
/// <summary>创建列表视图</summary>
/// <param name="lpCreateStruct">[IN]列表视图的样式</param>
/// <returns>成功返回 0;</returns>
/// <exception>NULL</exception>
/// <remarks>创建列表视图, 进行如下初始化:
/// <list type="bullet">
/// <item><description>修改列表视图和列表控件的样式.</description></item>
/// <item><description>填充列表, 列出"我的电子书"下的图书.</description></item>
/// </list>
/// </remarks>
int CBookListView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
#ifdef MORE_LISTVIEW_STYTLE
lpCreateStruct->style = lpCreateStruct->style |LVS_REPORT | LVS_SINGLESEL | LVS_SHOWSELALWAYS |LVS_OWNERDRAWFIXED ;
#else
lpCreateStruct->style = lpCreateStruct->style |LVS_REPORT | LVS_SINGLESEL | LVS_SHOWSELALWAYS ;
#endif
#ifdef USE_SETFONT
lpCreateStruct->style = lpCreateStruct->style |LVS_REPORT | LVS_SINGLESEL | LVS_SHOWSELALWAYS |LVS_OWNERDRAWFIXED ;
#else
lpCreateStruct->style = lpCreateStruct->style |LVS_REPORT | LVS_SINGLESEL | LVS_SHOWSELALWAYS ;
#endif
// lpCreateStruct->dwExStyle = lpCreateStruct->dwExStyle | /*LVS_EX_HEADERDRAGDROP |*/ LVS_EX_FULLROWSELECT | LVS_EX_DOUBLEBUFFER | LVS_EX_BORDERSELECT ;
if (CListView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: 添加试验行
CListCtrl* pListCtrl = &(this->GetListCtrl());
Library::BookLibrary &bookLib= (((CKingBookDoc*)this->GetDocument())->bookLib);
// 添加试验树木信息
std::vector<BookObject> booksID;
Library::Book bookInfo;
bookLib.GetBooksOfClass(BookObject(IDS_BOOKCLASS_ROOT), booksID);
// pListCtrl->InsertColumn(0, _T("id"), 0, 200);
pListCtrl->InsertColumn(0, _T("名称"),0, 200);
pListCtrl->InsertColumn(1, _T("作者"),0, 200);
pListCtrl->InsertColumn(2, _T("摘要"),0, 200);
for(int i = 0; i < booksID.size(); i ++)
{
bookLib.GetBookInfo(booksID[i].GetID(), bookInfo);
pListCtrl->InsertItem(0, bookInfo.GetName().c_str());
pListCtrl->SetItemText(0, 1, bookInfo.GetAuthor().c_str());
pListCtrl->SetItemText(0, 2, bookInfo.GetBrief().c_str());
}
pListCtrl->SetExtendedStyle( lpCreateStruct->dwExStyle /*| LVS_EX_HEADERDRAGDROP*/ | LVS_EX_FULLROWSELECT | LVS_EX_DOUBLEBUFFER |LVS_EX_GRIDLINES /*| LVS_EX_BORDERSELECT*/);
//pListCtrl->SetTextBkColor(RGB(163, 239, 220));
return 0;
}
/// <stdname>
/// void CBookListView::OnUpdate(CView* pSender, LPARAM lHint, CObject* /*pHint*/)
/// </stdname>
/// <summary>更新视图</summary>
/// <param name="lHint">[IN]更新视图的原因代码.</param>
/// <returns>void</returns>
/// <exception>NULL</exception>
/// <remarks>lHint的原因为IDUP_TREEVIEW, IDUP_DOC_BOOK时更新视图.<br/>
/// 当TreeLiew中选中不同节点时, 更新视图的方式不同, 区分三类:图书分类+收藏夹, 最近浏览, 所有图书.<br/>
/// <b>步骤:</b>
/// <list type="number">
/// <item><description>清空列表中已有的内容.</description></item>
/// <item><description>给列表添加新列, 根据TreeLiew中选中节点的不同添加不同的列</description></item>
/// <item><description>添加行, 添加详细信息</description></item>
/// </list>
/// </remarks>
void CBookListView::OnUpdate(CView* pSender, LPARAM lHint, CObject* /*pHint*/)
{
// 发送者必须是TreeView或Doc
if(lHint != IDUP_TREEVIEW && lHint != IDUP_DOC_BOOK)
return;
//if((pSender == NULL) || (typeid(*pSender) == typeid(CBookTreeView)))
// return;
CKingBookDoc* pDoc = (CKingBookDoc*)this->GetDocument();
CListCtrl* pListCtrl = &(this->GetListCtrl());
//清空已有信息
this->booksID.clear();
pListCtrl->DeleteAllItems();
int nColumnCount = pListCtrl->GetHeaderCtrl()->GetItemCount();
for(int i = 0; i < nColumnCount; i ++)
{
pListCtrl->DeleteColumn(0);
}
// 显示某个分类的图书
try{
// 区分不同情况:图书分类+收藏夹, 最近浏览, 所有图书
if(pDoc->GetShownBookClass() == IDS_RECENT)
{
pListCtrl->InsertColumn(0, _T("名称"),0, 200);
pListCtrl->InsertColumn(1, _T("作者"),0, 200);
pListCtrl->InsertColumn(2, _T("最近打开"),0, 150);
pListCtrl->InsertColumn(3, _T("频率"),0, 50);
pListCtrl->InsertColumn(5, _T("摘要"),0, 200);
Library::Book bookInfo;
std::vector<RecentItem> recentItems;
// 得到"最近浏览"的信息
pDoc->bookLib.GetRecentInfo(recentItems);
for(int i = 0; i < recentItems.size(); i ++)
{
pDoc->bookLib.GetBookInfo(recentItems[i].GetID(), bookInfo);
LVITEM item;
item.iItem = i;
item.iSubItem = 0;
item.mask = LVIF_PARAM | LVIF_TEXT;
item.pszText = const_cast<LPSTR>(bookInfo.GetName().c_str());
this->booksID.push_back(bookInfo.GetID());
item.lParam = (LPARAM) this->booksID.size() -1; // ID's index
if(i == pListCtrl->InsertItem(&item))
{
// MessageBox(_T(" LPARAM "));
}
pListCtrl->SetItemText(i, 1, bookInfo.GetAuthor().c_str());
time_t lastTime = FromString<time_t>(recentItems[i].GetLastTime());
CTime ct(lastTime);
pListCtrl->SetItemText(i, 2, ct.Format("%Y-%m-%d %H:%M:%S"));
pListCtrl->SetItemText(i, 3, ToString<char>(recentItems[i].GetCount()).c_str());
pListCtrl->SetItemText(i, 4, bookInfo.GetBrief().c_str());
}
}
else if(pDoc->GetShownBookClass() == IDS_ALL_BOOK)
{
std::vector<BookObject> books;
Book bookInfo;
// 得到所有图书的信息
pDoc->bookLib.GetAllBookInfo(books);
pListCtrl->InsertColumn(0, _T("名称"),0, 200);
pListCtrl->InsertColumn(1, _T("作者"),0, 200);
pListCtrl->InsertColumn(2, _T("摘要"),0, 200);
for(int i = 0; i < books.size(); i ++)
{
pDoc->bookLib.GetBookInfo(books[i].GetID(), bookInfo);
LVITEM item;
item.iItem = i;
item.iSubItem = 0;
item.mask = LVIF_PARAM | LVIF_TEXT;
item.pszText = const_cast<LPSTR>(bookInfo.GetName().c_str());
this->booksID.push_back(bookInfo.GetID());
item.lParam = (LPARAM) this->booksID.size() -1; // ID's index
if(i == pListCtrl->InsertItem(&item))
{
// MessageBox(_T(" LPARAM "));
}
pListCtrl->SetItemText(i, 1, bookInfo.GetAuthor().c_str());
pListCtrl->SetItemText(i, 2, bookInfo.GetBrief().c_str());
}
}
else
{
std::vector<BookObject> books;
Library::Book bookInfo;
// 得到某个分类下的图书
pDoc->bookLib.GetBooksOfClass(BookObject(pDoc->GetShownBookClass()), books);
pListCtrl->InsertColumn(0, _T("名称"),0, 200);
pListCtrl->InsertColumn(1, _T("作者"),0, 200);
pListCtrl->InsertColumn(2, _T("摘要"),0, 200);
for(int i = 0; i < books.size(); i ++)
{
pDoc->bookLib.GetBookInfo(books[i].GetID(), bookInfo);
LVITEM item;
item.iItem = i;
item.iSubItem = 0;
item.mask = LVIF_PARAM | LVIF_TEXT;
item.pszText = const_cast<LPSTR>(bookInfo.GetName().c_str());
this->booksID.push_back(bookInfo.GetID());
item.lParam = (LPARAM) this->booksID.size() -1; // ID's index
if(i == pListCtrl->InsertItem(&item))
{
// MessageBox(_T(" LPARAM "));
}
pListCtrl->SetItemText(i, 1, bookInfo.GetAuthor().c_str());
pListCtrl->SetItemText(i, 2, bookInfo.GetBrief().c_str());
}
}
}catch(std::runtime_error& e)
{
MessageBox(e.what());
}
}
/// <summary>鼠标双击事件.打开选中的图书</summary>
/// <param>忽略</param>
/// <returns>void</returns>
/// <exception>NULL</exception>
/// <remarks>
/// <b>步骤:</b>
/// <list type="number">
/// <item><description>得到鼠标位置, 并判断鼠标是否击中列表中的一行</description></item>
/// <item><description>选中鼠标击中的这一行</description></item>
/// <item><description>通知文档类, 选中列新行</description></item>
/// <item><description>通知文档类, 产生图书信息文件</description></item>
/// <item><description>通知文档类, 更新所有视图, 以便在InfoView中显示图书的详细信息</description></item>
/// <item><description>通知文档类, 打开图书</description></item>
/// </list>
/// </remarks>
void CBookListView::OnNMDblclk(NMHDR *pNMHDR, LRESULT *pResult)
{
CKingBookDoc* pDoc = (CKingBookDoc*)this->GetDocument();
CListCtrl* pListCtrl = &(this->GetListCtrl());
CPoint myPoint;
GetCursorPos(&myPoint);
ScreenToClient(&myPoint);
UINT uFlags;
int nItem = pListCtrl->HitTest(myPoint, &uFlags);
// 点中一本图书
if (nItem != -1)
{
pListCtrl->SetItem(nItem, 0, LVIF_STATE, NULL, 0, LVIS_SELECTED,
LVIS_SELECTED, 0);
//MessageBox(pListCtrl->GetItemText(nItem, 1));
int nIDIndex = (int)pListCtrl->GetItemData(nItem);
//MessageBox(this->booksID[nIDIndex].c_str());
pDoc->SetShownBook( this->booksID[nIDIndex]);
pDoc->CreateBookInfoFile(this->booksID[nIDIndex]);
pDoc->UpdateAllViews(this, IDUP_LISTVIEW);
pDoc->OpenBook();
}
*pResult = 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -