orderedtreectrl.cpp
来自「管理项目进度工具的原代码」· C++ 代码 · 共 715 行 · 第 1/2 页
CPP
715 行
if (dwParentItem)
VERIFY(mapParentPos.Lookup(dwParentItem, sParentPos));
BOOL bHasChildren = ItemHasChildren(hti);
if (nPos == -1) // it means we have to figure it out for ourselves
nPos = TCH().GetItemPos(hti, (HTREEITEM)dwParentItem);
if (nLevel == -1) // likewise
nLevel = TCH().GetItemLevel(hti);
// default
if (sParentPos.IsEmpty())
sPos.Format("%d", nPos);
else
sPos.Format("%s.%d", sParentPos, nPos);
// add to map for our children
if (bHasChildren)
mapParentPos[dwItem] = sPos;
// modify for actual output
if (bHasChildren && !TCH().IsItemExpanded(hti))
sPos += "...";
else if (nLevel == 0)
sPos += ".";
if (CRect().IntersectRect(rItem, rWindow)) // something to see
{
int nSaveDC = pDC->SaveDC();
// fill background of selected item
BOOL bFocused = TCH().HasFocus(/*!(GetStyle() & TVS_FULLROWSELECT)*/);
BOOL bDropHilited = (GetItemState(hti, TVIS_DROPHILITED) & TVIS_DROPHILITED);
if (bDropHilited)
pDC->FillSolidRect(rItem, GetSysColor(COLOR_3DFACE));
else if (bSelected)
{
if (bFocused)
pDC->FillSolidRect(rItem, GetSysColor(COLOR_HIGHLIGHT));
else
pDC->FillSolidRect(rItem, GetSysColor(COLOR_3DFACE));
}
else if (m_crAltLines != -1 && TCH().ItemLineIsOdd(hti))
pDC->FillSolidRect(rItem, m_crAltLines);
// draw pos
COLORREF crTextColor = GetSysColor((bSelected && bFocused) ? COLOR_HIGHLIGHTTEXT : COLOR_WINDOWTEXT);
pDC->SetTextColor(crTextColor);
pDC->SetBkMode(TRANSPARENT);
rItem.left += NCG_COLPADDING;
pDC->DrawText(sPos, rItem, DT_SINGLELINE | DT_VCENTER | DT_LEFT);
pDC->RestoreDC(nSaveDC);
}
// vertical divider
if (!bSelected && m_crGridlines != -1)
pDC->FillSolidRect(rItem.right - 1, rItem.top, 1, rItem.Height(), m_crGridlines);
}
}
void COrderedTreeCtrl::PostNcDrawItem(CDC* pDC, DWORD /*dwItem*/, const CRect& rItem, int nLevel)
{
if (nLevel == 0 && m_crGridlines != -1)
pDC->FillSolidRect(rItem.left, rItem.bottom - 1, rItem.Width(), 1, m_crGridlines);
}
BOOL COrderedTreeCtrl::OnItemexpanded(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
// invalidate client area
if (m_crAltLines != -1)
{
CRect rClient;
TCH().GetClientRect(rClient, pNMTreeView->itemNew.hItem);
InvalidateRect(rClient);
}
else if (pNMTreeView->itemNew.state & TVIS_EXPANDED)
{
TCH().InvalidateItem(pNMTreeView->itemNew.hItem);
}
m_gutter.RecalcGutter();
*pResult = 0;
return FALSE;
}
BOOL COrderedTreeCtrl::OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult)
{
NMCUSTOMDRAW* pNMCD = (NMCUSTOMDRAW*)pNMHDR;
NMTVCUSTOMDRAW* pTVCD = (NMTVCUSTOMDRAW*)pNMCD;
if (pNMCD->dwDrawStage == CDDS_PREPAINT)
*pResult |= CDRF_NOTIFYITEMDRAW | CDRF_NOTIFYPOSTPAINT;
else if (pNMCD->dwDrawStage == CDDS_ITEMPREPAINT)
{
HTREEITEM hti = (HTREEITEM)pTVCD->nmcd.dwItemSpec;
// do the odd lines
if (m_crAltLines != -1 && TCH().ItemLineIsOdd(hti))
{
BOOL bFullRow = (GetStyle() & TVS_FULLROWSELECT);
CRect rect(pNMCD->rc);
pTVCD->clrTextBk = GetAlternateLineColor();
if (bFullRow)
*pResult |= CDRF_NEWFONT;
else
{
COLORREF crOld = ::SetBkColor(pTVCD->nmcd.hdc, pTVCD->clrTextBk);
::ExtTextOut(pTVCD->nmcd.hdc, rect.left, rect.top, ETO_OPAQUE, rect, NULL, 0, NULL);
::SetBkColor(pTVCD->nmcd.hdc, crOld);
}
}
if (m_crGridlines != -1)
*pResult |= CDRF_NOTIFYPOSTPAINT;
}
else if (pNMCD->dwDrawStage == CDDS_ITEMPOSTPAINT && m_crGridlines != -1)
{
CDC* pDC = CDC::FromHandle(pNMCD->hdc);
// if the next visible item is a top level item then draw a horz line below us
HTREEITEM hti = (HTREEITEM)pTVCD->nmcd.dwItemSpec;
HTREEITEM htiNext = CTreeCtrl::GetNextVisibleItem(hti);
BOOL bFullRow = (GetStyle() & TVS_FULLROWSELECT);
if (!GetParentItem(htiNext))
{
// but if the bottom of the text coincides with the bottom of the
// item and we have the focus then take care not to draw over the focus rect
// but only if we do _not have TVS_FULLROWSELECT style
if ((pNMCD->uItemState & CDIS_FOCUS) && !bFullRow)
{
CRect rText;//(pNMCD->rc);
GetItemRect(hti, rText, TRUE);
if (rText.bottom == pNMCD->rc.bottom)
{
pDC->FillSolidRect(pNMCD->rc.left, pNMCD->rc.bottom - 1, rText.left - pNMCD->rc.left, 1, m_crGridlines);
pDC->FillSolidRect(rText.right, pNMCD->rc.bottom - 1, pNMCD->rc.right - rText.right, 1, m_crGridlines);
}
else
pDC->FillSolidRect(pNMCD->rc.left, pNMCD->rc.bottom - 1, pNMCD->rc.right - pNMCD->rc.left, 1, m_crGridlines);
}
else
pDC->FillSolidRect(pNMCD->rc.left, pNMCD->rc.bottom - 1, pNMCD->rc.right - pNMCD->rc.left, 1, m_crGridlines);
}
*pResult |= CDRF_DODEFAULT;
}
return FALSE; // to continue routing
}
int COrderedTreeCtrl::RecalcColumnWidth(CDC* pDC, UINT nColID, UINT& nWidth)
{
switch (nColID)
{
case OTC_POSCOLUMNID:
if (m_bShowingPosColumn)
{
g_mapWidths.RemoveAll(); // to handle a font change
HTREEITEM hti = GetChildItem(NULL);
int nPos = 1;
int nMaxWidth = 0;
while (hti)
{
int nItemWidth = GetGutterWidth(hti, 0, nPos, pDC);
nMaxWidth = max(nMaxWidth, nItemWidth);
hti = CTreeCtrl::GetNextItem(hti, TVGN_NEXT);
nPos++;
}
nWidth = max(nMaxWidth, MINGUTTER);
}
else
nWidth = 0;
// rebuild visible item map if there are other columns
if (m_bShowingPosColumn || m_gutter.GetColumnCount() > 1)
TCH().BuildVisibleIndexMap();
return TRUE;
}
return 0;
}
UINT COrderedTreeCtrl::GetGutterWidth(HTREEITEM hti, int nLevel, int nPos, CDC* pDC)
{
UINT nMaxWidth = 0;
if (GetItemState(hti, TVIS_EXPANDED) & TVIS_EXPANDED)
{
HTREEITEM htiChild = GetChildItem(hti);
int nPosChild = 1;
nLevel++;
while (htiChild)
{
UINT nWidth = GetGutterWidth(htiChild, nLevel, nPosChild, pDC);
nMaxWidth = max(nMaxWidth, nWidth);
htiChild = CTreeCtrl::GetNextItem(htiChild, TVGN_NEXT);
nPosChild++;
}
}
else if (ItemHasChildren(hti)/* && ShowingEllipsis(nLevel)*/)
{
nMaxWidth = GetWidth(-1, pDC); // ellipsis
}
return nMaxWidth + GetWidth(nPos, pDC);
}
BOOL COrderedTreeCtrl::OnClick(NMHDR* /*pNMHDR*/, LRESULT* pResult)
{
// make sure the selection is correctly set
CPoint point(::GetMessagePos());
ScreenToClient(&point);
HTREEITEM htiHit = HitTest(point);
if (htiHit)
SelectItem(htiHit);
*pResult = 0;
return FALSE; // to continue routing
}
void COrderedTreeCtrl::OnBeginDrag(NMHDR* pNMHDR, LRESULT* pResult)
{
NMTREEVIEW* pNMTV = (NMTREEVIEW*)pNMHDR;
SelectItem(pNMTV->itemNew.hItem);
m_gutter.Redraw();
*pResult = 0;
}
UINT COrderedTreeCtrl::GetWidth(int nNumber, CDC* pDC)
{
UINT nWidth = 0;
if (g_mapWidths.Lookup(nNumber, nWidth))
return nWidth;
if (nNumber >= 0)
{
CString sNumber;
sNumber.Format("%d.", nNumber);
nWidth = pDC->GetTextExtent(sNumber).cx;
}
else if (nNumber == -1)
nWidth = pDC->GetTextExtent("...").cx;
g_mapWidths[nNumber] = nWidth;
return nWidth;
}
LRESULT COrderedTreeCtrl::OnGutterNotifyItemClick(WPARAM /*wParam*/, LPARAM lParam)
{
NCGITEMCLICK* pNGIC = (NCGITEMCLICK*)lParam;
ASSERT (pNGIC);
TCH().EndLabelEdit(FALSE); // always
HTREEITEM hti = (HTREEITEM)pNGIC->dwItem;
SelectItem(hti); // always
switch (pNGIC->nMsgID)
{
case WM_NCLBUTTONDBLCLK:
case WM_LBUTTONDBLCLK:
if (hti && ItemHasChildren(hti))
{
// kill any label edit that might have been started by
// the first button click
TCH().EndLabelEdit(TRUE);
UINT nHitFlags = 0;
HitTest(pNGIC->ptClick, &nHitFlags);
if (pNGIC->nMsgID != WM_LBUTTONDBLCLK ||
!(nHitFlags & (TVHT_ONITEMBUTTON | TVHT_ONITEMLABEL | TVHT_ONITEMICON)))
{
BOOL bExpanded = TCH().IsItemExpanded(hti);
Expand(hti, bExpanded ? TVE_COLLAPSE : TVE_EXPAND);
CRect rItem;
TCH().GetClientRect(rItem, hti);
InvalidateRect(rItem);
return TRUE; // handled
}
}
break;
}
return FALSE;
}
void COrderedTreeCtrl::OnStyleChanged(int nStyleType, LPSTYLESTRUCT lpSS)
{
if (nStyleType == GWL_STYLE)
{
BOOL bButtonsOld = lpSS->styleOld & TVS_HASBUTTONS;
BOOL bButtonsNew = lpSS->styleNew & TVS_HASBUTTONS;
if (bButtonsOld != bButtonsNew)
RecalcGutter();
else if (bButtonsNew)
{
UINT uStyleOld = lpSS->styleOld & (TVS_HASLINES | TVS_LINESATROOT);
UINT uStyleNew = lpSS->styleNew & (TVS_HASLINES | TVS_LINESATROOT);
if (uStyleOld != uStyleNew)
RecalcGutter();
}
}
CTreeCtrl::OnStyleChanged(nStyleType, lpSS);
}
void COrderedTreeCtrl::OnSettingChange(UINT /*uFlags*/, LPCTSTR /*lpszSection*/)
{
// there is the strangest 'bug' under XP using the default 96DPI font
// setting, such that allowing the treectrl to receive this message
// results in corrupted expansion icons.
// to cause the minimum sideeffects we check for XP, theming and whether
// buttons are visible
// nope. disable completed for now
// if (!((GetStyle() & TVS_HASBUTTONS) && CThemed(this).AreControlsThemed()))
// CTreeCtrl::OnSettingChange(uFlags, lpszSection);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?