⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 booklistview.cpp

📁 个人电子图书管理系统.提供电子书的书名和其他信息.检查电子书信息的合法性(E-1) .为这本电子书生成 id.使用分类id
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/// <summary>鼠标右击事件, 弹出菜单.在TreeView中选择"收藏夹",最近浏览"等, 显示的右键菜单各不相同</summary>
/// <param>忽略</param>
/// <returns>void</returns>
/// <exception>NULL</exception>
/// <remarks>
///  <b>步骤:</b>
///  <list type="number">
///  <item><description>得到鼠标位置, 并判断鼠标是否击中列表中的一行</description></item>
///  <item><description>根据TreeLiew中选中的节点, 弹出菜单.</description></item>
///  </list>
///  <b>如果鼠标在列表中击中了一项, 还有如下操作.</b>
///  <list type="number">
///  <item><description>选中鼠标击中的这一行</description></item>
///  <item><description>通知文档类, 选中列新行</description></item>
///  <item><description>通知文档类, 产生图书信息文件</description></item>
///  <item><description>通知文档类, 更新所有视图, 以便在InfoView中显示图书的详细信息</description></item>
///  </list>
/// </remarks>
void CBookListView::OnNMRclick(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);
        // 弹出菜单
        CMenu mHitItem;
        if(pDoc->GetShownBookClass() == IDS_FAVORITE) // TreeView中击中"收藏夹"
        { 
            mHitItem.LoadMenu(IDR_LVFAV_HIT_ITEM);
        }
        else if(pDoc->GetShownBookClass() == IDS_RECENT)
        {
            mHitItem.LoadMenu(IDR_LVREC_HIT_ITEM);
        }
        else if(pDoc->GetShownBookClass() == IDS_ALL_BOOK) 
        {
            mHitItem.LoadMenu(IDR_LVALL_HIT_ITEM);
        }
        else
        {
            mHitItem.LoadMenu(IDR_LV_HIT_ITEM);
        }
        CMenu *subMenu;
        subMenu = mHitItem.GetSubMenu(0);
        ClientToScreen(&myPoint); 
        subMenu->TrackPopupMenu(0, myPoint.x, myPoint.y, this);
    }
    else // 没有点中
    {
        // 弹出菜单
        CMenu mHitItem;
        if(pDoc->GetShownBookClass() == IDS_FAVORITE) // TreeView中击中"收藏夹"
        { 
            mHitItem.LoadMenu(IDR_LVFAV_NOHIT_ITEM);
        }
        else if(pDoc->GetShownBookClass() == IDS_RECENT)
        {
            mHitItem.LoadMenu(IDR_LVREC_NOHIT_ITEM);
        }
        else if(pDoc->GetShownBookClass() == IDS_ALL_BOOK) 
        {
            mHitItem.LoadMenu(IDR_LVALL_NOHIT_ITEM);
        }
        else
        {
            mHitItem.LoadMenu(IDR_LV_NOHIT_ITEM);
        }
        CMenu *subMenu;
        subMenu = mHitItem.GetSubMenu(0);
        ClientToScreen(&myPoint); 
        subMenu->TrackPopupMenu(0, myPoint.x, myPoint.y, this);
    }
    *pResult = 0;
}



/// <summary>用户在ListView里点击鼠标左键。如果选中图书,将在HtmlView里显示这本书的详细信息</summary>
/// <param>忽略</param>
/// <returns>void</returns>
/// <exception>NULL</exception>
/// <remarks>
/// </remarks>
void CBookListView::OnNMClick(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);
    }
    else
    {
        pDoc->SetShownBook("");
        pDoc->UpdateAllViews(this, IDUP_LISTVIEW);
    }

    *pResult = 0;
}


/// <summary>主要处理上下箭头弹起</summary>

