翻譯|其它|編輯:郝浩|2006-06-21 10:24:00.000|閱讀 1662 次
概述:
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
雖然我們可用直接指定位圖設置表頭圖像,但我們也可以使用圖像列表設置表頭圖像。
步驟一:從CHeaderCtrl類導出CMyHeaderCtrl類。
步驟二:添加變量
protected:
CImageList* m_pImageList;
CMap m_mapImageIndex;
步驟三:在構造函數中初始化變量
m_pImageList=NULL;
步驟四:加入下列函數
CImageList* CMyHeaderCtrl::SetImageList( CImageList* pImageList )
{
CImageList *pPrevList = m_pImageList;
m_pImageList = pImageList;
return pPrevList;
}
int CMyHeaderCtrl::GetItemImage( int nItem )
{
int imageIndex;
if( m_mapImageIndex.Lookup( nItem, imageIndex ) )
return imageIndex;
return -1;
}
void CMyHeaderCtrl::SetItemImage( int nItem, int nImage )
{
// Save the image index
m_mapImageIndex[nItem] = nImage;
// Change the item to ownder drawn
HD_ITEM hditem;
hditem.mask = HDI_FORMAT;
GetItem( nItem, &hditem );
hditem.fmt |= HDF_OWNERDRAW;
SetItem( nItem, &hditem );
// Invalidate header control so that
// it gets redrawn
Invalidate();
}
步驟五:重載Drawitem()函數
void CMyHeaderCtrl::DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct )
{
CDC dc;
dc.Attach( lpDrawItemStruct->hDC );
// Get the column rect
CRect rcLabel( lpDrawItemStruct->rcItem );
// Save DC
int nSavedDC = dc.SaveDC();
// Set clipping region to limit drawing
within column
CRgn rgn;
rgn.CreateRectRgnIndirect( &rcLabel );
dc.SelectObject( &rgn );
rgn.DeleteObject();
// Labels are offset by a certain amount
// This offset is related to the width of
// a space character
int offset = dc.GetTextExtent(_T(" "), 1 ).cx*2;
// Draw image from image list
int imageIndex;
if (m_pImageList &&
m_mapImageIndex.Lookup( lpDrawItemStruct->itemID, imageIndex ) )
{
if( imageIndex != -1 )
{
m_pImageList->Draw(&dc,
imageIndex,
CPoint( rcLabel.left +
offset,offset/3 ),
ILD_TRANSPARENT );
// Now adjust the label
rectangle
IMAGEINFO imageinfo;
if( m_pImageList->GetImageInfo(
imageIndex, &imageinfo ) )
{
rcLabel.left += offset/2 +
imageinfo.rcImage.right - imageinfo.rcImage.left;
}
}
}
// Get the column text and format
TCHAR buf[256];
HD_ITEM hditem;
hditem.mask = HDI_TEXT | HDI_FORMAT;
hditem.pszText = buf;
hditem.cchTextMax = 255;
GetItem( lpDrawItemStruct->itemID, &hditem );
// Determine format for drawing column label
UINT uFormat = DT_SINGLELINE | DT_NOPREFIX | DT_NOCLIP
| DT_VCENTER | DT_END_ELLIPSIS ;
if( hditem.fmt & HDF_CENTER)
uFormat |= DT_CENTER;
else if( hditem.fmt & HDF_RIGHT)
uFormat |= DT_RIGHT;
else
uFormat |= DT_LEFT;
// Adjust the rect if the mouse button is pressed on it
if( lpDrawItemStruct->itemState == ODS_SELECTED )
{
rcLabel.left++;
rcLabel.top += 2;
rcLabel.right++;
}
rcLabel.left += offset;
rcLabel.right -= offset;
// Draw column label
if( rcLabel.left < rcLabel.right )
dc.DrawText(buf,-1,rcLabel, uFormat);
// Restore dc
dc.RestoreDC( nSavedDC );
// Detach the dc before returning
dc.Detach();
}
步驟六:在列表類中加入列表頭類變量
步驟七:調用PreSubclassWindow,使DrawItem函數被調用。
void CMyListCtrl::PreSubclassWindow()
{
CListCtrl::PreSubclassWindow();
// Add initialization code
m_headerctrl.SubclassWindow(
::GetDlgItem(m_hWnd,0) );
}
步驟八:調用SetImageList() and SetItemImage()設置圖像。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn