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

📄 mygridlist.cpp

📁 用MFC自绘制特性扩展网格列表控件
💻 CPP
字号:
// MyGridList.cpp : implementation file
//

#include "stdafx.h"
#include "TestMyGridList.h"
#include "MyGridList.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMyGridList

CMyGridList::CMyGridList()
{
	m_strSelect = _T("");
	m_nItem=0;
	m_iCloumn=1;
}

CMyGridList::~CMyGridList()
{
}


BEGIN_MESSAGE_MAP(CMyGridList, CListCtrl)
	//{{AFX_MSG_MAP(CMyGridList)
	ON_WM_DRAWITEM()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyGridList message handlers
void CMyGridList::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct) 
//后加。响应自绘制消息
{
	// TODO: Add your message handler code here and/or call default
	CHeaderCtrl * pHdr = (CHeaderCtrl *)GetDlgItem(0);
	if( pHdr == NULL )
	{
		ASSERT(0);
		return;
	}

	// 创建 CDC 临时对象
	CDC* pDC = CDC::FromHandle( lpDrawItemStruct->hDC );

	if( lpDrawItemStruct->itemAction & (ODA_DRAWENTIRE | ODA_SELECT) )
	{ 	
		COLORREF crOldText;   // 不要永久改变原 CDC 的属性
		COLORREF crOldBack;   // 不要永久改变原 CDC 的属性

		// 高亮度显示被选中的对象	
		if( lpDrawItemStruct->itemState & ODS_SELECTED )
		{
			crOldText = pDC->SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT) );
			crOldBack = pDC->SetBkColor(::GetSysColor(COLOR_HIGHLIGHT) );

			// this is the trick! we always know the row number of the selection (the one with highlight)
			// remember this row for later and update box text
			// this code is kinda redundant (dw *is* lpDrawItemStruct->itemData)
			DWORD dw = GetItemData(lpDrawItemStruct->itemData); 
			m_strSelect.Format("你选中的是第 %d 行", (int)dw);
			UpdateData(FALSE);
		}

	   	// 清除整个背景区,
		pDC->ExtTextOut( lpDrawItemStruct->rcItem.left,
							  lpDrawItemStruct->rcItem.top,
							  ETO_OPAQUE, &(lpDrawItemStruct->rcItem),"", 0, NULL );              

		// 进行一些设置工作
		CString s;
		int x, y;
		x = lpDrawItemStruct->rcItem.left;
		y =lpDrawItemStruct->rcItem.top;

		HD_ITEM hditem;
		hditem.mask = HDI_WIDTH;
		/// why -1?
		pDC->MoveTo( lpDrawItemStruct->rcItem.left, lpDrawItemStruct->rcItem.bottom-1 ); 
		// -2 is probably a text metric (frame?)
		pDC->LineTo( lpDrawItemStruct->rcItem.right-2, lpDrawItemStruct->rcItem.bottom -1);

		for( int i = 0; i < m_iCloumn; i++)
		{
			s = GetItemText(lpDrawItemStruct->itemData, i);

			// 输出文字 
			pDC->TextOut(  x+2, y, s, s.GetLength()  );

			// 根据表头排列各列
			pHdr->GetItem( i, &hditem);
			x += hditem.cxy;

			// 2 is probably a text metric (frame?)
			pDC->MoveTo( x-2, lpDrawItemStruct->rcItem.bottom -1);//-1后加,以消除竖线下部的突出点 
			pDC->LineTo( x-2, lpDrawItemStruct->rcItem.top -1);//-1后加,以消除竖线上部的断点
		}

		// 绘制选中(获得焦点)对象的状态
		if( lpDrawItemStruct->itemState & ODA_FOCUS )
		{
			pDC->DrawFocusRect( &(lpDrawItemStruct->rcItem) );  
		}

		// 恢复设备上下文(CDC)的状态
		if( lpDrawItemStruct->itemState & ODS_SELECTED )
		{
			pDC->SetTextColor( crOldText );
			pDC->SetBkColor( crOldBack );
		}

	} // end of if on	ODA_DRAWENTIRE | ODA_SELECT

   	if( lpDrawItemStruct->itemAction & ODA_FOCUS )
	{
		pDC->DrawFocusRect( &(lpDrawItemStruct->rcItem) );
	}

	//OnDrawItem(nIDCtl, lpDrawItemStruct);	
}

void CMyGridList::AddOne()//后加,插入一行
{
	//加入列表内容
	CString sEntries[10];
    sEntries[0].Format( "列表记录 %d", m_nItem);
	for(int i = 1; i < m_iCloumn; i++)
	{
		sEntries[i].Format( "记录内容 %d", i);
	} 

	// form the list control structure
	LV_ITEM lvitem;
	lvitem.mask = LVIF_TEXT | LVIF_PARAM;

	// this is the trick! load the 0-index row number into the helper parameter for the 0th column
	lvitem.lParam = m_nItem;
	// point to the row number
	lvitem.iItem = m_nItem;	  
	lvitem.pszText = (LPSTR)(const char *)sEntries[0]; 
	// must be zero for the 0th column
	lvitem.iSubItem = 0;
	// do the insert
	int ret = InsertItem( &lvitem ); 
	// a check
	ASSERT( ret != -1 );

	// now populate the subitems
	for( i = 1; i < m_iCloumn; i++)
	{
		BOOL b = SetItemText( m_nItem, i, (LPTSTR)(const char *)sEntries[i] );
		ASSERT(b);
	}
}

void CMyGridList::SetColumn(int iNUM)//设置网格列表控件的列数
{
	m_iCloumn=iNUM;
}

⌨️ 快捷键说明

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