QuakeGod
2023-10-20 6fa60de2b0d0237054aa7571191df0f291838031
提交 | 用户 | age
418cb3 1 
Q 2 #include "pch.h"
3 #include "framework.h"
4 #include "Resource.h"
5 #include "MainFrm.h"
6 #include "NavView.h"
7
8 #include "MTerm1.h"
9
10 #define IDC_NAVTREE1 1046
11 class CClassViewMenuButton : public CMFCToolBarMenuButton
12 {
13     friend class CNavView;
14
15     DECLARE_SERIAL(CClassViewMenuButton)
16
17 public:
18     CClassViewMenuButton(HMENU hMenu = nullptr) noexcept : CMFCToolBarMenuButton((UINT)-1, hMenu, -1)
19     {
20     }
21
22     virtual void OnDraw(CDC* pDC, const CRect& rect, CMFCToolBarImages* pImages, BOOL bHorz = TRUE,
23         BOOL bCustomizeMode = FALSE, BOOL bHighlight = FALSE, BOOL bDrawBorder = TRUE, BOOL bGrayDisabledButtons = TRUE)
24     {
25         pImages = CMFCToolBar::GetImages();
26
27         CAfxDrawState ds;
28         pImages->PrepareDrawImage(ds);
29
30         CMFCToolBarMenuButton::OnDraw(pDC, rect, pImages, bHorz, bCustomizeMode, bHighlight, bDrawBorder, bGrayDisabledButtons);
31
32         pImages->EndDrawImage(ds);
33     }
34 };
35
36 IMPLEMENT_SERIAL(CClassViewMenuButton, CMFCToolBarMenuButton, 1)
37
38 //////////////////////////////////////////////////////////////////////
39 // 构造/析构
40 //////////////////////////////////////////////////////////////////////
41
42 CNavView::CNavView() noexcept
43 {
44     m_nCurrSort = ID_SORTING_GROUPBYTYPE;
45 }
46
47 CNavView::~CNavView()
48 {
49 }
50
51 BEGIN_MESSAGE_MAP(CNavView, CDockablePane)
52     ON_WM_CREATE()
53     ON_WM_SIZE()
54     ON_WM_CONTEXTMENU()
55 //    ON_COMMAND(ID_CLASS_ADD_MEMBER_FUNCTION, OnClassAddMemberFunction)
56 //    ON_COMMAND(ID_CLASS_ADD_MEMBER_VARIABLE, OnClassAddMemberVariable)
57 //    ON_COMMAND(ID_CLASS_DEFINITION, OnClassDefinition)
58 //    ON_COMMAND(ID_CLASS_PROPERTIES, OnClassProperties)
59     ON_COMMAND(ID_NEW_FOLDER, OnNewFolder)
60     ON_WM_PAINT()
61     ON_WM_SETFOCUS()
62     ON_COMMAND_RANGE(ID_SORTING_GROUPBYTYPE, ID_SORTING_SORTBYACCESS, OnSort)
63     ON_UPDATE_COMMAND_UI_RANGE(ID_SORTING_GROUPBYTYPE, ID_SORTING_SORTBYACCESS, OnUpdateSort)
64     ON_NOTIFY(NM_DBLCLK, IDC_NAVTREE1, OnNMDblclkNavTree1)
65     ON_NOTIFY(NM_CLICK, IDC_NAVTREE1, OnNMClkNavTree1)
66
67     ON_WM_TIMER()
68 END_MESSAGE_MAP()
69
70 /////////////////////////////////////////////////////////////////////////////
71 // CNavView 消息处理程序
72
73 int CNavView::OnCreate(LPCREATESTRUCT lpCreateStruct)
74 {
75     if (CDockablePane::OnCreate(lpCreateStruct) == -1)
76         return -1;
77
78     CRect rectDummy;
79     rectDummy.SetRectEmpty();
80
81     // 创建视图: 
82     const DWORD dwViewStyle = WS_CHILD | WS_VISIBLE | TVS_LINESATROOT | TVS_HASLINES  | TVS_HASBUTTONS | WS_CLIPSIBLINGS | WS_CLIPCHILDREN ; //
83
84     if (!m_NavViewTree.Create(dwViewStyle, rectDummy, this, IDC_NAVTREE1))
85     {
86         TRACE0("未能创建导航栏视图\n");
87         return -1;      // 未能创建
88     }
89     m_NavViewTree.SetExtendedStyle(TVS_EX_DOUBLEBUFFER, TVS_EX_DOUBLEBUFFER);
90     // 加载图像: 
91     m_wndToolBar.Create(this, AFX_DEFAULT_TOOLBAR_STYLE, IDR_SORT);
92     m_wndToolBar.LoadToolBar(IDR_SORT, 0, 0, TRUE /* 已锁定*/);
93
94     OnChangeVisualStyle();
95
96     m_wndToolBar.SetPaneStyle(m_wndToolBar.GetPaneStyle() | CBRS_TOOLTIPS | CBRS_FLYBY);
97     m_wndToolBar.SetPaneStyle(m_wndToolBar.GetPaneStyle() & ~(CBRS_GRIPPER | CBRS_SIZE_DYNAMIC | CBRS_BORDER_TOP | CBRS_BORDER_BOTTOM | CBRS_BORDER_LEFT | CBRS_BORDER_RIGHT));
98
99     m_wndToolBar.SetOwner(this);
100
101     // 所有命令将通过此控件路由,而不是通过主框架路由: 
102     m_wndToolBar.SetRouteCommandsViaFrame(FALSE);
103
104     CMenu menuSort;
105     menuSort.LoadMenu(IDR_POPUP_SORT);
106
107     m_wndToolBar.ReplaceButton(ID_SORT_MENU, CClassViewMenuButton(menuSort.GetSubMenu(0)->GetSafeHmenu()));
108
109     CClassViewMenuButton* pButton =  DYNAMIC_DOWNCAST(CClassViewMenuButton, m_wndToolBar.GetButton(0));
110
111     if (pButton != nullptr)
112     {
113         pButton->m_bText = FALSE;
114         pButton->m_bImage = TRUE;
115         pButton->SetImage(GetCmdMgr()->GetCmdImage(m_nCurrSort));
116         pButton->SetMessageWnd(this);
117     }
118
119     // 填入一些静态树视图数据(此处只需填入虚拟代码,而不是复杂的数据)
120     SetTimer(0, 100, NULL);
121     return 0;
122 }
123
124 void CNavView::OnSize(UINT nType, int cx, int cy)
125 {
126     CDockablePane::OnSize(nType, cx, cy);
127     AdjustLayout();
128 }
129
130 void CNavView::FillClassView()
131 {
132     HTREEITEM hRoot1 = m_NavViewTree.InsertItem(_T("连接1 Via COM3 230400 8-N-1 KLinkV1.00"), 0, 0);
133     m_NavViewTree.SetItemState(hRoot1, TVIS_BOLD, TVIS_BOLD);
134     HTREEITEM hItem;
135     HTREEITEM hClass1 = m_NavViewTree.InsertItem(_T("KL10-E16D-N1  1/1 Run, Good"), 5, 5, hRoot1);
136     m_NavViewTree.SetItemData(hClass1, 10);
137     hItem = m_NavViewTree.InsertItem(_T("设备信息"), 3, 3, hClass1);
138     m_NavViewTree.SetItemData(hItem, 11);
139     hItem = m_NavViewTree.InsertItem(_T("工作状态,系统参数"), 3, 3, hClass1);
140     m_NavViewTree.SetItemData(hItem, 12);
141
142     HTREEITEM hPort2 = m_NavViewTree.InsertItem(_T("端口2 485 KBus 本机地址(0)/5 Run "), 1, 1, hClass1);
143     m_NavViewTree.SetItemData(hPort2, 13);
144     hItem = m_NavViewTree.InsertItem(_T("1 KL10-E08D-N1 Run Good"), 5, 5, hPort2);
145     m_NavViewTree.SetItemData(hItem, 131);
146     hItem = m_NavViewTree.InsertItem(_T("2 KL10-E08D-N1 Run bad"), 5, 5, hPort2);
147     m_NavViewTree.SetItemData(hItem, 132);
148     hItem = m_NavViewTree.InsertItem(_T("3 KL10-E08D-N1 Run Good"), 5, 5, hPort2);
149     m_NavViewTree.SetItemData(hItem, 133);
150     hItem = m_NavViewTree.InsertItem(_T("4 KL10-E08D-N1 Run Good"), 5, 5, hPort2);
151     m_NavViewTree.SetItemData(hItem, 134);
152     hItem = m_NavViewTree.InsertItem(_T("5 KL10-E08D-N1 Run bad"), 5, 5, hPort2);
153     m_NavViewTree.SetItemData(hItem, 135);
154     
155     HTREEITEM hPort3 = m_NavViewTree.InsertItem(_T("端口3 WireLess "), 1, 1, hClass1);
156     m_NavViewTree.SetItemData(hPort3, 14);
157
158     m_NavViewTree.SetBkColor(COLORREF(0xf0f0f0f0) );
159
160     m_NavViewTree.Expand(hRoot1, TVE_EXPAND);
161
162     HTREEITEM hClass2 = m_NavViewTree.InsertItem(_T("CFakeAboutDlg"), 1, 1, hRoot1);
163     m_NavViewTree.SetItemData(hClass2, 20);
164     hItem = m_NavViewTree.InsertItem(_T("CFakeAboutDlg()"), 3, 3, hClass2);
165     m_NavViewTree.SetItemData(hItem, 21);
166
167     HTREEITEM hClass3 = m_NavViewTree.InsertItem(_T("CFakeAppDoc"), 1, 1, hRoot1);
168     m_NavViewTree.SetItemData(hClass3, 30);
169     hItem = m_NavViewTree.InsertItem(_T("CFakeAppDoc()"), 4, 4, hClass3);
170     m_NavViewTree.SetItemData(hItem, 31);
171     hItem = m_NavViewTree.InsertItem(_T("~CFakeAppDoc()"), 3, 3, hClass3);
172     m_NavViewTree.SetItemData(hItem, 32);
173     hItem = m_NavViewTree.InsertItem(_T("OnNewDocument()"), 3, 3, hClass3);
174     m_NavViewTree.SetItemData(hItem, 33);
175
176     HTREEITEM hClass4 = m_NavViewTree.InsertItem(_T("CFakeAppView"), 1, 1, hRoot1);
177     m_NavViewTree.SetItemData(hClass4, 40);
178     hItem = m_NavViewTree.InsertItem(_T("CFakeAppView()"), 4, 4, hClass4);
179     m_NavViewTree.SetItemData(hItem, 41);
180     hItem = m_NavViewTree.InsertItem(_T("~CFakeAppView()"), 3, 3, hClass4);
181     m_NavViewTree.SetItemData(hItem, 42);
182     hItem = m_NavViewTree.InsertItem(_T("GetDocument()"), 3, 3, hClass4);
183     m_NavViewTree.SetItemData(hItem, 43);
184     m_NavViewTree.Expand(hClass4, TVE_EXPAND);
185
186     HTREEITEM hClass5 = m_NavViewTree.InsertItem(_T("Globals"), 2, 2, hRoot1);
187     m_NavViewTree.SetItemData(hClass5, 50);
188     hItem = m_NavViewTree.InsertItem(_T("theFakeApp"), 5, 5, hClass5);
189     m_NavViewTree.SetItemData(hItem, 51);
190     m_NavViewTree.Expand(hClass5, TVE_EXPAND);
191
192     HTREEITEM hRoot2 = m_NavViewTree.InsertItem(_T("连接状态2"), 0, 0);
193
194     m_NavViewTree.SetItemState(hRoot2, TVIS_BOLD, TVIS_BOLD);
195
196
197 }
198
199 void CNavView::OnContextMenu(CWnd* pWnd, CPoint point)
200 {
201     CTreeCtrl* pWndTree = (CTreeCtrl*)&m_NavViewTree;
202     ASSERT_VALID(pWndTree);
203
204     if (pWnd != pWndTree)
205     {
206         CDockablePane::OnContextMenu(pWnd, point);
207         return;
208     }
209
210     if (point != CPoint(-1, -1))
211     {
212         // 选择已单击的项: 
213         CPoint ptTree = point;
214         pWndTree->ScreenToClient(&ptTree);
215
216         UINT flags = 0;
217         HTREEITEM hTreeItem = pWndTree->HitTest(ptTree, &flags);
218         if (hTreeItem != nullptr)
219         {
220             pWndTree->SelectItem(hTreeItem);
221         }
222     }
223
224     pWndTree->SetFocus();
225     CMenu menu;
226     menu.LoadMenu(IDR_POPUP_SORT);
227
228     CMenu* pSumMenu = menu.GetSubMenu(0);
229
230     if (AfxGetMainWnd()->IsKindOf(RUNTIME_CLASS(CMDIFrameWndEx)))
231     {
232         CMFCPopupMenu* pPopupMenu = new CMFCPopupMenu;
233
234         if (!pPopupMenu->Create(this, point.x, point.y, (HMENU)pSumMenu->m_hMenu, FALSE, TRUE))
235             return;
236
237         ((CMDIFrameWndEx*)AfxGetMainWnd())->OnShowPopupMenu(pPopupMenu);
238         UpdateDialogControls(this, FALSE);
239     }
240 }
241
242 void CNavView::AdjustLayout()
243 {
244     if (GetSafeHwnd() == nullptr)
245     {
246         return;
247     }
248
249     CRect rectClient;
250     GetClientRect(rectClient);
251
252     int cyTlb = m_wndToolBar.CalcFixedLayout(FALSE, TRUE).cy;
253
254     m_wndToolBar.SetWindowPos(nullptr, rectClient.left, rectClient.top, rectClient.Width(), cyTlb, SWP_NOACTIVATE | SWP_NOZORDER);
255     m_NavViewTree.SetWindowPos(nullptr, rectClient.left + 1, rectClient.top + cyTlb + 1, rectClient.Width() - 2, rectClient.Height() - cyTlb - 2, SWP_NOACTIVATE | SWP_NOZORDER);
256 }
257
258 BOOL CNavView::PreTranslateMessage(MSG* pMsg)
259 {
260     return CDockablePane::PreTranslateMessage(pMsg);
261 }
262
263 void CNavView::OnSort(UINT id)
264 {
265     if (m_nCurrSort == id)
266     {
267         return;
268     }
269
270     m_nCurrSort = id;
271
272     CClassViewMenuButton* pButton =  DYNAMIC_DOWNCAST(CClassViewMenuButton, m_wndToolBar.GetButton(0));
273
274     if (pButton != nullptr)
275     {
276         pButton->SetImage(GetCmdMgr()->GetCmdImage(id));
277         m_wndToolBar.Invalidate();
278         m_wndToolBar.UpdateWindow();
279     }
280 }
281
282
283 void CNavView::OnTimer(UINT_PTR nIDEvent)
284 {
285     // TODO: 在此添加消息处理程序代码和/或调用默认值
286     if (nIDEvent == 0) {
287         KillTimer(0);
288         DelayInit();
289         SetTimer(1, 200, NULL);
290     }
291     else if (nIDEvent == 1) {
292         UpdateDisplay();
293     }
294     else if (nIDEvent == 2) {
295
296     }
297     else if (nIDEvent == 3) {
298
299     }
300     CDockablePane::OnTimer(nIDEvent);
301 }
302
303
304 int CNavView::DelayInit()
305 {
306     // TODO: 在此处添加实现代码.
307     FillClassView();
308     return 0;
309 }
310
311 int CNavView::UpdateDisplay()
312 {
313     // TODO: 在此处添加实现代码.
314 //    m_NavViewTree.DeleteAllItems();
315 //    FillClassView();
316     CString s1;
317     HTREEITEM hroot1 =  m_NavViewTree.GetRootItem();
318     HTREEITEM hItem1 = m_NavViewTree.GetChildItem(hroot1);
319     nDisplayCount++;
320     s1.Format(_T("KL10-E16D-N1  1/1 Run, Good  %d"), nDisplayCount);
321     m_NavViewTree.SetItemText(hItem1, s1);
322     return 0;
323 }
324
325 void CNavView::OnUpdateSort(CCmdUI* pCmdUI)
326 {
327     pCmdUI->SetCheck(pCmdUI->m_nID == m_nCurrSort);
328 }
329
330 void CNavView::OnClassAddMemberFunction()
331 {
332     AfxMessageBox(_T("添加成员函数..."));
333 }
334
335 void CNavView::OnClassAddMemberVariable()
336 {
337     // TODO: 在此处添加命令处理程序代码
338 }
339
340 void CNavView::OnClassDefinition()
341 {
342     // TODO: 在此处添加命令处理程序代码
343 }
344
345 void CNavView::OnClassProperties()
346 {
347     // TODO: 在此处添加命令处理程序代码
348 }
349
350 void CNavView::OnNewFolder()
351 {
352     AfxMessageBox(_T("新建文件夹..."));
353 }
354
355 void CNavView::OnPaint()
356 {
357     CPaintDC dc(this); // 用于绘制的设备上下文
358
359     CRect rectTree;
360     m_NavViewTree.GetWindowRect(rectTree);
361     ScreenToClient(rectTree);
362
363     rectTree.InflateRect(1, 1);
364     dc.Draw3dRect(rectTree, ::GetSysColor(COLOR_3DSHADOW), ::GetSysColor(COLOR_3DSHADOW));
365 }
366
367 void CNavView::OnSetFocus(CWnd* pOldWnd)
368 {
369     CDockablePane::OnSetFocus(pOldWnd);
370
371     m_NavViewTree.SetFocus();
372 }
373
374 void CNavView::OnChangeVisualStyle()
375 {
376     m_NavViewImages.DeleteImageList();
377
378     UINT uiBmpId = theApp.m_bHiColorIcons ? IDB_CLASS_VIEW_24 : IDB_CLASS_VIEW;
379 //    uiBmpId = IDB_NAV_VIEW1;
380     CBitmap bmp;
381     if (!bmp.LoadBitmap(uiBmpId))
382     {
383         TRACE(_T("无法加载位图: %x\n"), uiBmpId);
384         ASSERT(FALSE);
385         return;
386     }
387
388     BITMAP bmpObj;
389     bmp.GetBitmap(&bmpObj);
390
391     UINT nFlags = ILC_MASK;
392
393     nFlags |= (theApp.m_bHiColorIcons) ? ILC_COLOR24 : ILC_COLOR4;
394
395     m_NavViewImages.Create(16, bmpObj.bmHeight, nFlags, 0, 0);
396     m_NavViewImages.Add(&bmp, RGB(255, 0, 0));
397
398     m_NavViewTree.SetImageList(&m_NavViewImages, TVSIL_NORMAL);
399
400     m_wndToolBar.CleanUpLockedImages();
401     m_wndToolBar.LoadBitmap(theApp.m_bHiColorIcons ? IDB_SORT_24 : IDR_SORT, 0, 0, TRUE /* 锁定*/);
402 }
403
404 void CNavView::OnNMClkNavTree1(NMHDR* pNMHDR, LRESULT* pResult)
405 {
406     LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);
407 //    HTREEITEM hTreeItem = pNMTreeView->itemNew.hItem;
408 //    HTREEITEM hTreeItem = m_NavViewTree.GetSelectedItem();
409     //以GetItemText()函数为例: 
410     
411 //    CString s2 = m_NavViewTree.GetItemText(hTreeItem);
412     //AfxMessageBox(_T("You have selected ") + s1);
413 //    CString s1;
414 //    DWORD_PTR dw = m_NavViewTree.GetItemData(hTreeItem);
415 //    s1.Format(_T("Form Clk action %d, id %d oldid %d data %d x %d y %d  text %s "),
416 //        pNMTreeView->action, hTreeItem, pNMTreeView->itemOld.hItem, dw, pNMTreeView->ptDrag.x, pNMTreeView->ptDrag.y, s2);
417
418 //    SysLog(s1);
419
420     *pResult = 0;
421 }
422
423
424 void CNavView::OnNMDblclkNavTree1(NMHDR* pNMHDR, LRESULT* pResult)
425 {
426     LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);
427
428     HTREEITEM hTreeItem = m_NavViewTree.GetSelectedItem();
429     //以GetItemText()函数为例: 
430     CString s2 = m_NavViewTree.GetItemText(hTreeItem);
431     //AfxMessageBox(_T("You have selected ") + s1);
432     CString s1;
433     DWORD_PTR dw = m_NavViewTree.GetItemData(hTreeItem);
434     s1.Format(_T("Form dbclk action %d, id %d oldid %d data %d x %d y %d  text %s "),
435         pNMTreeView->action, hTreeItem, pNMTreeView->itemOld.hItem, dw, pNMTreeView->ptDrag.x, pNMTreeView->ptDrag.y, s2);
436
437     SysLog(s1);
438
439     *pResult = 0;
440 }
441
442