From df4d0e875ccfe40add25100a75dedee54e566aaa Mon Sep 17 00:00:00 2001
From: LAPTOP-SNT8I5JK\Boounion <Chenluhua@qq.com>
Date: 星期四, 18 九月 2025 09:06:24 +0800
Subject: [PATCH] 1.CConrolJobManagerDlg临时数据存储。
---
SourceCode/Bond/Servo/CExpandableListCtrl.cpp | 410 +++++++++++++++++++++++++++++++++++++++++++++++++--------
1 files changed, 349 insertions(+), 61 deletions(-)
diff --git a/SourceCode/Bond/Servo/CExpandableListCtrl.cpp b/SourceCode/Bond/Servo/CExpandableListCtrl.cpp
index c15cf59..ef21ed0 100644
--- a/SourceCode/Bond/Servo/CExpandableListCtrl.cpp
+++ b/SourceCode/Bond/Servo/CExpandableListCtrl.cpp
@@ -3,7 +3,11 @@
IMPLEMENT_DYNAMIC(CExpandableListCtrl, CListCtrl)
-CExpandableListCtrl::CExpandableListCtrl() {}
+CExpandableListCtrl::CExpandableListCtrl()
+{
+ m_popupCols = { };
+}
+
CExpandableListCtrl::~CExpandableListCtrl() {}
BEGIN_MESSAGE_MAP(CExpandableListCtrl, CListCtrl)
@@ -17,20 +21,20 @@
if (CListCtrl::OnCreate(lpCreateStruct) == -1)
return -1;
- // 鎶ヨ〃椋庢牸鍒椾妇渚�
SetExtendedStyle(GetExtendedStyle()
- | LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP | LVS_EX_GRIDLINES);
-
- // 绀轰緥鍒楋紙鍙湪澶栭儴璁剧疆锛�
- if (GetHeaderCtrl() == nullptr || GetHeaderCtrl()->GetItemCount() == 0) {
- InsertColumn(0, _T("鍚嶇О"), LVCFMT_LEFT, 260);
- InsertColumn(1, _T("鐘舵��"), LVCFMT_LEFT, 120);
- InsertColumn(2, _T("鎻忚堪"), LVCFMT_LEFT, 260);
- }
+ | LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP | LVS_EX_GRIDLINES | LVS_EX_DOUBLEBUFFER);
return 0;
}
+void CExpandableListCtrl::PreSubclassWindow()
+{
+ SetExtendedStyle(GetExtendedStyle()
+ | LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP | LVS_EX_GRIDLINES | LVS_EX_DOUBLEBUFFER);
+ CListCtrl::PreSubclassWindow();
+}
+
+// ===== 鏍� API =====
CExpandableListCtrl::Node* CExpandableListCtrl::InsertRoot(const std::vector<CString>& cols)
{
auto n = std::make_unique<Node>((int)max(1, (int)cols.size()));
@@ -65,15 +69,12 @@
void CExpandableListCtrl::RebuildVisible()
{
- // 1) 閲嶅缓鍙搴忓垪
m_visible.clear();
for (auto& r : m_roots) appendVisible(r.get());
- // 2) 閲嶇粯/閲嶅~鏁版嵁
SetRedraw(FALSE);
DeleteAllItems();
- // 鎻掑叆鍙琛�
for (int i = 0; i < (int)m_visible.size(); ++i) {
Node* n = m_visible[i];
LVITEM lvi{};
@@ -83,11 +84,16 @@
lvi.pszText = const_cast<LPTSTR>((LPCTSTR)(n->cols.empty() ? _T("") : n->cols[0]));
InsertItem(&lvi);
- for (int col = 1; col < GetHeaderCtrl()->GetItemCount(); ++col) {
+ const int colCount = GetHeaderCtrl() ? GetHeaderCtrl()->GetItemCount() : 1;
+ for (int col = 1; col < colCount; ++col) {
CString txt = (col < (int)n->cols.size()) ? n->cols[col] : _T("");
SetItemText(i, col, txt);
}
}
+
+ // 閲嶅缓鍚庯紝鎸夎鍙烽鑹叉暟缁勫榻�
+ m_rowColors.resize(GetItemCount());
+
SetRedraw(TRUE);
Invalidate();
}
@@ -117,21 +123,121 @@
return m_visible[i];
}
+// ===== 棰滆壊 API =====
+void CExpandableListCtrl::SetNodeColor(Node* n, COLORREF text, COLORREF bk)
+{
+ if (!n) return;
+ RowColor rc{};
+ rc.text = text; rc.bk = bk;
+ rc.hasText = (text != CLR_DEFAULT);
+ rc.hasBk = (bk != CLR_DEFAULT);
+ m_colorByNode[n] = rc;
+
+ for (int i = 0; i < (int)m_visible.size(); ++i) {
+ if (m_visible[i] == n) {
+ RedrawItems(i, i);
+ UpdateWindow();
+ return;
+ }
+ }
+}
+
+void CExpandableListCtrl::ClearNodeColor(Node* n)
+{
+ if (!n) return;
+ auto it = m_colorByNode.find(n);
+ if (it != m_colorByNode.end()) {
+ m_colorByNode.erase(it);
+ for (int i = 0; i < (int)m_visible.size(); ++i) {
+ if (m_visible[i] == n) {
+ RedrawItems(i, i);
+ UpdateWindow();
+ return;
+ }
+ }
+ }
+}
+
+void CExpandableListCtrl::ClearAllColors()
+{
+ m_colorByNode.clear();
+ m_rowColors.clear();
+ Invalidate(FALSE);
+}
+
+// 鍏煎鏃ф帴鍙o細鎸夆�滃彲瑙佽鍙封�濈潃鑹�
+void CExpandableListCtrl::SetItemColor(DWORD_PTR iItem, COLORREF TextColor, COLORREF TextBkColor)
+{
+ SetItemColorByVisibleIndex((int)iItem, TextColor, TextBkColor);
+}
+void CExpandableListCtrl::SetItemColorByVisibleIndex(int row, COLORREF text, COLORREF bk)
+{
+ if (row < 0) return;
+ if (row >= (int)m_rowColors.size())
+ m_rowColors.resize(row + 1);
+
+ RowColor rc{};
+ rc.text = text; rc.bk = bk;
+ rc.hasText = (text != CLR_DEFAULT);
+ rc.hasBk = (bk != CLR_DEFAULT);
+ m_rowColors[row] = rc;
+
+ RedrawItems(row, row);
+ UpdateWindow();
+}
+
CRect CExpandableListCtrl::expanderRectForRow(int row) const
{
- CRect rc;
- // 鍙栭鍒楃煩褰�
- if (!GetSubItemRect(row, 0, LVIR_BOUNDS, rc))
+ CRect rcLabel;
+ if (!const_cast<CExpandableListCtrl*>(this)->GetSubItemRect(row, 0, LVIR_LABEL, rcLabel))
return CRect(0, 0, 0, 0);
Node* n = const_cast<CExpandableListCtrl*>(this)->GetNodeByVisibleIndex(row);
- int indent = (n ? n->level : 0);
+ if (!n || n->children.empty())
+ return CRect(0, 0, 0, 0);
- // 缂╄繘锛氭瘡绾х粰 16px
- int left = rc.left + m_expanderPadding + indent * 16;
- CRect box(left, rc.CenterPoint().y - m_expanderSize / 2,
- left + m_expanderSize, rc.CenterPoint().y + m_expanderSize / 2);
- return box;
+ const int indent = n->level;
+ const int left = rcLabel.left + m_expanderPadding + indent * 16;
+
+ return CRect(
+ left,
+ rcLabel.CenterPoint().y - m_expanderSize / 2,
+ left + m_expanderSize,
+ rcLabel.CenterPoint().y + m_expanderSize / 2
+ );
+}
+
+// 棰滆壊璁$畻锛氫紭鍏� Node*锛屽叾娆¤鍙凤紱鑻ラ渶瑕佸垯璁╃郴缁熼珮浜鐩�
+void CExpandableListCtrl::computeColorsForRow(int row, COLORREF& outText, COLORREF& outBk) const
+{
+ outText = ListView_GetTextColor(const_cast<CExpandableListCtrl*>(this)->m_hWnd);
+ outBk = ListView_GetBkColor(const_cast<CExpandableListCtrl*>(this)->m_hWnd);
+
+ const bool selected = (const_cast<CExpandableListCtrl*>(this)->GetItemState(row, LVIS_SELECTED) & LVIS_SELECTED) != 0;
+ const bool focusOnCtrl = (const_cast<CExpandableListCtrl*>(this)->GetSafeHwnd() == ::GetFocus());
+
+ if (m_preserveSelHighlight && selected) {
+ outBk = GetSysColor(focusOnCtrl ? COLOR_HIGHLIGHT : COLOR_3DFACE);
+ outText = GetSysColor(focusOnCtrl ? COLOR_HIGHLIGHTTEXT : COLOR_WINDOWTEXT);
+ return;
+ }
+
+ // Node* 棰滆壊
+ if (Node* n = const_cast<CExpandableListCtrl*>(this)->GetNodeByVisibleIndex(row)) {
+ auto it = m_colorByNode.find(n);
+ if (it != m_colorByNode.end()) {
+ if (it->second.hasText) outText = it->second.text;
+ if (it->second.hasBk) outBk = it->second.bk;
+ return;
+ }
+ }
+
+ // 琛屽彿棰滆壊
+ if (row >= 0 && row < (int)m_rowColors.size()) {
+ const RowColor& rc = m_rowColors[row];
+ if (rc.hasText) outText = rc.text;
+ if (rc.hasBk) outBk = rc.bk;
+ }
}
void CExpandableListCtrl::OnClick(NMHDR* pNMHDR, LRESULT* pResult)
@@ -139,8 +245,6 @@
LPNMITEMACTIVATE pia = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
if (pia->iItem >= 0) {
CPoint pt = pia->ptAction;
-
- // 鍛戒腑灞曞紑鎸夐挳锛�
CRect expRc = expanderRectForRow(pia->iItem);
if (expRc.PtInRect(pt)) {
Node* n = GetNodeByVisibleIndex(pia->iItem);
@@ -149,6 +253,38 @@
}
}
}
+
+ // 鈥斺�� 鑻ョ偣鍑诲埌闇�瑕佲�滃叏鏂囨樉绀衡�濈殑鍒楋紝鍒欏悜鐖剁獥鍙e彂閫佽嚜瀹氫箟閫氱煡 鈥斺�� //
+ if (!m_popupCols.empty()) {
+ LPNMITEMACTIVATE pia = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
+
+ // 鐢� SubItemHitTest 鏇寸簿鍑嗘嬁鍒板垪
+ LVHITTESTINFO ht{};
+ ht.pt = pia->ptAction;
+ int hit = SubItemHitTest(&ht);
+ if (hit >= 0 && ht.iItem >= 0 && ht.iSubItem >= 0) {
+ const int row = ht.iItem;
+ const int col = ht.iSubItem;
+
+ if (m_popupCols.count(col)) {
+ CString full = GetItemText(row, col);
+ if (!full.IsEmpty() && _IsCellTruncated(row, col, full)) {
+ NMC_ELC_SHOWFULLTEXT nm{};
+ nm.hdr.hwndFrom = m_hWnd;
+ nm.hdr.idFrom = GetDlgCtrlID();
+ nm.hdr.code = ELCN_SHOWFULLTEXT;
+ nm.iItem = row;
+ nm.iSubItem = col;
+ nm.text = full;
+
+ if (CWnd* pParent = GetParent()) {
+ pParent->SendMessage(WM_NOTIFY, nm.hdr.idFrom, reinterpret_cast<LPARAM>(&nm));
+ }
+ }
+ }
+ }
+ }
+
*pResult = 0;
}
@@ -163,51 +299,106 @@
return;
case CDDS_ITEMPREPAINT:
+ {
+ const int row = (int)pCD->nmcd.dwItemSpec;
+ COLORREF txt, bk;
+ computeColorsForRow(row, txt, bk);
+ pCD->clrText = txt;
+ pCD->clrTextBk = bk;
*pResult = CDRF_NOTIFYSUBITEMDRAW;
return;
+ }
case CDDS_ITEMPREPAINT | CDDS_SUBITEM:
{
- int row = (int)pCD->nmcd.dwItemSpec;
- int col = pCD->iSubItem;
+ const int row = (int)pCD->nmcd.dwItemSpec;
+ const int col = pCD->iSubItem;
CDC* pDC = CDC::FromHandle(pCD->nmcd.hdc);
- // 浠呭湪棣栧垪缁樺埗灞曞紑鎸夐挳涓庣缉杩涘紩瀵�
- if (col == 0) {
- CRect rc;
- GetSubItemRect(row, 0, LVIR_BOUNDS, rc);
-
- // 榛樿鏂囨湰璁╃郴缁熺敾锛氭垜浠厛鐢绘寜閽拰缂╄繘鑳屾櫙锛屽啀杩斿洖 CDRF_DODEFAULT
- Node* n = GetNodeByVisibleIndex(row);
- if (n) {
- // 缁樺埗灞曞紑涓夎/鏂瑰潡
- if (!n->children.empty()) {
- CRect box = expanderRectForRow(row);
- // 灏忔柟妗�
- pDC->Rectangle(box);
-
- // 鐢烩��+鈥濇垨鈥�-鈥�
- CPen pen(PS_SOLID, 1, RGB(0, 0, 0));
- CPen* oldPen = pDC->SelectObject(&pen);
- // 妯嚎
- pDC->MoveTo(box.left + 2, box.CenterPoint().y);
- pDC->LineTo(box.right - 2, box.CenterPoint().y);
- if (!n->expanded) {
- // 绔栫嚎锛堣〃绀� + 鍙凤級
- pDC->MoveTo(box.CenterPoint().x, box.top + 2);
- pDC->LineTo(box.CenterPoint().x, box.bottom - 2);
- }
- pDC->SelectObject(oldPen);
- }
-
- // 鎶婃枃鏈乏杈圭晫鍙崇Щ锛岀暀鍑虹缉杩涗笌鎸夐挳绌洪棿
- // 杩欓噷涓嶆敼绯荤粺缁樺埗鐨勬枃鏈捣鐐癸紝鑰屾槸閫氳繃鍦ㄦ枃鏈墠缃┖鏍肩殑鏂瑰紡澶勭悊鏇寸畝鍗曪細
- // 鎴戜滑鐩存帴鏀规樉绀烘枃鏈紙鎬ц兘瓒冲锛夛細鍦� RebuildVisible 鏃跺凡缁忓~浜嗙函鏂囨湰銆�
- // 濡傛灉浣犺绮惧噯鎺у埗鏂囨湰浣嶇疆锛屽彲浠ユ敼 OWNERDRAW 鎴栬嚜缁樻枃鏈��
- }
+ // 濡傛灉娌℃湁鏍戣妭鐐癸紙绾钩鍒楄〃锛夛紝棣栧垪涔熻蛋榛樿缁樺埗
+ Node* n = GetNodeByVisibleIndex(row);
+ if (col != 0 || !n) {
+ *pResult = CDRF_DODEFAULT;
+ return;
}
- *pResult = CDRF_DODEFAULT;
+ // 棣栧垪鑷粯锛堟爲妯″紡锛�
+ CRect rc; GetSubItemRect(row, 0, LVIR_LABEL, rc);
+
+ COLORREF bk, txt;
+ computeColorsForRow(row, txt, bk);
+
+ CBrush bkBrush(bk);
+ pDC->FillRect(rc, &bkBrush);
+
+ if (!n->children.empty())
+ {
+ CRect box = expanderRectForRow(row);
+ const int ROFFSET = 2;
+ const int WIDE = max(9, min(min(box.Width(), box.Height()), 13));
+ const int WIDE2 = WIDE / 2;
+ const int EXPANDED_WIDE = WIDE;
+
+ box.DeflateRect(1, 1);
+
+ POINT pt[3];
+ if (n->expanded) {
+ int nBottomOffset = (box.Height() - EXPANDED_WIDE) / 2;
+ pt[0].x = box.right - ROFFSET - EXPANDED_WIDE;
+ pt[0].y = box.bottom - nBottomOffset;
+ pt[1].x = box.right - ROFFSET;
+ pt[1].y = box.bottom - nBottomOffset;
+ pt[2].x = box.right - ROFFSET;
+ pt[2].y = box.bottom - nBottomOffset - EXPANDED_WIDE;
+ }
+ else {
+ int nBottomOffset = (box.Height() - WIDE) / 2;
+ pt[0].x = box.right - ROFFSET - WIDE2;
+ pt[0].y = box.bottom - nBottomOffset - WIDE;
+ pt[1].x = box.right - ROFFSET - WIDE2;
+ pt[1].y = box.bottom - nBottomOffset;
+ pt[2].x = box.right - ROFFSET;
+ pt[2].y = box.bottom - nBottomOffset - WIDE2;
+ }
+
+ HGDIOBJ oldPen = pDC->SelectObject(GetStockObject(NULL_PEN));
+ HBRUSH hBrush = CreateSolidBrush(txt);
+ HGDIOBJ oldBrush = pDC->SelectObject(hBrush);
+ pDC->Polygon(pt, 3);
+ pDC->SelectObject(oldPen);
+ pDC->SelectObject(oldBrush);
+ DeleteObject(hBrush);
+ }
+
+ const int indentPx = n->level * 14;
+ const int baseLeft = rc.left + m_expanderPadding + indentPx;
+
+ CRect textRc = rc;
+ if (!n->children.empty()) {
+ textRc.left = baseLeft + m_expanderSize + m_textGap;
+ }
+ else {
+ constexpr int kLeafGap = 2;
+ textRc.left = baseLeft + kLeafGap;
+ }
+
+ pDC->SetBkMode(TRANSPARENT);
+ pDC->SetTextColor(txt);
+ CString txt0 = n->cols.empty() ? _T("") : n->cols[0];
+ pDC->DrawText(txt0, textRc, DT_SINGLELINE | DT_VCENTER | DT_NOPREFIX | DT_END_ELLIPSIS);
+
+ if (GetExtendedStyle() & LVS_EX_GRIDLINES)
+ {
+ CRect rcRow; GetSubItemRect(row, 0, LVIR_BOUNDS, rcRow);
+ const int y = rcRow.bottom - 1;
+ CPen pen(PS_SOLID, 1, GetSysColor(COLOR_3DLIGHT));
+ CPen* oldPen = pDC->SelectObject(&pen);
+ pDC->MoveTo(rcRow.left, y);
+ pDC->LineTo(rcRow.right, y);
+ pDC->SelectObject(oldPen);
+ }
+
+ *pResult = CDRF_SKIPDEFAULT;
return;
}
}
@@ -215,3 +406,100 @@
*pResult = CDRF_DODEFAULT;
}
+// 鍏煎琛屼负锛氬悓姝� SetItemText 鍒� Node->cols锛涚淮鎶よ鍙烽鑹叉暟缁勭殑鎻掑叆/鍒犻櫎
+LRESULT CExpandableListCtrl::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
+{
+ // 鍚屾 SetItemText 鍒� Node锛圓/W 鍏煎锛�
+ if (message == LVM_SETITEMTEXT
+#ifdef LVM_SETITEMTEXTA
+ || message == LVM_SETITEMTEXTA
+#endif
+#ifdef LVM_SETITEMTEXTW
+ || message == LVM_SETITEMTEXTW
+#endif
+ )
+ {
+ int row = static_cast<int>(wParam);
+ LVITEM* p = reinterpret_cast<LVITEM*>(lParam);
+ if (p) {
+ Node* n = GetNodeByVisibleIndex(row);
+ if (n) {
+ int sub = p->iSubItem;
+ if (sub >= (int)n->cols.size())
+ n->cols.resize(sub + 1);
+ CString newText = p->pszText ? p->pszText : _T("");
+ n->cols[sub] = newText;
+ }
+ }
+ // 缁х画浜ょ粰鍩虹被澶勭悊
+ }
+
+ LRESULT nRet = CListCtrl::WindowProc(message, wParam, lParam);
+
+ // 缁存姢琛屽彿棰滆壊鏁扮粍锛堝吋瀹规棫 SetItemColor锛�
+ if (message == LVM_INSERTITEM) {
+ if (nRet != -1) {
+ LVITEM* p = (LVITEM*)lParam;
+ int pos = p ? p->iItem : (int)nRet;
+ if (pos < 0) pos = (int)nRet;
+ if (pos > (int)m_rowColors.size()) pos = (int)m_rowColors.size();
+ m_rowColors.insert(m_rowColors.begin() + pos, RowColor{}); // 榛樿鑹�
+ }
+ }
+ else if (message == LVM_DELETEITEM) {
+ if (nRet != 0) {
+ int pos = (int)wParam;
+ if (pos >= 0 && pos < (int)m_rowColors.size())
+ m_rowColors.erase(m_rowColors.begin() + pos);
+ }
+ }
+ else if (message == LVM_DELETEALLITEMS) {
+ if (nRet != 0) {
+ m_rowColors.clear();
+ }
+ }
+
+ return nRet;
+}
+
+// CExpandableListCtrl.cpp 閲�
+void CExpandableListCtrl::ClearTree()
+{
+ // 娓呮暟鎹�
+ m_roots.clear();
+ m_visible.clear();
+
+ // 娓呭彲瑙侀」锛堝姟蹇咃紒鍚﹀垯鏃ч〉鐨勮浼氭畫鐣欙級
+ SetRedraw(FALSE);
+ DeleteAllItems();
+ SetRedraw(TRUE);
+
+ Invalidate();
+}
+
+void CExpandableListCtrl::SetPopupFullTextColumns(const std::vector<int>& cols)
+{
+ m_popupCols.clear();
+ for (int c : cols) m_popupCols.insert(c);
+}
+
+bool CExpandableListCtrl::_IsCellTruncated(int row, int col, const CString& text) const
+{
+ if (text.IsEmpty()) return false;
+
+ // 鍗曞厓鏍兼樉绀哄尯鍩熷搴�
+ CRect rcCell;
+ if (!const_cast<CExpandableListCtrl*>(this)->GetSubItemRect(row, col, LVIR_BOUNDS, rcCell))
+ return false;
+
+ // 鐢ㄦ帶浠跺瓧浣撴祴閲忔枃鏈儚绱犲
+ CClientDC dc(const_cast<CExpandableListCtrl*>(this));
+ CFont* pOld = dc.SelectObject(const_cast<CExpandableListCtrl*>(this)->GetFont());
+ CSize sz = dc.GetTextExtent(text);
+ dc.SelectObject(pOld);
+
+ const int kPadding = 8; // 棰勭暀涓�鐐硅竟璺�/鐪佺暐鍙蜂綑閲�
+ return sz.cx > (rcCell.Width() - kPadding);
+}
+
+
--
Gitblit v1.9.3