📄 listviewex.cpp
字号:
// ListViewEx.cpp : implementation file
//
#include "stdafx.h"
#include "DirList.h"
#include "ListViewEx.h"
#include "stdafx.h"
#include "proto.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// Offset the text to keep it from butting up
// against the edge of the column
#define TEXT_OFFSET 6
/////////////////////////////////////////////////////////////////////////////
// CListViewEx
IMPLEMENT_DYNCREATE(CListViewEx, CListView)
BEGIN_MESSAGE_MAP(CListViewEx, CListView)
//{{AFX_MSG_MAP(CListViewEx)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CListViewEx construction/destruction
CListViewEx::CListViewEx()
{
m_NormalTextColor = ::GetSysColor(COLOR_WINDOWTEXT);
m_NormalBackColor = ::GetSysColor(COLOR_WINDOW);
m_HighlightTextColor = ::GetSysColor(COLOR_HIGHLIGHTTEXT);
m_HighlightBackColor = ::GetSysColor(COLOR_HIGHLIGHT);
m_SelectedTextColor = ::GetSysColor(COLOR_INFOTEXT);
m_SelectedBackColor = ::GetSysColor(COLOR_INACTIVEBORDER);
m_NormalFont.CreatePointFont (80, "MS Sans Serif");
m_BoldFont.CreatePointFont (120, "Arial Bold");
}
CListViewEx::~CListViewEx()
{
}
/////////////////////////////////////////////////////////////////////////////
// CListViewEx drawing
void CListViewEx::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CListCtrl& lc=GetListCtrl();
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
CRect rcItem(lpDrawItemStruct->rcItem);
UINT uiFlags;
CImageList* pImageList;
int nItem = lpDrawItemStruct->itemID;
BOOL bFocus = (GetFocus() == this);
COLORREF clrTextSave;
COLORREF clrImage = m_NormalBackColor;
CRect rcIcon;
pImageList = lc.GetImageList (LVSIL_SMALL);
CRect r;
GetClientRect (r);
rcItem.right = r.right;
LV_ITEM lvi;
lvi.mask = LVIF_IMAGE | LVIF_STATE;
lvi.iItem = nItem;
lvi.iSubItem = 0;
lvi.stateMask = 0xFFFF; // get all state flags
lc.GetItem(&lvi);
CRect rcFullRow;
lc.GetItemRect(nItem, rcFullRow, LVIR_BOUNDS);
CRect rcLabel;
lc.GetItemRect(nItem, rcLabel, LVIR_LABEL);
CRect rcSubLabels = rcFullRow;
rcSubLabels.left = rcLabel.right;
// Show the colors.
//
// There are six cases.
// The item is selected and the view has the focus
// The item is selected and the view does not have
// the focus
// The item has the focus and is not selected and
// the view has the focus
// The item has the focus, is not selected and the
// view does not have the focus
// the item does not have the focus and is not
// selected and the view has the focus
// the item does not have the focus and is not
// selected and the view does not have the focus
//
lc.GetItemRect(nItem, rcIcon, LVIR_ICON);
if (rcIcon.right > 2)
rcFullRow.left += rcIcon.right + TEXT_OFFSET / 2;
if (bFocus) // The view has the focus
{
if (lvi.state & LVIS_FOCUSED)
{
clrTextSave = pDC->SetTextColor(m_HighlightTextColor);
pDC->FillRect(rcFullRow, &CBrush(m_HighlightBackColor));
}
else if (lvi.state & LVIS_SELECTED)
{
clrTextSave = pDC->SetTextColor(m_HighlightTextColor);
pDC->FillRect(rcFullRow, &CBrush(m_HighlightBackColor));
}
else
{
clrTextSave = pDC->SetTextColor(m_NormalTextColor);
pDC->FillRect(rcFullRow, &CBrush(m_NormalBackColor));
}
}
else
{
if ((lvi.state & LVIS_FOCUSED) && (GetStyle() & LVS_SHOWSELALWAYS))
{
pDC->Rectangle (rcFullRow);
pDC->SetTextColor(m_NormalTextColor);
pDC->FillRect(rcFullRow, &CBrush(m_SelectedBackColor));
}
else if ((lvi.state & LVIS_SELECTED) && (GetStyle() & LVS_SHOWSELALWAYS))
{
pDC->SetTextColor(m_NormalTextColor);
pDC->FillRect(rcFullRow, &CBrush(m_SelectedBackColor));
}
else
{
clrTextSave = pDC->SetTextColor(m_NormalTextColor);
pDC->FillRect(rcFullRow, &CBrush(m_NormalBackColor));
}
}
BOOL bSelected = lvi.state & (LVIS_DROPHILITED | LVIS_SELECTED);
//
// Set the color and mask for the icon.
// There are several values you can try:
// ILD_BLEND25 -- Blends 25 percent of
// the system system color.
// ILD_BLEND50 -- Blends 50 percent of
// the system system color.
// ILD_NORMAL -- Draws the image using the
// image list background color.
//
if (lvi.state & LVIS_CUT)
{
clrImage = m_NormalBackColor;
uiFlags |= ILD_SELECTED;
}
else if (bSelected)
{
clrImage = ::GetSysColor(COLOR_HIGHLIGHT);
uiFlags |= ILD_FOCUS;
}
else
{
uiFlags = ILD_TRANSPARENT;
}
//
// Draw the state icon if there is one
//
UINT nStateImageMask = lvi.state & LVIS_STATEIMAGEMASK;
if (nStateImageMask)
{
int nImage = (nStateImageMask>>12) - 1;
pImageList = lc.GetImageList(LVSIL_STATE);
if (pImageList)
{
pImageList->Draw(pDC, nImage,
CPoint(rcItem.left, rcItem.top), uiFlags);
}
}
//
// Draw the icon if there is one
//
pImageList = lc.GetImageList(LVSIL_SMALL);
if (pImageList)
{
lc.GetItemRect(nItem, rcIcon, LVIR_ICON);
UINT nOvlImageMask=lvi.state & LVIS_OVERLAYMASK;
if (rcItem.left<rcItem.right-1)
{
ImageList_DrawEx(pImageList->m_hImageList, lvi.iImage,
pDC->m_hDC,rcIcon.left + 1,rcIcon.top + 1, 16, 16,
m_NormalBackColor, clrImage, uiFlags | nOvlImageMask);
}
}
UINT uiAlign;
uiFlags = DT_SINGLELINE | DT_NOPREFIX | DT_NOCLIP | DT_VCENTER | DT_END_ELLIPSIS;
//
// Draw the text in the columns.
//
LV_COLUMN lvc;
lvc.mask = LVCF_FMT | LVCF_WIDTH;
for(int nColumn = 0; lc.GetColumn(nColumn, &lvc); nColumn++)
{
CString strText;
int nOffset;
switch(lvc.fmt & LVCFMT_JUSTIFYMASK)
{
case LVCFMT_RIGHT:
uiAlign = DT_RIGHT;
break;
case LVCFMT_CENTER:
uiAlign = DT_CENTER;
break;
default:
break;
}
if (!nColumn)
{
if (rcIcon.right > 2)
nOffset = 1;
else
nOffset = 0;
lc.GetItemRect(nItem, rcItem, LVIR_LABEL);
rcLabel = rcItem;
// Override the switch statement. First column is always
// set to the left. (Doesn't have top be; change a needed)
uiAlign = DT_LEFT;
}
else
{
nOffset = 1;
rcItem.left = rcItem.right;
rcItem.right += lvc.cx;
}
strText = lc.GetItemText(nItem, nColumn);
if (strText.IsEmpty())
continue;
rcLabel = rcItem;
//
// Adjust the text rectangle so the text doesn't butt
// up against the edge of the column.
//
rcLabel.left += TEXT_OFFSET * nOffset;
rcLabel.right -= TEXT_OFFSET;
pDC->DrawText(strText, rcLabel, uiAlign | uiFlags);
}
//
// If the item has the focus, draw the focus rectangle
//
if (lvi.state & LVIS_FOCUSED && bFocus)
pDC->DrawFocusRect(rcFullRow);
//
// Restore original colors if item was selected
//
if (bSelected)
{
pDC->SetTextColor(clrTextSave);
}
}
/////////////////////////////////////////////////////////////////////////////
// CListViewEx diagnostics
#ifdef _DEBUG
void CListViewEx::Dump(CDumpContext& dc) const
{
CListView::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// Some color functions. Not needed generally. Cut or modify
// as needed.
// Sets the text color of an unselected, unhighlighted line
void CListViewEx::SetNormalTextColor (COLORREF newTextColor)
{
m_NormalTextColor = newTextColor;
}
// Sets the background color of an unselected, unhighlighted line
void CListViewEx::SetNormalBkgndColor (COLORREF newBackColor)
{
m_NormalBackColor = newBackColor;
//
// This is the general background color of the control, so
// set that now.
GetListCtrl().SetBkColor (newBackColor);
}
// Sets the text color of a highlighteded line
void CListViewEx::SetHiliteTextColor (COLORREF newTextColor)
{
m_HighlightTextColor = newTextColor;
}
// Sets the background color of a highlighteded line
void CListViewEx::SetHiliteBkgndColor (COLORREF newBackColor)
{
m_HighlightBackColor = newBackColor;
}
// Sets the text color of a selected line
void CListViewEx::SetSelectedTextColor (COLORREF newTextColor)
{
m_SelectedTextColor = newTextColor;
}
// Sets the background color of a selected line
void CListViewEx::SetSelectedBkgndColor (COLORREF newBackColor)
{
m_SelectedBackColor = newBackColor;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -