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

📄 listvwex.cpp

📁 我上载了那么多怎么都说已经有上载的啦
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// ListVwEx.cpp : implementation file
// Written By: zeng fanfeng

#include "stdafx.h"
#include "GPMIS.h"
#include "ListVwEx.h"
#include "mainfrm.h"
#include "ComboItem.h"

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

/////////////////////////////////////////////////////////////////////////////
// CListVwEx

IMPLEMENT_DYNCREATE(CListVwEx, CListEditView)

CListVwEx::CListVwEx(DWORD dwMyStyle) : CListEditView(), m_nSortedColumn(-1), 
m_bSortAscending(TRUE),m_nCompareAs(ELCT_STRING_CASE),m_nDateTimeType(0)
{
	m_dwMyStyle = dwMyStyle;

	for( int i = 0; i<64; i++ )
	{
		m_bCanEdit[i] = FALSE;
		m_nColumnType[i] = ELCT_STRING_CASE;
		m_nColumnCtrl[i] = eLast;
	}

	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_bSortFirstCol = FALSE;

	m_subitem = 0;
}

CListVwEx::~CListVwEx()
{
	int nSize = m_RowClrArray.GetSize();
	for(int i = 0; i < nSize; i++)
	{
		COLORREF* p = (COLORREF*)m_RowClrArray.GetAt(i);
		delete p;
	}
	m_RowClrArray.RemoveAll();
}


BEGIN_MESSAGE_MAP(CListVwEx, CListEditView)
	//{{AFX_MSG_MAP(CListVwEx)
	ON_WM_LBUTTONDOWN()
	ON_WM_SIZE()
	ON_WM_SETFOCUS()
	ON_WM_PAINT()
	ON_WM_KILLFOCUS()
	ON_NOTIFY(HDN_ITEMCLICKA, 0, OnHeaderClicked)
	ON_NOTIFY(HDN_ITEMCLICKW, 0, OnHeaderClicked)
	ON_WM_LBUTTONDBLCLK()
	//}}AFX_MSG_MAP
	ON_MESSAGE(LVM_SETIMAGELIST, OnSetImageList)
	ON_MESSAGE(LVM_SETTEXTCOLOR, OnSetTextColor)
	ON_MESSAGE(LVM_SETTEXTBKCOLOR, OnSetTextBkColor)
	ON_MESSAGE(LVM_SETBKCOLOR, OnSetBkColor)

	ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTW, 0, 0xFFFF, OnToolTipText)
	ON_NOTIFY_EX_RANGE(TTN_NEEDTEXTA, 0, 0xFFFF, OnToolTipText)

	ON_NOTIFY_REFLECT(LVN_BEGINLABELEDIT, OnBeginLabelEdit)
	ON_NOTIFY_REFLECT(LVN_ENDLABELEDIT, OnEndLabelEdit)

	ON_NOTIFY_REFLECT(NM_CLICK, OnClick)
	ON_NOTIFY_REFLECT(LVN_KEYDOWN, OnKeydown)
	
END_MESSAGE_MAP()

BOOL CListVwEx::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 
{
	// TODO: Add your specialized code here and/or call the base class
	dwStyle |= m_dwMyStyle;
	
	return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
}

/////////////////////////////////////////////////////////////////////////////
// CListVwEx drawing

void CListVwEx::OnDraw(CDC* pDC)
{
	CDocument* pDoc = GetDocument();
	// TODO: add draw code here
}

BOOL CListVwEx::PreCreateWindow(CREATESTRUCT& cs) 
{
	// TODO: Add your specialized code here and/or call the base class
	cs.style &= ~LVS_TYPEMASK;
	cs.style |= LVS_REPORT | LVS_OWNERDRAWFIXED ;
	m_bFullRowSel = TRUE;
	
	return CListEditView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CListVwEx diagnostics

#ifdef _DEBUG
void CListVwEx::AssertValid() const
{
	CListEditView::AssertValid();
}

void CListVwEx::Dump(CDumpContext& dc) const
{
	CListEditView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CListVwEx message handlers
LRESULT CListVwEx::OnSetImageList(WPARAM wParam, LPARAM lParam)
{
	if( (int) wParam == LVSIL_STATE)
	{
		int cx, cy;

		if(::ImageList_GetIconSize((HIMAGELIST)lParam, &cx, &cy))
			m_cxStateImageOffset = cx;
		else
			m_cxStateImageOffset = 0;
	}

	return(Default());
}

LRESULT CListVwEx::OnSetTextColor(WPARAM wParam, LPARAM lParam)
{
	m_clrText = (COLORREF)lParam;
	return(Default());
}

LRESULT CListVwEx::OnSetTextBkColor(WPARAM wParam, LPARAM lParam)
{
	m_clrTextBk = (COLORREF)lParam;
	return(Default());
}

LRESULT CListVwEx::OnSetBkColor(WPARAM wParam, LPARAM lParam)
{
	m_clrBkgnd = (COLORREF)lParam;
	return(Default());
}

void CListVwEx::OnSize(UINT nType, int cx, int cy) 
{
	//stop editing if resizing
	if( GetFocus() != this ) SetFocus();

	m_cxClient = cx;
	CListEditView::OnSize(nType, cx, cy);
}

void CListVwEx::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
//	CEditableListCtrl& ListCtrl = (CEditableListCtrl&)GetListCtrl();
	CListEditView::OnLButtonDown(nFlags, point);
}

void CListVwEx::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	LVHITTESTINFO	lvhit;
	lvhit.pt = point;
	int item = GetListCtrl().SubItemHitTest(&lvhit);

	//if (over a subitem)
	if (item != -1 && lvhit.iSubItem && (lvhit.flags & LVHT_ONITEM ))
	{
	//mouse click outside the editbox in an allready editing cell cancels editing
		if (m_subitem == lvhit.iSubItem && item == m_item)
		{
			CListEditView::OnLButtonDblClk(nFlags, point);
		}
		else
		{
			CListEditView::OnLButtonDblClk(nFlags, point);

			m_subitem = lvhit.iSubItem;
			m_item = item;
			if( m_bCanEdit[m_subitem] )
			{
				GetListCtrl().EditLabel(item);
			}
			else
			{
				m_subitem = 0;
			}
		}

	}
	else
		CListEditView::OnLButtonDblClk(nFlags, point);
	
}

void CListVwEx::OnBeginLabelEdit(NMHDR* pNMHDR, LRESULT* pResult) 
{
	LV_DISPINFO* pDispInfo = (LV_DISPINFO*)pNMHDR;

	//if (subitem selected for editing)
	if (m_subitem >= 0 && m_bCanEdit[m_subitem])
	{
		ASSERT(m_item == pDispInfo->item.iItem);

		CRect	subrect;
		GetListCtrl().GetSubItemRect( pDispInfo->item.iItem, m_subitem, LVIR_BOUNDS , subrect );

		m_strSubItem = GetListCtrl().GetItemText(pDispInfo->item.iItem, m_subitem);

		//get edit control and subclass
		HWND hWnd=(HWND)SendMessage(LVM_GETEDITCONTROL);
		ASSERT(hWnd!=NULL);
		VERIFY(m_editWnd.SubclassWindow(hWnd));

		//move edit control text 1 pixel to the right of org label, as Windows does it...
		m_editWnd.m_x = subrect.left + 6; 
		m_editWnd.SetWindowText(GetListCtrl().GetItemText(pDispInfo->item.iItem,m_subitem));

		//hide subitem text so it don't show if we delete some text in the editcontrol
		//OnPaint handles other issues also regarding this
		CRect	rect;
		GetListCtrl().GetSubItemRect(pDispInfo->item.iItem,m_subitem,LVIR_LABEL ,rect);
		CDC* hDc = GetDC();
		hDc->FillRect(rect,&CBrush(::GetSysColor(COLOR_WINDOW)));
		ReleaseDC(hDc);
		
		GetListCtrl().SetItemText(pDispInfo->item.iItem,m_subitem, "");

		*pResult = FALSE;
	}
	else
	{
		*pResult = TRUE;
	}

	//return: editing permitted
}

void CListVwEx::OnEndLabelEdit(NMHDR* pNMHDR, LRESULT* pResult) 
{
	LV_DISPINFO *plvDispInfo = (LV_DISPINFO *)pNMHDR;
	LV_ITEM	*plvItem = &plvDispInfo->item;

	//if (end of sub-editing) do cleanup
	if (m_subitem && m_bCanEdit[m_subitem])
	{
		//plvItem->pszText is NULL if editing canceled
		if (plvItem->pszText != NULL )
			GetListCtrl().SetItemText(plvItem->iItem,m_subitem, plvItem->pszText);
		else
			GetListCtrl().SetItemText(plvItem->iItem,m_subitem, m_strSubItem);

		m_strSubItem = "";
		
		VERIFY(m_editWnd.UnsubclassWindow()!=NULL);
		
		m_subitem = 0;
		//allways revert to org label (Windows thinks we are editing the leftmost item)
		*pResult = 0;
	}
	else
		//return: update label on leftmost item
		*pResult = 1;

}

void CListVwEx::OnPaint() 
{
	// in full row select mode, we need to extend the clipping region
	// so we can paint a selection all the way to the right
	if (m_bClientWidthSel &&
		(GetStyle() & LVS_TYPEMASK) == LVS_REPORT &&
		GetFullRowSel())
	{
		CRect rcAllLabels;
		GetListCtrl().GetItemRect(0, rcAllLabels, LVIR_BOUNDS);

		if(rcAllLabels.right < m_cxClient)
		{
			// need to call BeginPaint (in CPaintDC c-tor)
			// to get correct clipping rect
			CPaintDC dc(this);

			CRect rcClip;
			dc.GetClipBox(rcClip);

			rcClip.left = min(rcAllLabels.right-1, rcClip.left);
			rcClip.right = m_cxClient;

			InvalidateRect(rcClip, FALSE);
			// EndPaint will be called in CPaintDC d-tor
		}
	}

	if (m_subitem)
	{
		CRect	rect;
		CRect	editrect;

		GetListCtrl().GetSubItemRect(m_item,m_subitem,LVIR_LABEL ,rect);
		
		m_editWnd.GetWindowRect(editrect);
		ScreenToClient(editrect);

		//block text redraw of the subitems text (underneath the editcontrol)
		//if we didn't do this and deleted some text in the edit control, 
		//the subitems original label would show
		if (editrect.right < rect.right)
			{
			rect.left = editrect.right ;
			ValidateRect(rect);
			}
		
		//block filling redraw of leftmost item (caused by FillRect)
		GetListCtrl().GetItemRect(m_item,rect,LVIR_LABEL );
		ValidateRect(rect);
	}
	CListEditView::OnPaint();
}

void CListVwEx::OnSetFocus(CWnd* pOldWnd) 
{
	CListEditView::OnSetFocus(pOldWnd);

	// check if we are getting focus from label edit box
	if(pOldWnd!=NULL && pOldWnd->GetParent()==this)
		return;

	// repaint items that should change appearance
	if(m_bFullRowSel && (GetStyle() & LVS_TYPEMASK)==LVS_REPORT)
		RepaintSelectedItems();
}

void CListVwEx::OnKillFocus(CWnd* pNewWnd) 
{
	CListEditView::OnKillFocus(pNewWnd);

	// check if we are losing focus to label edit box
	if(pNewWnd != NULL && pNewWnd->GetParent() == this)
		return;

	// repaint items that should change appearance
	if(m_bFullRowSel && (GetStyle() & LVS_TYPEMASK) == LVS_REPORT)
		RepaintSelectedItems();
}

BOOL CListVwEx::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 CListVwEx::GetFullRowSel()
{
	return(m_bFullRowSel);
}

/////////////////////////////////////////////////////////////////////////////
// CListVwEx drawing

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

void CListVwEx::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
	CListCtrl& ListCtrl = GetListCtrl();
	CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
	CRect rcItem(lpDrawItemStruct->rcItem);
	UINT uiFlags = ILD_TRANSPARENT;
	CImageList* pImageList;
	int nItem = lpDrawItemStruct->itemID;
	BOOL bFocus = (GetFocus() == this);
	COLORREF clrTextSave, clrBkSave;
	COLORREF clrImage = m_clrBkgnd;
	static _TCHAR szBuff[MAX_PATH];
	LPCTSTR pszText;
	static _TCHAR szBuffer[MAX_PATH];

// get item data
	LV_ITEM lvi;
	lvi.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_STATE;
	lvi.iItem = nItem;
	lvi.iSubItem = 0;
	lvi.pszText = szBuff;
	lvi.cchTextMax = sizeof(szBuff);
	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);

// set colors if item is selected

	CRect rcAllLabels;

⌨️ 快捷键说明

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