?? mygridlist.cpp
字號(hào):
// 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)
//后加。響應(yīng)自繪制消息
{
// TODO: Add your message handler code here and/or call default
CHeaderCtrl * pHdr = (CHeaderCtrl *)GetDlgItem(0);
if( pHdr == NULL )
{
ASSERT(0);
return;
}
// 創(chuàng)建 CDC 臨時(shí)對(duì)象
CDC* pDC = CDC::FromHandle( lpDrawItemStruct->hDC );
if( lpDrawItemStruct->itemAction & (ODA_DRAWENTIRE | ODA_SELECT) )
{
COLORREF crOldText; // 不要永久改變?cè)?CDC 的屬性
COLORREF crOldBack; // 不要永久改變?cè)?CDC 的屬性
// 高亮度顯示被選中的對(duì)象
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);
}
// 清除整個(gè)背景區(qū),
pDC->ExtTextOut( lpDrawItemStruct->rcItem.left,
lpDrawItemStruct->rcItem.top,
ETO_OPAQUE, &(lpDrawItemStruct->rcItem),"", 0, NULL );
// 進(jìn)行一些設(shè)置工作
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() );
// 根據(jù)表頭排列各列
pHdr->GetItem( i, &hditem);
x += hditem.cxy;
// 2 is probably a text metric (frame?)
pDC->MoveTo( x-2, lpDrawItemStruct->rcItem.bottom -1);//-1后加,以消除豎線下部的突出點(diǎn)
pDC->LineTo( x-2, lpDrawItemStruct->rcItem.top -1);//-1后加,以消除豎線上部的斷點(diǎn)
}
// 繪制選中(獲得焦點(diǎn))對(duì)象的狀態(tài)
if( lpDrawItemStruct->itemState & ODA_FOCUS )
{
pDC->DrawFocusRect( &(lpDrawItemStruct->rcItem) );
}
// 恢復(fù)設(shè)備上下文(CDC)的狀態(tài)
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()//后加,插入一行
{
//加入列表內(nèi)容
CString sEntries[10];
sEntries[0].Format( "列表記錄 %d", m_nItem);
for(int i = 1; i < m_iCloumn; i++)
{
sEntries[i].Format( "記錄內(nèi)容 %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)//設(shè)置網(wǎng)格列表控件的列數(shù)
{
m_iCloumn=iNUM;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -