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

📄 listviewex.cpp

📁 一个邮件客户端源代码,包括收发邮件,安排日程等很多内容
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// Copyright (C) 1997-2002 Valeriy Ovechkin
// 
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
// ListVwEx.cpp : implementation of the CListViewEx class
//
// This is a part of the Microsoft Foundation Classes C++ library.
// Copyright (C) 1992-1996 Microsoft Corporation
// All rights reserved.
//
// This source code is only intended as a supplement to the
// Microsoft Foundation Classes Reference and related
// electronic documentation provided with the library.
// See these sources for detailed information regarding the
// Microsoft Foundation Classes product.

#include "stdafx.h"
#include "ListViewEx.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CListViewEx

IMPLEMENT_DYNCREATE(CListViewEx, CListView)

BEGIN_MESSAGE_MAP(CListViewEx, CListView)
	//{{AFX_MSG_MAP(CListViewEx)
	ON_WM_SIZE()
	ON_WM_PAINT()
	ON_WM_SETFOCUS()
	ON_WM_KILLFOCUS()
	ON_WM_RBUTTONDOWN()
	ON_WM_RBUTTONUP()
	ON_WM_ERASEBKGND()
	//}}AFX_MSG_MAP
	ON_MESSAGE(LVM_SETIMAGELIST, OnSetImageList)
	ON_MESSAGE(LVM_SETTEXTCOLOR, OnSetTextColor)
	ON_MESSAGE(LVM_SETTEXTBKCOLOR, OnSetTextBkColor)
	ON_MESSAGE(LVM_SETBKCOLOR, OnSetBkColor)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CListViewEx construction/destruction

CListViewEx::CListViewEx()
{
	m_bFullRowSel=FALSE;
	m_bClientWidthSel=TRUE;

	m_cxClient=0;
	m_cxStateImageOffset=0;

	m_clrText=::GetSysColor(COLOR_WINDOWTEXT);
	m_clrTextBk=::GetSysColor(COLOR_WINDOW);
	m_clrBkgnd=::GetSysColor(COLOR_WINDOW);
	
	m_dwLastRButtonDown = 0;
}

CListViewEx::~CListViewEx()
{
}

BOOL CListViewEx::PreCreateWindow(CREATESTRUCT& cs)
{
	// default is report view and full row selection
	cs.style&=~LVS_TYPEMASK;
	cs.style|=LVS_REPORT | LVS_OWNERDRAWFIXED;
	m_bFullRowSel=TRUE;

	return(CListView::PreCreateWindow(cs));
}

BOOL CListViewEx::SetFullRowSel(BOOL bFullRowSel)
{
	// no painting during change
	LockWindowUpdate();

	m_bFullRowSel=bFullRowSel;

	BOOL bRet;

	if(m_bFullRowSel)
		bRet=ModifyStyle(0L,LVS_OWNERDRAWFIXED);
	else
		bRet=ModifyStyle(LVS_OWNERDRAWFIXED,0L);

	// repaint window if we are not changing view type
	if(bRet && (GetStyle() & LVS_TYPEMASK)==LVS_REPORT)
		Invalidate();

	// repaint changes
	UnlockWindowUpdate();

	return(bRet);
}

BOOL CListViewEx::GetFullRowSel()
{
	return(m_bFullRowSel);
}

/////////////////////////////////////////////////////////////////////////////
// CListViewEx drawing
void CListViewEx::GetItemColors(int nItem, COLORREF& clrText, 
								COLORREF& clrBk, BOOL bSelected)
{
	BOOL bFocus=(GetFocus()==this);
	if (bSelected)
	{
		clrText = ::GetSysColor(COLOR_HIGHLIGHTTEXT);
		clrBk = bFocus ? ::GetSysColor(COLOR_HIGHLIGHT) : ::GetSysColor(COLOR_3DFACE);
	}
}

// offsets for first and other columns
#define OFFSET_FIRST	2
#define OFFSET_OTHER	3

void CListViewEx::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
	CListCtrl& ListCtrl=GetListCtrl();
	int nColumns=ListCtrl.GetHeaderCtrl()->GetItemCount();
	int nColumn=0;
	int nOffset=OFFSET_FIRST;
	CDC* pDC=CDC::FromHandle(lpDrawItemStruct->hDC);
	CRect rcColumn;
	CRect rcItem(lpDrawItemStruct->rcItem);
	UINT uiFlags=ILD_TRANSPARENT;
	UINT nJustify=DT_LEFT;
	CImageList* pImageList;
	int nItem=lpDrawItemStruct->itemID;
	BOOL bFocus=(GetFocus()==this);
	COLORREF clrTextSave(0), clrBkSave(0);
	COLORREF clrImage=m_clrBkgnd;
	static _TCHAR szBuff[MAX_PATH];

	// get item data
	LV_ITEM lvi;
	lvi.mask= LVIF_IMAGE | LVIF_STATE;
	lvi.iItem=nItem;
	lvi.iSubItem=0;
	lvi.pszText=0;
	lvi.cchTextMax=0;
	lvi.stateMask=0xFFFF;		// get all state flags
	ListCtrl.GetItem(&lvi);

	BOOL bSelected=(bFocus || (GetStyle() & LVS_SHOWSELALWAYS)) && lvi.state & LVIS_SELECTED;
	bSelected=bSelected || (lvi.state & LVIS_DROPHILITED);

	LVCOLUMN lvc;
	lvc.mask=LVCF_FMT | LVCF_WIDTH | LVCF_ORDER;
	CRect rcAllLabels;
	ListCtrl.GetItemRect(nItem,rcAllLabels,LVIR_BOUNDS);

	// set colors if item is selected
	COLORREF clrText = GetSysColor(COLOR_WINDOWTEXT);
	COLORREF clrBk = GetSysColor(COLOR_WINDOW);
	GetItemColors(nItem, clrText, clrBk, bSelected);


	CBrush brush(clrBk);
	pDC->FillRect(rcAllLabels, &brush);

	clrTextSave = pDC->SetTextColor(clrText);
	clrBkSave   = pDC->SetBkColor(clrBk);

	// set color and mask for the icon
	if(lvi.state & LVIS_CUT)
	{
		clrImage = m_clrBkgnd;
		uiFlags |= ILD_SELECTED;
	}
	else if(bSelected)
	{
		clrImage = ::GetSysColor(COLOR_HIGHLIGHT);
		uiFlags |= ILD_SELECTED;
	}

	int nLastColRight = 0;
	for(int nIndex=0; nIndex<nColumns; nIndex++)
	{
		nColumn = ListCtrl.GetHeaderCtrl()->OrderToIndex(nIndex);
		ListCtrl.GetColumn(nColumn,&lvc);
		ListCtrl.GetHeaderCtrl()->GetItemRect(nColumn, &rcColumn);
		rcItem.left=rcColumn.left+nOffset;
		rcItem.right=rcColumn.right-nOffset;

		if (nIndex > 0)
		{
			switch(lvc.fmt & LVCFMT_JUSTIFYMASK)
			{
			case LVCFMT_RIGHT:
				nJustify=DT_RIGHT;
				break;
			case LVCFMT_CENTER:
				nJustify=DT_CENTER;
				break;
			default:
				nJustify=DT_LEFT;
				break;
			}
		}

		if (nColumn == 0)
		{
			// draw state icon
			UINT nStateImageMask=lvi.state & LVIS_STATEIMAGEMASK;
			if(nStateImageMask)
			{
				int nImage=(nStateImageMask>>12)-1;
				pImageList=ListCtrl.GetImageList(LVSIL_STATE);
				if(pImageList)
					pImageList->Draw(pDC,nImage,CPoint(rcItem.left,rcItem.top),ILD_TRANSPARENT);
			}

			// draw normal and overlay icon
			CRect rcIcon;
			ListCtrl.GetItemRect(nItem,rcIcon,LVIR_ICON);

			pImageList=ListCtrl.GetImageList(LVSIL_SMALL);
			if(pImageList)
			{
				UINT nOvlImageMask=lvi.state & LVIS_OVERLAYMASK;
				if(rcItem.left<rcItem.right-1)
					ImageList_DrawEx(pImageList->m_hImageList,lvi.iImage,pDC->m_hDC,rcIcon.left,rcIcon.top,16,16,m_clrBkgnd,clrImage,uiFlags | nOvlImageMask);
			}

			// draw item label
			ListCtrl.GetItemRect(nItem,rcItem,LVIR_LABEL);
			rcItem.right-=m_cxStateImageOffset;
			nLastColRight = rcItem.right;
		}
		else
		{
			rcItem.right = nLastColRight + rcItem.Width() + 2*nOffset;
			rcItem.left = nLastColRight + nOffset*2;
			nLastColRight = rcItem.right;
		}
		
		DrawSubItem(pDC, nItem, nColumn, rcItem, nJustify, bSelected);
		nOffset = OFFSET_OTHER;
	}

	// draw focus rectangle if item has focus
	if(lvi.state & LVIS_FOCUSED && bFocus)
	{
		pDC->DrawFocusRect(rcAllLabels);
	}

	// set original colors if item was selected
	//if(bSelected)
	{
		pDC->SetTextColor(clrTextSave);
		pDC->SetBkColor(clrBkSave);
	}
}
int CListViewEx::GetItemText(int nItem, int nColumn, LPTSTR pBuf, int nLen)
{
	if (m_bUseMapping)
	{
		nColumn = GetVisibleColIndex(nColumn);
		if (nColumn<0)
			return 0;
	}
	if (nColumn>0)
	{
		return GetListCtrl().GetItemText(nItem,nColumn,pBuf, nLen);
	}
	// main item text
	LV_ITEM lvi;
	lvi.mask=LVIF_TEXT;
	lvi.iItem=nItem;
	lvi.iSubItem=0;
	lvi.pszText=pBuf;
	lvi.cchTextMax=nLen;
	lvi.stateMask=0;		// get all state flags
	GetListCtrl().GetItem(&lvi);
	return strlen(pBuf);
}

void CListViewEx::DrawSubItem(CDC* pDC, int nItem, int nColumn, CRect& rcLabel, int nJustify, BOOL )
{
	static _TCHAR szBuff[MAX_PATH];

⌨️ 快捷键说明

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