📄 appheadc.cpp
字号:
#include "stdafx.h"
#include "appheadc.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAppHeaderCtrl
CAppHeaderCtrl::CAppHeaderCtrl ()
{
m_nSortCol = -1;
m_bFlatHeader = FALSE;
m_bBoldFont = FALSE;
m_nOffset = 6;
m_bLBtnDown = FALSE;
}
CAppHeaderCtrl:: ~ CAppHeaderCtrl ()
{
}
BEGIN_MESSAGE_MAP (CAppHeaderCtrl, CHeaderCtrl)
//{{AFX_MSG_MAP(CAppHeaderCtrl)
ON_WM_PAINT ()
ON_WM_WINDOWPOSCHANGING ()
//}}AFX_MSG_MAP
END_MESSAGE_MAP ()
/////////////////////////////////////////////////////////////////////////////
// CAppHeaderCtrl message handlers
void CAppHeaderCtrl::FlatHeader (BOOL bBoldFont /*=TRUE*/ )
{
// Get the log font.
NONCLIENTMETRICS ncm;
memset (&ncm, 0, sizeof (NONCLIENTMETRICS));
ncm.cbSize = sizeof (NONCLIENTMETRICS);
VERIFY (::SystemParametersInfo (SPI_GETNONCLIENTMETRICS,
sizeof (NONCLIENTMETRICS), &ncm, 0));
ncm.lfMessageFont.lfWeight = bBoldFont ? 700 : 400;
m_HeaderFont.CreateFontIndirect (&ncm.lfMessageFont);
SetFont (&m_HeaderFont);
m_bBoldFont = bBoldFont;
m_bFlatHeader = TRUE;
}
int CAppHeaderCtrl::
SetSortImage (int nCol, BOOL bAsc)
{
int nPrevCol = m_nSortCol;
m_nSortCol = nCol;
m_bSortAsc = bAsc;
// Change the item to ownder drawn
HD_ITEM hditem;
hditem.mask = HDI_BITMAP | HDI_FORMAT;
GetItem (nCol, &hditem);
if (hditem.hbm == NULL)
{
hditem.fmt |= HDF_OWNERDRAW;
SetItem (nCol, &hditem);
// Invalidate header control so that it gets redrawn
Invalidate ();
}
return nPrevCol;
}
void CAppHeaderCtrl::
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 ();
// Draw the background
CBrush brush (::GetSysColor (COLOR_3DFACE));
dc.FillRect (rcLabel, &brush);
dc.SetBkMode (TRANSPARENT);
// 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++;
}
// Adjust the rect further if Sort arrow is to be displayed
if (lpDrawItemStruct->itemID == (UINT) m_nSortCol)
{
rcLabel.right -= 3 * m_nOffset;
}
rcLabel.left += m_nOffset;
rcLabel.right -= m_nOffset;
// Draw column label
if (rcLabel.left < rcLabel.right)
{
if (m_bBoldFont)
{
dc.SelectObject (&m_HeaderFont);
}
dc.DrawText (buf, -1, rcLabel, uFormat);
}
// Draw the Sort arrow
if (lpDrawItemStruct->itemID == (UINT) m_nSortCol)
{
CRect rcIcon (lpDrawItemStruct->rcItem);
// Set up pens to use for drawing the triangle
CPen penLite (PS_SOLID, 1,::GetSysColor (COLOR_3DHILIGHT));
CPen penShad (PS_SOLID, 1,::GetSysColor (COLOR_3DSHADOW));
CPen *pOldPen = dc.SelectObject (&penLite);
if (m_bSortAsc)
{
// Draw triangle pointing upwards
dc.MoveTo (rcIcon.right - 2 * m_nOffset, m_nOffset - 1);
dc.LineTo (rcIcon.right - 3 * m_nOffset / 2, rcIcon.bottom - m_nOffset);
dc.LineTo (rcIcon.right - 5 * m_nOffset / 2 - 2, rcIcon.bottom - m_nOffset);
dc.MoveTo (rcIcon.right - 5 * m_nOffset / 2 - 1, rcIcon.bottom - m_nOffset - 1);
dc.SelectObject (&penShad);
dc.LineTo (rcIcon.right - 2 * m_nOffset, m_nOffset - 2);
}
else
{
// Draw triangle pointing downwords
dc.MoveTo (rcIcon.right - 3 * m_nOffset / 2, m_nOffset - 1);
dc.LineTo (rcIcon.right - 2 * m_nOffset - 1, rcIcon.bottom - m_nOffset + 1);
dc.MoveTo (rcIcon.right - 2 * m_nOffset - 1, rcIcon.bottom - m_nOffset);
dc.SelectObject (&penShad);
dc.LineTo (rcIcon.right - 5 * m_nOffset / 2 - 1, m_nOffset - 1);
dc.LineTo (rcIcon.right - 3 * m_nOffset / 2, m_nOffset - 1);
}
// Restore the pen
dc.SelectObject (pOldPen);
}
// Restore dc
dc.RestoreDC (nSavedDC);
// Detach the dc before returning
dc.Detach ();
}
void CAppHeaderCtrl::
OnPaint ()
{
Default ();
if (m_bFlatHeader)
DrawFlatBorder ();
}
void CAppHeaderCtrl::
DrawFlatBorder ()
{
CDC *pDC = GetDC ();
CRect rci;
GetWindowRect (&rci);
ScreenToClient (&rci);
// Cover up thick 3D border.
pDC->Draw3dRect (rci,::GetSysColor (COLOR_3DFACE),
::GetSysColor (COLOR_3DFACE));
rci.DeflateRect (1, 1);
pDC->Draw3dRect (rci,::GetSysColor (COLOR_3DFACE),
::GetSysColor (COLOR_3DFACE));
// Draw flat style border around entire header.
rci.InflateRect (1, 1);
pDC->Draw3dRect (rci,::GetSysColor (COLOR_3DHILIGHT),
::GetSysColor (COLOR_3DSHADOW));
// Create the pens for further cover-up.
CPen penLite (PS_SOLID, 1,::GetSysColor (COLOR_3DHILIGHT));
CPen penShad (PS_SOLID, 1,::GetSysColor (COLOR_3DSHADOW));
CPen penFace (PS_SOLID, 1,::GetSysColor (COLOR_3DFACE));
CPen *pOldPen = pDC->SelectObject (&penLite);
pDC->SelectObject (&penFace);
pDC->MoveTo (rci.right - 1, 2);
pDC->LineTo (rci.right - 1, rci.bottom - 2);
// Set up the header item struct.
HD_ITEM hdi;
memset (&hdi, 0, sizeof (HD_ITEM));
hdi.fmt = HDF_STRING | HDF_LEFT | HDF_OWNERDRAW;
hdi.mask = HDI_WIDTH | HDI_TEXT | HDI_FORMAT;
int cx = 0;
// For each header item found, do further cover up.
for (int i = 0; i < GetItemCount (); ++i)
{
GetItem (i, &hdi);
cx += hdi.cxy;
pDC->SelectObject (&penShad);
pDC->MoveTo (cx, 2);
pDC->LineTo (cx, rci.bottom - 2);
pDC->SelectObject (&penLite);
pDC->MoveTo (cx + 1, 2);
pDC->LineTo (cx + 1, rci.bottom - 2);
pDC->SelectObject (&penFace);
pDC->MoveTo (cx - 1, 2);
pDC->LineTo (cx - 1, rci.bottom - 2);
pDC->SelectObject (&penFace);
pDC->MoveTo (cx - 2, 2);
pDC->LineTo (cx - 2, rci.bottom - 2);
}
// Restore the pen and release device context.
pDC->SelectObject (pOldPen);
ReleaseDC (pDC);
}
void CAppHeaderCtrl::
OnWindowPosChanging (WINDOWPOS FAR * lpwndpos)
{
CHeaderCtrl::OnWindowPosChanging (lpwndpos);
Invalidate ();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -