📄 headerlistbox.cpp
字号:
// HeaderListBox.cpp : implementation file
//
#include "stdafx.h"
#include "Controls.h"
#include "HeaderListBox.h"
#include "HeaderWnd.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CHeaderListBox
CHeaderListBox::CHeaderListBox()
{
}
CHeaderListBox::~CHeaderListBox()
{
}
BEGIN_MESSAGE_MAP(CHeaderListBox, CListBox)
//{{AFX_MSG_MAP(CHeaderListBox)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CHeaderListBox message handlers
void CHeaderListBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
if (lpDrawItemStruct->itemID == (UINT) -1)
return;
int nItem = lpDrawItemStruct->itemID;
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
CBrush brush;
if (lpDrawItemStruct->itemState & ODS_SELECTED)
{
brush.CreateSolidBrush (GetSysColor (COLOR_HIGHLIGHT));
pDC->SetTextColor (GetSysColor (COLOR_HIGHLIGHTTEXT));
}
else
{
brush.CreateSolidBrush (GetSysColor (COLOR_WINDOW));
pDC->SetTextColor (GetSysColor (COLOR_WINDOWTEXT));
}
pDC->FillRect (&lpDrawItemStruct->rcItem, &brush);
pDC->SetBkMode (TRANSPARENT);
CRect rcItem(lpDrawItemStruct->rcItem);
rcItem.InflateRect(-2, -2);
CRect rc (rcItem);
HDITEM hi;
CHeaderCtrl *Header = ((CHeaderWnd *) GetParent())->GetHeaderCtrl ();
memset (&hi, '\0', sizeof (HDITEM));
hi.mask = HDI_WIDTH | HDI_ORDER;
// Header->GetItem (0, &hi);
Header->GetItemRect (0, rcItem);
rc.left = rcItem.left + 2;
// rc.right = rc.left + hi.cxy - 2;
rc.right = rc.left + rcItem.right - rcItem.left - 2;
POSITION pos = m_ListData.FindIndex (nItem);
if (pos == NULL)
return;
WIN32_FIND_DATA fd = m_ListData.GetAt (pos);
CString strText = fd.cFileName;
pDC->DrawText (strText, &rc, DT_END_ELLIPSIS);
Header->GetItemRect (1, rcItem);
rc.left = rcItem.left + 2;
rc.right = rc.left + rcItem.right - rcItem.left - 2;
FormatNumber (strText, fd.nFileSizeLow);
pDC->DrawText (strText, &rc, DT_END_ELLIPSIS);
Header->GetItemRect (2, rcItem);
rc.left = rcItem.left + 2;
rc.right = rc.left + rcItem.right - rcItem.left - 2;
CTime Now = fd.ftCreationTime;
strText.Format ("%02d/%02d/%02d %02d:%02d",
Now.GetMonth(), Now.GetDay(), Now.GetYear() % 100,
Now.GetHour(), Now.GetMinute());
pDC->DrawText (strText, &rc, DT_END_ELLIPSIS);
// Focus rect
if (lpDrawItemStruct->itemAction & ODA_FOCUS)
pDC->DrawFocusRect(&lpDrawItemStruct->rcItem);
}
void CHeaderListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
if (lpMeasureItemStruct->itemID != IDC_HEADER_LISTBOX)
return;
CWindowDC (this);
LOGFONT lf;
CFont *font = GetFont();
font->GetLogFont (&lf);
lpMeasureItemStruct->itemHeight = lf.lfHeight;
if (lpMeasureItemStruct->itemHeight < 0)
lpMeasureItemStruct->itemHeight *= -1;
CHeaderCtrl *Header = ((CHeaderWnd *) GetParent())->GetHeaderCtrl ();
CRect rcItem;
Header->GetItemRect (2, rcItem);
lpMeasureItemStruct->itemWidth = rcItem.right;
}
void CHeaderListBox::AddListItem(WIN32_FIND_DATA &fd)
{
POSITION pos = m_ListData.AddTail (fd);
AddString ((LPCSTR) pos);
}
void CHeaderListBox::FormatNumber(CString &strText, ULONG ulSize)
{
TCHAR szBuf[32];
strText.Empty();
memset (szBuf, '\0', sizeof (szBuf));
sprintf (szBuf + 1, "%ld", ulSize);
TCHAR *psz = szBuf + strlen (szBuf + 1);
for (int i = 1; *(psz - 1); ++i, --psz)
{
if (!(i%3))
InsertChar (psz, ',');
}
strText = (szBuf + 1);
}
void CHeaderListBox::InsertChar(TCHAR *sz, TCHAR ch)
{
char *s = sz + strlen (sz);
while (s > sz)
{
*s = *(s-1);
--s;
}
*s = ch;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -