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

📄 leftview.cpp

📁 一个网络监视的程序
💻 CPP
字号:
// LeftView.cpp : implementation file
//

#include "stdafx.h"
#include "ccams.h"
#include "LeftView.h"
#include "SetClient.h"

//#include "ActivClient.h"

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

/////////////////////////////////////////////////////////////////////////////
// CLeftView

IMPLEMENT_DYNCREATE(CLeftView, CFormView)

CLeftView::CLeftView()
	: CFormView(CLeftView::IDD)
{
	m_hPreItem = 0;
	//{{AFX_DATA_INIT(CLeftView)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}

CLeftView::~CLeftView()
{
}

void CLeftView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CLeftView)
	DDX_Control(pDX, IDC_TREE_CLIENTS, m_treeClients);
	DDX_Control(pDX, IDC_BUTTON_HISDIAG, m_butDiag);
	DDX_Control(pDX, IDC_BUTTON_HISLIST, m_butList);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CLeftView, CFormView)
	//{{AFX_MSG_MAP(CLeftView)
	ON_WM_SIZE()
	ON_BN_CLICKED(IDC_BUTTON_HISLIST, OnButtonHisList)
	ON_BN_CLICKED(IDC_BUTTON_HISDIAG, OnButtonHisDiag)
	ON_WM_TIMER()
	ON_NOTIFY(NM_DBLCLK, IDC_TREE_CLIENTS, OnDblclkTreeClients)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLeftView diagnostics

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

void CLeftView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

CCCAMSDoc* CLeftView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCCAMSDoc)));
	return (CCCAMSDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CLeftView message handlers

//===============================================================
// 函 数 名:OnSize
// 功能描述:动态控制控件大小, 使控件占满整个显示区域
// 输入参数:void
// 输出参数:void
// 创建日期:04-7-18
// 修改日期:04-7-21
// 作    者:陈欢
// 调用函数: non
// 被调函数: 显示区域大小变化时被调用
// 附加说明:重载父类函数
//===============================================================
void CLeftView::OnSize(UINT nType, int cx, int cy) 
{
//	CFormView::OnSize(nType, cx, cy);
	
	// TODO: Add your message handler code here
	this->ShowWindow(SW_MAXIMIZE);
	
	// 列表列表控件必须是第一个控件
	//CWnd *pchild = this->GetWindow(GW_CHILD); //Identifies the CWnd first child window

	CWnd *pchild = GetDlgItem( IDC_TREE_CLIENTS );
	if (pchild != NULL)
	{
		CRect rect;
		this->GetClientRect(&rect);
		pchild->ScreenToClient(&rect);
		pchild->SetWindowPos(NULL, 0, 30, rect.Width(), rect.Height()-30,
							SWP_NOZORDER | SWP_NOACTIVATE);
	}
}

//===============================================================
// 函 数 名:OnButtonHisList
// 功能描述:控制按钮显示效果
// 输入参数:void
// 输出参数:void
// 创建日期:04-7-18
// 修改日期:04-7-20
// 作    者:陈欢
// 调用函数: InitListTree
// 被调函数: OnInitialUpdate
// 修改变量: GetDocument()->m_nCurView
// 附加说明:单击按钮 "History List" 时的响应函数, 初始化时被调用一次
//===============================================================
void CLeftView::OnButtonHisList() 
{
	m_butDiag.SetState(false);
	m_butList.SetState(true);

	m_butDiag.EnableWindow( true );
	m_butList.EnableWindow( false );
	
	InitListTree();

	GetDocument()->m_nCurView = 0;
	GetDocument()->UpdateAllViews( this );
}

//===============================================================
// 函 数 名:OOnButtonHisDiag
// 功能描述:控制按钮显示效果
// 输入参数:void
// 输出参数:void
// 创建日期:04-7-18
// 修改日期:04-7-20
// 作    者:陈欢
// 调用函数: InitDiagTree
// 被调函数: non
// 修改变量: GetDocument()->m_nCurView
//===============================================================
void CLeftView::OnButtonHisDiag() 
{
	m_butDiag.SetState(true);
	m_butList.SetState(false);

	m_butDiag.EnableWindow( false );
	m_butList.EnableWindow( true );

	InitDiagTree();

	GetDocument()->m_nCurView = 1;
	GetDocument()->UpdateAllViews( this );
}

void CLeftView::OnInitialUpdate() 
{
	CFormView::OnInitialUpdate();
	
	// TODO: Add your specialized code here and/or call the base class
	OnButtonHisList();

	m_treeClients.SetBkColor( RGB(255, 200, 0) );

	// 每间隔 1 秒, 更新主机列表;
	SetTimer( 1, 1000, 0 );
}

//===============================================================
// 函 数 名:InitListTree
// 功能描述:单击 History List 时, 更新树控件的内容
// 创建日期:04-7-18
// 修改日期:04-7-20
// 作    者:陈欢
// 调用函数: non
// 被调函数: OnButtonHisList
// 修改变量: m_hPreItem, GetDocument()->m_bClientEmpty
// 附加说明: 主机列表来自数据库中的数据
//===============================================================
void CLeftView::InitListTree()
{
	//清空树形控件中的原有内容
	if ( m_treeClients.GetCount() != 0 )
		m_treeClients.DeleteAllItems();

	//创建根项目
	TV_INSERTSTRUCT curItem;

	curItem.hParent = TVI_ROOT;			//the item is inserted at the root of the tree view control.		
	curItem.hInsertAfter = TVI_LAST;	//Inserts the item into the list in alphabetical order.
	curItem.item.mask = TVIF_TEXT;

	char root[] = "All Clients";
	curItem.item.pszText = root;
	m_hPreItem = m_treeClients.InsertItem( &curItem );

	curItem.hParent = m_hPreItem;	

	extern CRITICAL_SECTION g_criClientAccess;
	extern CSetClient	gm_setClient;
	EnterCriticalSection( &g_criClientAccess );
try
{
	if ( !gm_setClient.IsOpen() )
		gm_setClient.Open();
	gm_setClient.Requery();

	// 插入主机列表
	while ( !gm_setClient.IsEOF() )
	{
		curItem.item.pszText = gm_setClient.m_ip.GetBuffer( gm_setClient.m_ip.GetLength() );
		m_treeClients.InsertItem( &curItem );
		gm_setClient.MoveNext();
	}
	gm_setClient.Close();
} catch(...) {}

	LeaveCriticalSection( &g_criClientAccess );

	m_treeClients.Expand( m_hPreItem, TVE_EXPAND );	//展开所有目录, 实际上就两级目录
	GetDocument()->m_bClientEmpty = m_treeClients.GetCount() <= 1;
	GetDocument()->UpdateAllViews(this);
}

//===============================================================
// 函 数 名:InitDiagTree
// 功能描述:单击 History Diagram 时, 更新树控件的内容
// 创建日期:04-7-18
// 修改日期:04-7-20
// 作    者:陈欢
// 调用函数: non
// 被调函数: OnButtonHisDiag
// 修改变量: m_hPreItem, GetDocument()->m_bClientEmpty
// 附加说明: 主机列表来自全局变量 theActiveClientList
//===============================================================
void CLeftView::InitDiagTree()
{
	//清空树形控件中的原有内容
	if ( m_treeClients.GetCount() != 0 )
		m_treeClients.DeleteAllItems();

	//创建根项目
	TV_INSERTSTRUCT curItem;

	curItem.hParent = TVI_ROOT;			//the item is inserted at the root of the tree view control.		
	curItem.hInsertAfter = TVI_LAST;	//Inserts the item into the list in alphabetical order.
	curItem.item.mask = TVIF_TEXT;

	CString sRoot = "Active Clients";
	curItem.item.pszText = sRoot.GetBuffer(0);
	m_hPreItem = m_treeClients.InsertItem( &curItem );

	curItem.hParent = m_hPreItem;

	extern CRITICAL_SECTION g_criActiveClientList;
	extern ActivClient theActiveClientList;
	EnterCriticalSection( &g_criActiveClientList );
	int cout = theActiveClientList.GetCount();
	int i = 0;
	while ( i < cout )
	{
		curItem.item.pszText = theActiveClientList.m_caList[i].GetBuffer(0);
		m_treeClients.InsertItem( &curItem );
		i++;
	}
	LeaveCriticalSection( &g_criActiveClientList );

	m_treeClients.Expand( m_hPreItem, TVE_EXPAND );	//展开所有目录, 实际上就两级目录
}

//===============================================================
// 函 数 名:OnTimer
// 功能描述:更新主机列表, 修改标志量
// 创建日期:04-7-18
// 修改日期:04-7-20
// 作    者:陈欢
// 调用函数: non
// 被调函数: OnButtonHisDiag
// 修改变量: 全局 bNewAccess
// 附加说明: 每一秒被调用一次
//===============================================================
void CLeftView::OnTimer(UINT nIDEvent)
{
	// 更新 History List 主机列表
	// 读取当前列表中的主机个数, 与数据库中的主机个数进行比较
	int listNum = m_treeClients.GetCount() - 1;	// 当前列表中的主机个数,  
	int DBNum = 0;		// 数据库中的主机个数
	extern CRITICAL_SECTION g_criClientAccess;
	extern CSetClient	gm_setClient;
	EnterCriticalSection( &g_criClientAccess );
	if ( !gm_setClient.IsOpen() )
		gm_setClient.Open();
	gm_setClient.Requery();
	while ( !gm_setClient.IsEOF() )
	{
		DBNum++;
		gm_setClient.MoveNext();
	}
	gm_setClient.Close();
	LeaveCriticalSection( &g_criClientAccess );

	// 两个数量不同, 且当前显示的是 List, 更新
	if ( (listNum != DBNum) && !m_butList.IsWindowEnabled() )
		InitListTree();

	// 如果有新主机加入, 更新 History Diagram 主机列表
	extern CRITICAL_SECTION g_criNewAccess;
	extern bool bNewAccess;
	EnterCriticalSection( &g_criNewAccess );
	if ( bNewAccess && !m_butDiag.IsWindowEnabled() )
	{
		InitDiagTree();
		bNewAccess = false;
	}
	LeaveCriticalSection( &g_criNewAccess );

	CFormView::OnTimer(nIDEvent);
}

//===============================================================
// 函 数 名:OnDblclkTreeClients
// 功能描述:响应双击主机列表事件
// 创建日期:04-7-18
// 修改日期:04-7-20
// 作    者:陈欢
// 调用函数: non
// 被调函数: OnButtonHisDiag
// 修改变量: m_hPreItem
//===============================================================
void CLeftView::OnDblclkTreeClients(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
//	NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;

	// 重复双击同一主机IP, 不处理
	HTREEITEM hItem = m_treeClients.GetSelectedItem();
	if ( hItem == m_hPreItem )
		return;
	m_hPreItem = hItem;

	// 若当前显示 history list
	if ( !m_butList.IsWindowEnabled() )
	{
		// 双击根目录时特别处理
		if ( hItem == m_treeClients.GetRootItem() )
			GetDocument()->m_strCurClient = "";
		else
			GetDocument()->m_strCurClient = m_treeClients.GetItemText( hItem );
	}
	else
	{
		// 双击根目录时特别处理
		if ( hItem == m_treeClients.GetRootItem() )
			GetDocument()->m_strCurDiagramClient = "";
		else
			GetDocument()->m_strCurDiagramClient = m_treeClients.GetItemText( hItem );
	}
	GetDocument()->UpdateAllViews( this );

	*pResult = 0;
}

⌨️ 快捷键说明

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