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

📄 ws1.cpp

📁 这是书上的代码
💻 CPP
字号:
// WS1.cpp : implementation file
//

#include "stdafx.h"
#include "TestLib.h"
#include "WS1.h"

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

/////////////////////////////////////////////////////////////////////////////
// CWS1

IMPLEMENT_DYNCREATE(CWS1, CFormView)

CWS1::CWS1()
	: CFormView(CWS1::IDD)
{
	//{{AFX_DATA_INIT(CWS1)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_curDType=0;	//未指向任何数据类型
}

CWS1::~CWS1()
{
}

void CWS1::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CWS1)
	DDX_Control(pDX, IDC_TREE1, m_treeCtrl);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CWS1, CFormView)
	//{{AFX_MSG_MAP(CWS1)
	ON_WM_SIZE()
	ON_NOTIFY(NM_RCLICK, IDC_TREE1, OnRclickTree1)
	ON_NOTIFY(TVN_SELCHANGED, IDC_TREE1, OnSelchangedTree1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWS1 diagnostics

#ifdef _DEBUG
void CWS1::AssertValid() const
{
	CFormView::AssertValid();
}

void CWS1::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CWS1 message handlers

void CWS1::OnSize(UINT nType, int cx, int cy) 
{
	CFormView::OnSize(nType, cx, cy);

	CWnd*	pWndTree;
	pWndTree =GetDlgItem(IDC_TREE1);
	if(pWndTree->GetSafeHwnd())
		pWndTree->MoveWindow(0,0,cx,cy);
		
}

void CWS1::OnInitialUpdate() 
{
	CFormView::OnInitialUpdate();
	
	m_imgState.Create(IDB_BITMAP_STATE,13, 1, RGB(255,255,255));
	m_imgList.Create(IDB_BITMAP_LIST,16, 1, RGB(255,0,255));
	
	m_treeCtrl.SetImageList(&m_imgList,TVSIL_NORMAL);
	m_treeCtrl.SetImageList(&m_imgState,TVSIL_STATE);
	
	TV_INSERTSTRUCT tvinsert;
	tvinsert.hParent=NULL;
	tvinsert.hInsertAfter=TVI_LAST;
	tvinsert.item.mask=TVIF_IMAGE|TVIF_SELECTEDIMAGE|TVIF_TEXT|TVIF_STATE;
	tvinsert.item.hItem=NULL;
	tvinsert.item.state=INDEXTOSTATEIMAGEMASK(1);
	tvinsert.item.stateMask=TVIS_STATEIMAGEMASK;
	tvinsert.item.cchTextMax=6;
	tvinsert.item.iSelectedImage=1;
	tvinsert.item.cChildren=0;
	tvinsert.item.lParam=0;
	
	tvinsert.item.pszText="所有数据类型";
	tvinsert.item.iImage=0;
	HTREEITEM hRoot=m_treeCtrl.InsertItem(&tvinsert);
	
	
	m_treeCtrl.SetItemState( hRoot, INDEXTOSTATEIMAGEMASK(0)|TVS_HASLINES, TVIS_STATEIMAGEMASK );
	
	//second level
	tvinsert.hParent=hRoot;
	tvinsert.item.iImage=4;
	tvinsert.item.iSelectedImage=5;
	tvinsert.item.pszText="类型1";
	m_treeCtrl.InsertItem(&tvinsert);
	
	tvinsert.hParent=hRoot;
	tvinsert.item.pszText="类型2";
	m_treeCtrl.InsertItem(&tvinsert);
	
	tvinsert.hParent=hRoot;
	tvinsert.item.pszText="类型3";
	m_treeCtrl.InsertItem(&tvinsert);
	
	tvinsert.hParent=hRoot;
	tvinsert.item.pszText="类型4";
	m_treeCtrl.InsertItem(&tvinsert);
	
	tvinsert.hParent=NULL;
    tvinsert.item.pszText="功能模块1";
	tvinsert.item.iImage=2;
	m_treeCtrl.InsertItem(&tvinsert);
	tvinsert.item.pszText="功能模块2";
	tvinsert.item.iImage=2;
	tvinsert.item.iSelectedImage=3;
	m_treeCtrl.InsertItem(&tvinsert);	

	m_treeCtrl.EnableMultiSelect();//允许多选

}

	
void CWS1::OnRclickTree1(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_LISTVIEW		*pNMTreeView;
	CMenu			pPopupMenu;
	CMenu			*pPopup=NULL;
	POINT			pt;
	TVHITTESTINFO	tvhti;
	TV_ITEM			tvi;
	TCHAR			szText[128];
	BOOL            bRtn=FALSE;
	
	pNMTreeView = (NM_LISTVIEW*)pNMHDR;
	::GetCursorPos((LPPOINT)&pt);
	::ScreenToClient(GetSafeHwnd(), &pt);
	tvhti.pt=pt;
	m_treeCtrl.HitTest(&tvhti);
	if (tvhti.flags & LVHT_ONITEM)
	{	
		::ClientToScreen(GetSafeHwnd(), &pt);
		tvi.mask = TVIF_HANDLE|TVIF_TEXT;
		tvi.hItem=tvhti.hItem;
		tvi.pszText=szText;
		tvi.cchTextMax=127;
		if (!m_treeCtrl.GetItem(&tvi))
			goto TheEnd;
		
		if(strncmp(szText,"类型",4)==0)
		{
			bRtn=pPopupMenu.LoadMenu(IDR_MENU1);
		}
		if(strncmp(szText,"功能模块",8)==0)
		{
			bRtn=pPopupMenu.LoadMenu(IDR_TESTLITYPE);
		}
		
		if(strncmp(szText,"IDF",3)==0)
		{
			bRtn=pPopupMenu.LoadMenu(IDR_MENU1);
		}
		if(bRtn==FALSE)
			goto TheEnd;
		pPopup = pPopupMenu.GetSubMenu(0);
		if(pPopup)
			pPopup->TrackPopupMenu(TPM_LEFTALIGN|TPM_LEFTBUTTON, pt.x, pt.y,(CWnd *)this);
	}	
TheEnd:	
	*pResult = 0;

}

void CWS1::ExecuteMenu(UINT dType,CWnd* wnd)
{
	CMenu	popMenu;
	CWnd*	pwnd;
	CPoint	posMouse;
	UINT	id;

	switch(dType)
		{
		case 1:id=IDR_DS_WS;break;
		case 2:id=IDR_HDF_MENU;break;
		case 3:id=IDR_IDF_MENU;break;
		default:id=0;break;
		}
	
	if(id==0)
		return;

	if (wnd == NULL)
		pwnd = AfxGetMainWnd();
	else
		pwnd = wnd;

	if(pwnd==NULL)
		return;
	popMenu.LoadMenu(id);
	
	::GetCursorPos(&posMouse);
	popMenu.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON,posMouse.x,posMouse.y,pwnd);
}

void CWS1::OnSelchangedTree1(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_LISTVIEW		*pNMTreeView;
	POINT			pt;
	TVHITTESTINFO	tvhti;
	TV_ITEM			tvi;
	TCHAR			szText[128];
	BOOL            bRtn=FALSE;
	
	pNMTreeView = (NM_LISTVIEW*)pNMHDR;
	::GetCursorPos((LPPOINT)&pt);
	::ScreenToClient(GetSafeHwnd(), &pt);
	tvhti.pt=pt;
	m_treeCtrl.HitTest(&tvhti);
	if (tvhti.flags & LVHT_ONITEM)
	{	
		::ClientToScreen(GetSafeHwnd(), &pt);
		tvi.mask = TVIF_HANDLE|TVIF_TEXT;
		tvi.hItem=tvhti.hItem;
		tvi.pszText=szText;
		tvi.cchTextMax=127;
		if (!m_treeCtrl.GetItem(&tvi))
			goto TheEnd;
		
		if(strncmp(szText,"类型",4)==0)
		{
			m_curDType=1;
		}
		if(strncmp(szText,"功能模块1",9)==0)
		{
			m_curDType=2;
		}
		
		if(strncmp(szText,"功能模块2",9)==0)
		{
			m_curDType=2;
		}
	}	
TheEnd:	
	*pResult = 0;
}

⌨️ 快捷键说明

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