⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mytreectrl.cpp

📁 Visual_C++[1].NET_Bible1 Visual_C++宝典书中的全部源码
💻 CPP
字号:
// MyTreeCtrl.cpp : implementation file
//

#include "stdafx.h"
#include "TreeCtrlDemo.h"
#include "MyTreeCtrl.h"

#include "AddItemDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMyTreeCtrl

CMyTreeCtrl::CMyTreeCtrl()
{
}

CMyTreeCtrl::~CMyTreeCtrl()
{
}


BEGIN_MESSAGE_MAP(CMyTreeCtrl, CTreeCtrl)
	//{{AFX_MSG_MAP(CMyTreeCtrl)
	ON_WM_RBUTTONDOWN()
	ON_COMMAND(ID_CONTEXTMENU_ADD, OnContextmenuAdd)
	ON_NOTIFY_REFLECT(TVN_ENDLABELEDIT, OnEndlabeledit)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyTreeCtrl message handlers

void CMyTreeCtrl::OnRButtonDown(UINT nFlags, CPoint point) 
{
 // set focus to the tree control
 SetFocus();

 // map the point that is passed to the 
 // function from client coordinates
 // to screen coordinates
 ClientToScreen(&point);

 // Get the currently selected item
 HTREEITEM hCurrSel = GetSelectedItem();

 // Figure out which item was right clicked
 CPoint pt(0, 0);
 ::GetCursorPos(&pt);
 ScreenToClient (&pt);
 HTREEITEM hNewSel = HitTest(pt, &nFlags);

 // Set the selection to the item that the
 // mouse was over when the user right
 // clicked
 if (NULL == hNewSel)
 {
  SelectItem(NULL);
 }
 else if (hCurrSel != hNewSel)
 {
  SelectItem(hNewSel);
  SetFocus();
 }

 // Load the context menu
 CMenu Menu;
 if (Menu.LoadMenu(IDM_CONTEXT_MENU))
 {
  CMenu* pSubMenu = Menu.GetSubMenu(0);

  if (pSubMenu!=NULL)
  {
   // Display the context menu
   pSubMenu->TrackPopupMenu(
    TPM_LEFTALIGN | TPM_RIGHTBUTTON,
    point.x, point.y, this);
  }
 }
}

void CMyTreeCtrl::OnContextmenuAdd() 
{
 HTREEITEM hitemParent = GetSelectedItem();

 // Instantiate and display the Add dialog
 CAddItemDlg dlg;
 if (IDOK == dlg.DoModal())
 {
  // Only add the item if it is not a duplicate.
  if (!DoesItemExist(hitemParent, dlg.m_strItem))
  {
    // Add the new item to the treeview.
    HTREEITEM hitem = InsertItem(dlg.m_strItem,
                                hitemParent);

    // Select the new item. This also causes the
    // item抯 parent to expand.
    SelectItem(hitem);
   }
   else
   {
    CString str;
    str.Format("%s already exists", dlg.m_strItem);
    AfxMessageBox(str);
   }
 }	
}

BOOL CMyTreeCtrl::DoesItemExist(HTREEITEM hitemParent, CString const& strItem)
{
	BOOL bDoesItemExist = FALSE;

	ASSERT(strItem.GetLength());

	HTREEITEM hChild = 
	GetChildItem(hitemParent ? hitemParent : TVI_ROOT);

	while (NULL != hChild && !bDoesItemExist)
	{
		CString strText = GetItemText(hChild);
		if (0 == strText.CompareNoCase(strItem))
		{
			bDoesItemExist = TRUE;
		}
		else
		{
			hChild = GetNextSiblingItem( hChild );        
		}
	}

	return bDoesItemExist;
}

void CMyTreeCtrl::OnEndlabeledit(NMHDR* pNMHDR, LRESULT* pResult) 
{
	TV_DISPINFO* pTVDispInfo = (TV_DISPINFO*)pNMHDR;

	BOOL bValidItem = FALSE;

	CString strItem = pTVDispInfo->item.pszText;
	if (0 < strItem.GetLength())
	{
		HTREEITEM hParent = GetParentItem(pTVDispInfo->item.hItem);
		CString strItem = pTVDispInfo->item.pszText;

		bValidItem = !DoesItemExist(hParent, strItem);
	}

	*pResult = bValidItem;
}

BOOL CMyTreeCtrl::PreTranslateMessage(MSG* pMsg) 
{
 BOOL bHandledMsg = FALSE; 

 switch (pMsg->message) 
 {                       
  case WM_KEYDOWN: 
  { 
   switch (pMsg->wParam) 
   { 
    case VK_ESCAPE:
    case VK_RETURN:    
     if (::GetKeyState(VK_CONTROL) & 0x8000) 
     { 
      break; 
     } 
  
     if (GetEditControl())
     {
      ::TranslateMessage(pMsg);
      ::DispatchMessage(pMsg);
      bHandledMsg = TRUE; 
     }

    break; 

    default: break; 
   } // switch (pMsg->wParam) 
  } // WM_KEYDOWN 
  break; 

  default: break; 
 } // switch (pMsg->message)                   

 // continue normal translation and dispatching              
 return (bHandledMsg ? 
  TRUE : CTreeCtrl::PreTranslateMessage(pMsg)); 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -