📄 sortableheaderctrl.cpp
字号:
//thc 2001.9.11
// SortableHeaderCtrl1.cpp : implementation file
//
#include "stdafx.h"
#include "SortableHeaderCtrl.h"
#include "Resource.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CSortableHeaderCtrl
CSortableHeaderCtrl::CSortableHeaderCtrl()
{
m_lColumnSortStates = 0;
m_iLastColumn=-1;
/* m_bmpArrowUp.LoadBitmap(IDB_ARROWUP);
m_bmpArrowDown.LoadBitmap(IDB_ARROWDOWN);*/
}
CSortableHeaderCtrl::~CSortableHeaderCtrl()
{
/* m_bmpArrowUp.DeleteObject();
m_bmpArrowDown.DeleteObject();*/
}
BEGIN_MESSAGE_MAP(CSortableHeaderCtrl, CHeaderCtrl)
//{{AFX_MSG_MAP(CSortableHeaderCtrl)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSortableHeaderCtrl message handlers
/*void CSortableHeaderCtrl::SetSortImage( int nCol, BOOL bAsc )
{
CBitmap *pTempBmp = NULL;
if( bAsc )
pTempBmp = &m_bmpArrowUp;
else
pTempBmp = &m_bmpArrowDown;
HD_ITEM Item;
Item.mask = HDI_FORMAT;
int bdj=GetItem( nCol, &Item );
Item.mask = HDI_BITMAP | HDI_FORMAT;
Item.fmt |= HDF_BITMAP;
Item.hbm = (HBITMAP)pTempBmp->GetSafeHandle();
int bdj1=SetItem( nCol, &Item );
m_iLastColumn = nCol;
//return 1;
}*/
const int CSortableHeaderCtrl::GetLastColumn() const
{
return m_iLastColumn;
}
void CSortableHeaderCtrl::RemoveSortImage( int iItem )
{
if( iItem != -1 )
{
HD_ITEM hditem;
hditem.mask = HDI_FORMAT;
GetItem( iItem, &hditem );
hditem.mask = HDI_FORMAT;
hditem.fmt &= ~HDF_BITMAP;
SetItem( iItem, &hditem );
}
}
bool CSortableHeaderCtrl::GetItemSortState( int iItem )
{
/*The bitwise shift operators shift their first operand left (<<) or
right (>>) by the number of positions the second operand specifies.
^ The bitwise-exclusive-OR operator compares each bit of its first
operand to the corresponding bit of its second operand. If one bit
is 0 and the other bit is 1, the corresponding result bit is set
to 1. Otherwise, the corresponding result bit is set to 0. */
///////////////////////////////////////////////////////////////
// Tang.H.C 2001.12.10
//m_lColumnSortStates=0:列表未排序或某位处于降序
//m_lColumnSortStates=1:列表某位处于升序
bool bSortState=((m_lColumnSortStates) & ( 1 << iItem ))==0 ?true:false;
//保持旧排序状态
m_lColumnSortStates &=( 1 << iItem );
m_lColumnSortStates ^=( 1 << iItem );
////////////////////////////////////////////////////////////////
return bSortState;
}
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
void CSortableHeaderCtrl::SetSortImageStandard( int nCol, BOOL bAsc )
{
// Change the item to ownder drawn
m_iLastColumn = nCol;
m_nSortCol = nCol;
m_bSortAsc = bAsc;
HD_ITEM hditem;
hditem.mask = HDI_FORMAT;//HDI_FORMAT The fmt member is valid.
GetItem(nCol, &hditem);
hditem.fmt |= HDF_OWNERDRAW;
SetItem(nCol, &hditem);
// Invalidate header control so that it gets redrawn
Invalidate();
}
void CSortableHeaderCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC dc;
dc.Attach(lpDrawItemStruct->hDC);
CRect rcLabel(lpDrawItemStruct->rcItem);
int nSavedDC = dc.SaveDC();
/* CRgn rgn;
rgn.CreateRectRgnIndirect(&rcLabel);
dc.SelectObject(&rgn);
rgn.DeleteObject();*/
// Draw the background
// dc.FillRect(rcLabel, &CBrush(::GetSysColor(COLOR_3DFACE)));
dc.FillSolidRect(rcLabel, GetSysColor(COLOR_3DFACE));
// 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;
// Get the column text and format
static TCHAR buf[256];
HD_ITEM hditem;
hditem.mask = HDI_TEXT | HDI_FORMAT | HDI_LPARAM;
hditem.pszText = buf;
hditem.cchTextMax = 255;
GetItem(lpDrawItemStruct->itemID, &hditem);
int iSubItem = hditem.lParam;
// 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++;
}*/
// Draw column label
// Adjust the rect further if Sort arrow is to be displayed
if (lpDrawItemStruct->itemID == (UINT)m_nSortCol) rcLabel.right -= 3 * offset;
rcLabel.left += offset;
rcLabel.right -= offset;
if (rcLabel.left < rcLabel.right)
dc.DrawText(buf,-1,rcLabel, uFormat);
// Draw the Sort arrow
if (lpDrawItemStruct->itemID == (UINT)m_nSortCol)//m_nSortCol))
{
CRect rcIcon(lpDrawItemStruct->rcItem);
// Set up pens to use for drawing the triangle
CPen penLight(PS_SOLID, 1, GetSysColor(COLOR_3DHILIGHT));
CPen penShadow(PS_SOLID, 1, GetSysColor(COLOR_3DSHADOW));
CPen *pOldPen = dc.SelectObject(&penLight);
if (m_bSortAsc)
{
// Draw triangle pointing upwards
dc.MoveTo(rcIcon.right - 2*offset, offset-1);
dc.LineTo(rcIcon.right - 3*offset/2, rcIcon.bottom - offset);
dc.LineTo(rcIcon.right - 5*offset/2-2, rcIcon.bottom - offset);
dc.MoveTo(rcIcon.right - 5*offset/2-1, rcIcon.bottom - offset-1);
dc.SelectObject(&penShadow);
dc.LineTo(rcIcon.right - 2*offset, offset-2);
}
else
{
// Draw triangle pointing downwords
dc.MoveTo(rcIcon.right - 3*offset/2, offset-1);
dc.LineTo(rcIcon.right - 2*offset-1, rcIcon.bottom - offset + 1);
dc.MoveTo(rcIcon.right - 2*offset-1, rcIcon.bottom - offset);
dc.SelectObject(&penShadow);
dc.LineTo(rcIcon.right - 5*offset/2-1, offset -1);
dc.LineTo(rcIcon.right - 3*offset/2, offset -1);
}
// Restore the pen
dc.SelectObject(pOldPen);
}
dc.RestoreDC(nSavedDC);
dc.Detach();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -