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

📄 listctrlstyled.cpp

📁 网络分析工具
💻 CPP
📖 第 1 页 / 共 4 页
字号:
/*

  ListCtrlStyled.cpp

  This file is part of the Packet Edit Studio Project.
  version: ALPHA

  * THIS IS THIRD PARTY SOURCE CODE *

  *Distributed under the terms of the zlib/libpng public license.*
  
  Author: 

	S閎astien ANDRE (codeguru.com)        - CListCtrlStyled Class
	(http://www.codeguru.com/cpp/controls/listview/article.php/c4189/)

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.
*/

// The packet list view is based on this class
//
// This file has NOT been edited other than the addition of the above comments


// ListCtrlStyled.cpp : implementation file
//

#include "stdafx.h"

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

CListCtrlStyled::CListCtrlStyled()
{ 	columns.SetSize(0);
	columns.RemoveAll();

	m_Default_pCFont = NULL;

	// Set default HIGHLIGHT colors
	//
	m_highlighttext = ::GetSysColor(COLOR_HIGHLIGHTTEXT);
	m_highlight = ::GetSysColor(COLOR_HIGHLIGHT);
}

CListCtrlStyled::~CListCtrlStyled()
{ 	// Must free if it's not alreay do
	//
	int nCols = (int)columns.GetSize();
	for(int nCol = 0; nCol < nCols;nCol++)
	{	if(columns[nCol] != NULL)
			this->Free_LS_item(columns[nCol]);
	}

	columns.RemoveAll();
	columns.SetSize(0);
}

BEGIN_MESSAGE_MAP(CListCtrlStyled, CListCtrl)
    //{{AFX_MSG_MAP(CListCtrlStyled)
    ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomdraw)
	ON_WM_DESTROY()
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

void CListCtrlStyled::OnDestroy()
{	// Must free all internal objects for prevent memory leaks
	//
	this->DeleteAllItems();
	CListCtrl::OnDestroy();
}

// ****************
// ** InsertItem **
// ****************
//
int CListCtrlStyled::InsertItem( const LVITEM* pItem )
{	// So we need to construct our style object for this item, store
	// it into the lParam of pItem. But before this we must store it's lParam
	// for mask this usage for the programmer. lparam must be usable by the same
	// way that the original CListCtrl. then we store it into our style object

	// For let after to modify lParam (pItem is a const pointer)
	//
	LVITEM pItem2 = (LVITEM) *pItem;

	// Create a new LS_item for store data
	//
	LS_item * lpLS_item;
	lpLS_item = NULL;
	lpLS_item = new LS_item;

	Init_LS_item(lpLS_item);		// Init the structure

	// Store lParam from the pItem
	//
	if(pItem2.mask & LVIF_PARAM)
	{	lpLS_item->lParam = pItem2.lParam;
		lpLS_item->mParam = true;
	}

	// Set the LS_Item to the lParam of pItem
	//
	pItem2.mask = pItem2.mask | LVIF_PARAM;				// Add LVIF_PARAM option
	pItem2.lParam = (LPARAM)(void *)lpLS_item;			// Link our structure

	// Create structure for each subitems (with default style too)
	//
	int nSubItems = this->GetHeaderCtrl()->GetItemCount() - 1;

	LS_item * lpLS_subitem;
	for(int bcl = 0; bcl < nSubItems ;bcl++)
	{	lpLS_subitem = NULL;
		lpLS_subitem = new LS_item;

		Init_LS_item(lpLS_subitem,false);				// Init the structure

		lpLS_item->subitems.Add(lpLS_subitem);			// Add the structure of this subitem to the list.
	}

	return (CListCtrl::InsertItem( &pItem2 ));
}

int CListCtrlStyled::InsertItem( UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam )
{
	// So we must create an LVItem and insert it
	LVITEM lvitem;
	lvitem.mask = nMask;
	lvitem.iItem = nItem;
	lvitem.iSubItem = 0;
	lvitem.pszText = (char*)lpszItem;
	lvitem.cchTextMax = 0;
	if(nMask & LVIF_TEXT)
		lvitem.cchTextMax = (int)strlen(lvitem.pszText);
	lvitem.state = nState;
	lvitem.stateMask = nStateMask;
	lvitem.iImage = nImage;
	lvitem.lParam = lParam;

	// Insert it
	return (this->InsertItem(&lvitem));
}

int CListCtrlStyled::InsertItem( int nItem, LPCTSTR lpszItem )
{ return (this->InsertItem(LVIF_TEXT,nItem,lpszItem,0,0,0,0)); }

int CListCtrlStyled::InsertItem( int nItem, LPCTSTR lpszItem, int nImage )
{ return (this->InsertItem(LVIF_TEXT|LVIF_IMAGE,nItem,lpszItem,0,0,nImage,0)); }

// *************
// ** SetItem **
// *************

BOOL CListCtrlStyled::SetItem( const LVITEM* pItem )
{
	if(pItem->mask & LVIF_PARAM)
	{	// We must assume that lParam member is correctly managed in our derived class
		//
		LVITEM pItem2 = (LVITEM) *pItem;

		// Try to retrieve the root pItem
		//
		LVITEM pRootItem;
		InitLVITEM(pItem2.iItem,0,&pRootItem);

		LS_item * lpLS_item = NULL;
		lpLS_item = (LS_item *)pRootItem.lParam;

		// Management of lParam
		//
		lpLS_item->lParam = pItem2.lParam;
		lpLS_item->mParam = true;

		// No lParam for this subitem
		//
		pItem2.lParam = NULL;
		pItem2.mask = pItem2.mask & (0xFFFF - LVIF_PARAM);

		return (CListCtrl::SetItem( &pItem2 ));
	}

	return (CListCtrl::SetItem( pItem ));
}


BOOL CListCtrlStyled::SetItem(int nItem,int nSubItem,UINT nMask,LPCTSTR lpszItem,int nImage,UINT nState,UINT nStateMask,LPARAM lParam)
{	return (this->SetItem(nItem,nSubItem,nMask,lpszItem,nImage,nState,nStateMask,lParam,0)); }

BOOL CListCtrlStyled::SetItem(int nItem,int nSubItem,UINT nMask,LPCTSTR lpszItem,int nImage,UINT nState,UINT nStateMask,LPARAM lParam,int nIndent)
{
	// So we must create an LVItem and insert it
	LVITEM lvitem;
	lvitem.mask = nMask;
	lvitem.iItem = nItem;
	lvitem.iSubItem = nSubItem;
	lvitem.pszText = (char*)lpszItem;
	lvitem.cchTextMax = 0;
	if(nMask & LVIF_TEXT)
		lvitem.cchTextMax = (int)strlen(lvitem.pszText);
	lvitem.state = nState;
	lvitem.stateMask = nStateMask;
	lvitem.iImage = nImage;
	lvitem.lParam = lParam;
	lvitem.iIndent = nIndent;

	// Insert it
	return (this->SetItem(&lvitem));
}

// ****************
// ** DeleteItem **
// ****************
BOOL CListCtrlStyled::DeleteItem( int nItem )
{
	// We must delete all LS_item before deleting this Item
	// So we must retrieve it for all columns
	//

	// retrieve the LS_item structure for this item
	//
	LVITEM pItem;
	InitLVITEM(nItem,0,&pItem);

	LS_item * lpLS_item = NULL;
	lpLS_item = (LS_item *)pItem.lParam;

	// Free his structure style
	//
	this->Free_LS_item(lpLS_item);

	return (CListCtrl::DeleteItem(nItem));
}

BOOL CListCtrlStyled::DeleteAllItems()
{
	// Get number of Rows
	//
	int nItems = CListCtrl::GetItemCount();

	// Delete Each row (One by One for managed our structure destruction)
	//
	for(int nItem = 0; nItem < nItems; nItem++)
		this->DeleteItem(0);

	// Call the base class DeleteAllItems (maybe some treatments must be do)
	//
	return (CListCtrl::DeleteAllItems());
}

// *********************
// ** Get/SetItemData **
// *********************
DWORD CListCtrlStyled::GetItemData( int nItem )
{	// retrieve the LS_item structure
	//
	LVITEM pItem;
	InitLVITEM(nItem,0,&pItem);

	LS_item * lpLS_item = NULL;
	lpLS_item = (LS_item *)pItem.lParam;

	if(lpLS_item->mParam)
		return (DWORD)(lpLS_item->lParam);
	else
		return NULL;	// No significant if the item mask hasn't LVIF_PARAM
}

BOOL CListCtrlStyled::SetItemData( int nItem, DWORD dwData )
{
	// Retrieve the LS_item structure
	//
	LVITEM pItem;
	InitLVITEM(nItem,0,&pItem);

	LS_item * lpLS_item = NULL;
	lpLS_item = (LS_item *)pItem.lParam;

	// set the new data
	//
	if(lpLS_item->mParam)
		lpLS_item->lParam = dwData;

	// Return false if the LVIF_PARAM isn't set for this item
	//
	return (lpLS_item->mParam);
}

// *************
// ** GetItem **
// *************
BOOL CListCtrlStyled::GetItem( LV_ITEM* pItem )
{
	/*
	typedef struct _LV_ITEM {
    UINT   mask;
    int    iItem;
    int    iSubItem;
    UINT   state;
    UINT   stateMask;
    LPSTR  pszText;
    int    cchTextMax;
    int    iImage;
    LPARAM lParam;       // 32-bit value to associate with item
	} LV_ITEM;
  */

	// Retrieve Information from Base class
	//
	BOOL Result = CListCtrl::GetItem(pItem);
	if(Result)
	{	// Replace the lParam value
		//
		LS_item * lpLS_item = NULL;
		lpLS_item = (LS_item *)pItem->lParam;

		if(lpLS_item->mParam)
		{	// Ok set the original lParam
			//
			pItem->lParam = lpLS_item->lParam;
		}
		else
		{	// No lParam member
			//
			pItem->lParam = NULL;
			pItem->mask = pItem->mask & (0xFFFF - LVIF_PARAM);
		}
	}

	return Result;
}

// *********************************
// ** InsertColumn / DeleteColumn **
// *********************************
int CListCtrlStyled::InsertColumn( int nCol, const LVCOLUMN* pColumn )
{
	int index = CListCtrl::InsertColumn(nCol,pColumn);
	if(index == -1)
		return index;

	// Adjust Size of columns Array
	//
	this->columns.SetSize( this->GetHeaderCtrl()->GetItemCount() );

	// Must update the "columns" array for hold a custom column style if needed
	//
	this->columns.InsertAt(index,NULL,1);

	// We must adjust all LS_item into the CListCtrl
	//
	int nRows = CListCtrl::GetItemCount();

	// Variables needed
	LVITEM pItem;
	LS_item * lpLS_item;
	LS_item * lpLS_subitem;

	for(int nItem = 0; nItem < 	nRows; nItem++)
	{	// Retrieve the LS_item for this item
		//
		InitLVITEM(nItem,0,&pItem);

		lpLS_item = NULL;
		lpLS_item = (LS_item *)pItem.lParam;

		// Create the style for the new subitem
		//
		lpLS_subitem = NULL;
		lpLS_subitem = new LS_item;

		this->Init_LS_item(lpLS_subitem,false);  // Init the structure

		// Insert it into his correct place (shift all element above)
		//
		if(index > 0)
			lpLS_item->subitems.InsertAt(index - 1,lpLS_subitem,1);
		else
			lpLS_item->subitems.InsertAt(index,lpLS_subitem,1);
	}

	return index;
}

int CListCtrlStyled::InsertColumn( int nCol, LPCTSTR lpszColumnHeading, int nFormat, int nWidth, int nSubItem)
{	// Create an LVCOLUMN item
	/*
	typedef struct _LVCOLUMN {
   	 UINT mask;
   	 int fmt;
   	 int cx;
   	 LPTSTR pszText;
   	 int cchTextMax;
   	 int iSubItem;
	#if (_WIN32_IE >= 0x0300)
   	 int iImage;
   	 int iOrder;
	#endif
	} LVCOLUMN, *LPLVCOLUMN;
	*/
	LVCOLUMN lvColumn;

	lvColumn.mask = LVCF_FMT | LVCF_TEXT;
	if(nWidth != -1)
		lvColumn.mask = lvColumn.mask | LVCF_WIDTH;
	if(nSubItem != -1)
		lvColumn.mask = lvColumn.mask | LVCF_SUBITEM;

	lvColumn.fmt = nFormat;
	lvColumn.cx = nWidth;
	lvColumn.pszText = (char*)lpszColumnHeading;
	lvColumn.cchTextMax  = (int)strlen( lvColumn.pszText );
	lvColumn.iSubItem = nSubItem;

	return (this->InsertColumn(nCol,&lvColumn));
}

BOOL CListCtrlStyled::DeleteColumn( int nCol )
{
	// Start be delete the Column Style if needed
	//
	LS_item * lpLS_col = NULL;
	lpLS_col = this->columns[nCol];

	if(lpLS_col != NULL)
		this->Free_LS_item(lpLS_col);

	// Update the Array
	//
	columns.RemoveAt(nCol,1);

	// Some Subitems will be deleted, we must delete also theses structure style
	//
	int nItems = CListCtrl::GetItemCount();
	for(int nItem = 0;nItem < nItems;nItem++)
	{ 	LVITEM pItem;
		InitLVITEM(nItem,0,&pItem);

		LS_item * lpLS_root = NULL;
		LS_item * lpLS_item = NULL;
		lpLS_root = (LS_item*) pItem.lParam;

		if(nCol > 0)
		{	lpLS_item = lpLS_root->subitems[nCol - 1];
			lpLS_root->subitems.RemoveAt(nCol - 1,1);
		}
		else
		{	if( this->GetHeaderCtrl()->GetItemCount() > 1)
			{	lpLS_item = lpLS_root->subitems[0];
				lpLS_root->subitems.RemoveAt(0,1);
			}
			else
			{	this->DeleteAllItems();
				return (CListCtrl::DeleteColumn(nCol ));
			}
		}

		this->Free_LS_item(lpLS_item);
	}

	// We can delete the Column now
	//
	return (CListCtrl::DeleteColumn(nCol));
}

// *************************
// ** SetColumnOrderArray **
// *************************
BOOL CListCtrlStyled::SetColumnOrderArray( int iCount, LPINT piArray )
{	// Must rearrange the "columns" array
	//
	CArray<struct iLS_item * , struct iLS_item *> old;

	old.SetSize( columns.GetSize() );
	old.RemoveAll();
	old.Copy( this->columns );

	for( int nItem = 0; nItem < iCount; nItem++)
		this->columns.SetAt( nItem, old[ piArray[nItem] ] );

	old.RemoveAll();

	return (CListCtrl::SetColumnOrderArray(iCount,piArray));
}

// ***************
// ** SortItems **
// ***************

PFNLVCOMPARE LIS_CompFunc;															// Store the user-defined callback function

int CALLBACK LIS_CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{	// Must call the origine compare function
	//
	LPARAM lp1;					// the real lParam1
	LPARAM lp2;					// the real lParam2
	LS_item * lpLS_item;

	// Set the correct lParam1
	//
	lpLS_item = NULL;
	lpLS_item = (LS_item*) lParam1;
	lp1 = lpLS_item->lParam;

	// Set the correct lParam2
	//
	lpLS_item = NULL;
	lpLS_item = (LS_item*) lParam2;
	lp2 = lpLS_item->lParam;

	// Call now the real callback function defined by the user
	//
	return LIS_CompFunc(lp1,lp2,lParamSort);
}

BOOL CListCtrlStyled::SortItems( PFNLVCOMPARE pfnCompare, DWORD dwData )
{	LIS_CompFunc = pfnCompare;
	PFNLVCOMPARE callback_func = LIS_CompareFunc;
	return (CListCtrl::SortItems( callback_func, dwData ));
}

// **************
// ** FindItem **
// **************
int CListCtrlStyled::FindItem( LV_FINDINFO* pFindInfo, int nStart)
{	/*
	typedef struct _LV_FINDINFO {
		UINT flags;    //see below
		LPCSTR psz;    //see below
		LPARAM lParam; //see below
	} LV_FINDINFO;
	*/

	if(pFindInfo->flags & LVFI_PARAM)

⌨️ 快捷键说明

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