/// <summary></summary>
/// <param name="nChar">virtual key code of the given key</param>
/// <returns>void</returns>
/// <exception>NULL</exception>
/// <remarks></remarks>
void CBookListView::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
{
    CListCtrl* pListCtrl = &(this->GetListCtrl());
    CKingBookDoc* pDoc = (CKingBookDoc*)this->GetDocument();
    if(nChar == VK_DOWN || nChar == VK_UP) // 上下箭头
    {
        POSITION pos = pListCtrl->GetFirstSelectedItemPosition();
        int nItem = pListCtrl->GetNextSelectedItem(pos); // 选中的项
        int nIDIndex = (int)pListCtrl->GetItemData(nItem);
        pDoc->SetShownBook( this->booksID[nIDIndex]);
        pDoc->CreateBookInfoFile(this->booksID[nIDIndex]);
        pDoc->UpdateAllViews(this, IDUP_LISTVIEW);
    }

    CListView::OnKeyUp(nChar, nRepCnt, nFlags);
}
#ifdef MORE_LISTVIEW_STYTLE
/// <summary>为修改行高和颜色</summary>
void CBookListView::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
    CListCtrl* pListCtrl = &(this->GetListCtrl());
    TCHAR  lpBuffer[256];

    LV_ITEM lvi;

    lvi.mask       = LVIF_TEXT | LVIF_PARAM ;
    lvi.iItem      = lpDrawItemStruct->itemID ;
    lvi.iSubItem   = 0;
    lvi.pszText    = lpBuffer ;
    lvi.cchTextMax = sizeof(lpBuffer);
    VERIFY(pListCtrl->GetItem(&lvi));

    LV_COLUMN lvc, lvcprev ;
    ::ZeroMemory(&lvc, sizeof(lvc));
    ::ZeroMemory(&lvcprev, sizeof(lvcprev));
    lvc.mask     = LVCF_WIDTH | LVCF_FMT;
    lvcprev.mask = LVCF_WIDTH | LVCF_FMT;

    for ( int nCol=0; pListCtrl->GetColumn(nCol, &lvc); nCol++)
    {
        if ( nCol > 0 )
        {
            // Get Previous Column Width in order to move the next display item
            pListCtrl->GetColumn(nCol-1, &lvcprev) ;
            lpDrawItemStruct->rcItem.left   += lvcprev.cx;
            lpDrawItemStruct->rcItem.right  += lpDrawItemStruct->rcItem.left ;
        }

        // Get the text
        ::ZeroMemory(&lvi, sizeof(lvi));
        lvi.iItem       = lpDrawItemStruct->itemID;
        lvi.mask        = LVIF_TEXT | LVIF_PARAM;
        lvi.iSubItem    = nCol;
        lvi.pszText     = lpBuffer;
        lvi.cchTextMax  = sizeof(lpBuffer);
        VERIFY(pListCtrl->GetItem(&lvi));

        CDC* pDC;
        pDC = CDC::FromHandle(lpDrawItemStruct->hDC);

        if ( lpDrawItemStruct->itemState & ODS_SELECTED )
        {
            RECT rcItem     = lpDrawItemStruct->rcItem;
            rcItem.right    += rcItem.left + pListCtrl->GetColumnWidth(nCol);
            pDC->FillSolidRect(&rcItem, GetSysColor(COLOR_HIGHLIGHT)) ;
            pDC->SetTextColor(GetSysColor(COLOR_HIGHLIGHTTEXT)) ;
        }
        else
        {
            if(lpDrawItemStruct->itemID % 2 == 0 )
            {
                //高亮
                pDC->FillSolidRect(&lpDrawItemStruct->rcItem, RGB(0xFF, 0xFF, 0xF0)) ;
            }
            else
            {   
                // 白色 
                pDC->FillSolidRect(&lpDrawItemStruct->rcItem, RGB(0xFF, 0xFF, 0xFF)) ;
            }

            pDC->SetTextColor(GetSysColor(COLOR_WINDOWTEXT)) ;
            //pDC->SetTextColor(RGB(0xFF, 0, 0)) ;
        }

        pDC->SelectObject(GetStockObject(DEFAULT_GUI_FONT));

        UINT  uFormat    = DT_LEFT | DT_MODIFYSTRING | DT_END_ELLIPSIS | DT_SINGLELINE ;
        RECT rcText = lpDrawItemStruct->rcItem;
        rcText.left = rcText.left + 5;
        rcText.right= rcText.left + pListCtrl->GetColumnWidth(nCol) - 5;
        rcText.top  = rcText.top + 5;
        ::DrawText(lpDrawItemStruct->hDC, lpBuffer, strlen(lpBuffer),
            &rcText, uFormat) ;

        pDC->SelectStockObject(SYSTEM_FONT) ;
    }
}

/// <summary>修改列表框的行高</summary>
void CBookListView::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
    // Set the height of the list box items. 
    lpMeasureItemStruct->itemHeight = 25; 
}
#endif //MORE_LISTVIEW_STYTLE

#ifdef USE_SETFONT
LRESULT CBookListView::OnSetFont(WPARAM wParam, LPARAM)
{
    CListCtrl* pListCtrl = &(this->GetListCtrl());
    LRESULT res = Default();

    CRect rc;
    ::GetWindowRect(pListCtrl->m_hWnd, &rc );

    WINDOWPOS wp;
    wp.hwnd  = m_hWnd;
    wp.cx    = rc.Width();
    wp.cy    = rc.Height();
    wp.flags = SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOZORDER;
    ::SendMessage(pListCtrl->m_hWnd, WM_WINDOWPOSCHANGED, 0, (LPARAM)&wp );

    return res;
}
void CBookListView::MeasureItem( LPMEASUREITEMSTRUCT lpMeasureItemStruct )
{
    AfxMessageBox("You just received a WM_MEASUREITEM message");
    CListCtrl* pListCtrl = &(this->GetListCtrl());
    LOGFONT lf;
    GetFont()->GetLogFont( &lf );
    
    if ( lf.lfHeight < 0 )
        lpMeasureItemStruct->itemHeight = -lf.lfHeight; 
    else
        lpMeasureItemStruct->itemHeight = lf.lfHeight; 

    TEXTMETRIC tm;
    HDC hDC = ::GetDC(NULL);
    CFont* pFont = pListCtrl->GetFont();
    HFONT hFontOld = (HFONT)SelectObject(hDC, pFont->GetSafeHandle());
    GetTextMetrics(hDC, &tm);
    lpMeasureItemStruct->itemHeight = tm.tmHeight + tm.tmExternalLeading + 1;
    SelectObject(hDC, hFontOld);
    ::ReleaseDC(NULL, hDC);
}
void  CBookListView::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
    CListCtrl* pListCtrl = &(this->GetListCtrl());
    pListCtrl->DrawItem(lpDrawItemStruct);

}

#endif USE_SETFONT
BOOL CBookListView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
    // TODO: 在此添加专用代码和/或调用基类

    return CListView::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -