📄 stationctrl.cpp
字号:
// Written by JHCC, 1997
// StationCtrl.cpp : implementation file
//
#include "stdafx.h"
#include "JHHB.h"
#include "StationCtrl.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CStationCtrl
CStationCtrl::CStationCtrl()
{
m_pCtrlInfoArray = NULL;
m_nItemSpace = 4;
m_nCheckBoxSpace = 2;
m_nCheckedItem = -1;
m_nCurrentTop = -1;
}
CStationCtrl::~CStationCtrl()
{
m_isCheckedArray.RemoveAll();
m_rtCheckBoundsArray.RemoveAll();
}
void CStationCtrl::SetItemSpace(int nItemSpace)
{
m_nItemSpace = nItemSpace;
}
void CStationCtrl::SetCheckBoxSpace(int nCheckBoxSpace)
{
m_nCheckBoxSpace = nCheckBoxSpace;
}
void CStationCtrl::SetCtrlInfoArray(CCtrlInfoArray* pCtrlInfoArray)
{
m_pCtrlInfoArray = pCtrlInfoArray;
for (int n = 0; n < m_pCtrlInfoArray->GetSize(); ++ n)
{
CCtrlInfoObject* pCtrlInfo = m_pCtrlInfoArray->GetAt(n);
m_isCheckedArray.Add(pCtrlInfo->m_bActive);
}
m_rtCheckBoundsArray.SetSize(m_pCtrlInfoArray->GetSize());
}
const int nDrawSpace = 2;
void CStationCtrl::CalcCheckBoxBounds(void)
{
CDC* pDC = GetDC();
int nCheckBoxSize = GetCheckBoxSize(pDC);
ReleaseDC(pDC);
for (int i = m_nCurrentTop; i <= GetLastVisibleItem(); ++ i)
{
CRect rt;
GetItemRect(i, &rt);
rt.DeflateRect(nDrawSpace, nDrawSpace);
CRect rtBox(
rt.left + m_nCheckBoxSpace,
rt.top,
rt.left + m_nCheckBoxSpace + nCheckBoxSize,
rt.top + nCheckBoxSize);
m_rtCheckBoundsArray[i] = rtBox;
/*
TRACE1("\nnIndex is %d !!\n", nIndex);
TRACE("rtBox is, left = %d, top = %d, right = %d, bottom = %d !\n",
rtBox.left, rtBox.top, rtBox.right, rtBox.bottom);
*/
}
}
int CStationCtrl::GetLastVisibleItem(void)
{
int nHeight = 0;
CRect rtBounds;
GetClientRect(&rtBounds);
for (int i = GetTopIndex(); i < GetCount(); ++ i)
{
MEASUREITEMSTRUCT measureItemStruct;
MeasureItem(&measureItemStruct);
nHeight += measureItemStruct.itemHeight;
if (nHeight >= rtBounds.Height())
{
break;
}
}
if (i == GetCount()) // last
-- i;
return i;
}
int CStationCtrl::GetCheckBoxSize(CDC* pDC)
{
TEXTMETRIC tm;
pDC->GetTextMetrics(&tm);
return tm.tmHeight + tm.tmExternalLeading;//tm.tmAscent;
}
void CStationCtrl::DrawItemText(int nIndex, CDC* pDC, CRect* pBounds, BOOL bHilight)
{
ASSERT(m_pCtrlInfoArray);
int nCheckBoxSize = GetCheckBoxSize(pDC);
CRect rt = *pBounds;
rt.DeflateRect(nDrawSpace, nDrawSpace);
COLORREF clrBk;
COLORREF clrText;
if (bHilight)
{
clrText = ::GetSysColor(COLOR_HIGHLIGHTTEXT);
clrBk = ::GetSysColor(COLOR_HIGHLIGHT);
}
else
{
clrText = ::GetSysColor(COLOR_WINDOWTEXT);
clrBk = ::GetSysColor(COLOR_WINDOW);
}
pDC->FillSolidRect(pBounds, clrBk);
COLORREF clrOldText = pDC->SetTextColor(clrText);
COLORREF clrOldBk = pDC->SetBkColor(clrBk);
pDC->ExtTextOut(
rt.left + nCheckBoxSize + 2 * m_nCheckBoxSpace + 1,
rt.top + 1,
ETO_CLIPPED,
&rt,
(m_pCtrlInfoArray->GetAt(nIndex))->GetFullName(),
NULL);
pDC->SetTextColor(clrOldText);
pDC->SetBkColor(clrOldBk);
/*
CRect rtBox(
rt.left + m_nCheckBoxSpace,
rt.top,
rt.left + m_nCheckBoxSpace + nCheckBoxSize,
rt.top + nCheckBoxSize);
m_rtCheckBoundsArray[nIndex] = rtBox;*/
CRect rtBox = m_rtCheckBoundsArray[nIndex];
DrawCheckBox(pDC, &rtBox, bHilight, m_isCheckedArray[nIndex]);
}
void CStationCtrl::DrawCheckBox(CDC* pDC, CRect* pBounds, BOOL bHilight,
BOOL bChecked)
{
pDC->FillSolidRect(pBounds, ::GetSysColor(COLOR_WINDOW));
if (bHilight)
{
pDC->DrawFocusRect(pBounds);
}
else
{
pDC->Rectangle(pBounds);
}
if (bChecked)
{
const int nCheckSpaceRatio = 5;
const int nCheckPenWidthRatio = 5;
COLORREF clrCheck = ::GetSysColor(COLOR_WINDOWTEXT);
CRect rtCheck = *pBounds;
rtCheck.DeflateRect(pBounds->Width() / nCheckSpaceRatio,
pBounds->Height() / nCheckSpaceRatio);
CPen pen(PS_SOLID, rtCheck.Height() / nCheckPenWidthRatio, clrCheck);
CPen* pOldPen = pDC->SelectObject(&pen);
pDC->MoveTo(rtCheck.left, rtCheck.top + rtCheck.Height() / 2);
pDC->LineTo(rtCheck.left + rtCheck.Width() / 3, rtCheck.bottom);
pDC->LineTo(rtCheck.right, rtCheck.top);
pDC->SelectObject(pOldPen);
}
}
BEGIN_MESSAGE_MAP(CStationCtrl, CListBox)
//{{AFX_MSG_MAP(CStationCtrl)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONDBLCLK()
ON_WM_LBUTTONUP()
ON_WM_KEYDOWN()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CStationCtrl message handlers
void CStationCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
// Save the state of the passed-in DC
int iDcOld = pDC->SaveDC();
CRect rtItem = lpDrawItemStruct->rcItem;
// Reset the bounds if items have scrolled
if (GetTopIndex() != m_nCurrentTop)
{
m_nCurrentTop = GetTopIndex();
CalcCheckBoxBounds();
}
/*
// Draw the contents of the list box item
if (lpDrawItemStruct->itemAction & ODA_DRAWENTIRE)
{
// Set the foreground text color gray if needed
// if (lpDrawItemStruct->itemState & ODS_DISABLED ||
// lpDrawItemStruct->itemState & ODS_GRAYED)
// pDC->SetTextColor(GetSysColor(COLOR_GRAYTEXT));
DrawItemText(lpDrawItemStruct->itemID, pDC, &rtItem, FALSE);
// Set the initial state of the selection
if (lpDrawItemStruct->itemState & ODS_SELECTED)
{
pDC->InvertRect(&rtItem);
}
// Set the initial state of the focus
if (lpDrawItemStruct->itemState & ODS_FOCUS)
pDC->DrawFocusRect(&rtItem);
}
else
{
// Toggle the selection rect
if (lpDrawItemStruct->itemAction & ODA_SELECT)
{
pDC->InvertRect(&rtItem);
}
// Toggle the focus rect
if (lpDrawItemStruct->itemAction & ODA_FOCUS)
pDC->DrawFocusRect(&rtItem);
}
*/
// Draw the entire item if necessary
if (lpDrawItemStruct->itemAction & ODA_DRAWENTIRE)
{
// Paint the color item in the color requested
DrawItemText(lpDrawItemStruct->itemID, pDC, &rtItem, FALSE);
}
if ((lpDrawItemStruct->itemState & ODS_SELECTED) &&
(lpDrawItemStruct->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)))
{
// item has been selected
DrawItemText(lpDrawItemStruct->itemID, pDC, &rtItem, TRUE);
}
if (!(lpDrawItemStruct->itemState & ODS_SELECTED) &&
(lpDrawItemStruct->itemAction & ODA_SELECT))
{
// Item has been de-selected
DrawItemText(lpDrawItemStruct->itemID, pDC, &rtItem, FALSE);
}
// Restore the orginal DC state
pDC->RestoreDC(iDcOld);
}
void CStationCtrl::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
// must use LBS_OWNERDRAWVARIABLE for this to work
CDC* pDC = GetDC();
lpMeasureItemStruct->itemHeight = GetCheckBoxSize(pDC) + m_nItemSpace;
ReleaseDC(pDC);
}
void CStationCtrl::OnLButtonDown(UINT nFlags, CPoint point)
{
TRACE1("TopIndex is %d \n", GetTopIndex());
TRACE1("GetLastVisibleItem() is %d \n", GetLastVisibleItem());
for (int n = GetTopIndex(); n <= GetLastVisibleItem(); ++ n)
{
CRect rtCheckBox = m_rtCheckBoundsArray[n];
if (rtCheckBox.PtInRect(point))
{
m_nCheckedItem = n;
TRACE1("\nm_nCheckedItem is -- %d -- \n", m_nCheckedItem);
break;
}
}
CListBox::OnLButtonDown(nFlags, point);
}
void CStationCtrl::OnLButtonDblClk(UINT nFlags, CPoint point)
{
OnLButtonDown(nFlags, point);
// CListBox::OnLButtonDblClk(nFlags, point);
}
void CStationCtrl::OnLButtonUp(UINT nFlags, CPoint point)
{
for (int n = GetTopIndex(); n <= GetLastVisibleItem(); ++ n)
{
CRect rtCheckBox = m_rtCheckBoundsArray[n];
if (rtCheckBox.PtInRect(point))
{
m_isCheckedArray[n] = !m_isCheckedArray[n];
if (m_nCheckedItem == n) // Up at Down pos
{
InvalidateRect(&rtCheckBox, TRUE);
}
break;
}
}
m_nCheckedItem = -1;
CListBox::OnLButtonUp(nFlags, point);
}
void CStationCtrl::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
switch (nChar)
{
case VK_SPACE:
int n = GetCurSel();
if (n == -1)
break;
m_isCheckedArray[n] = !m_isCheckedArray[n];
CRect rtCheckBox = m_rtCheckBoundsArray[n];
InvalidateRect(&rtCheckBox, TRUE);
break;
}
CListBox::OnKeyDown(nChar, nRepCnt, nFlags);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -