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

📄 machineview.cpp

📁 vc++网络程序设计实例详解 人民邮电出版社1-2章源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// MachineView.cpp : implementation of the CMachineView class
//

#include "stdafx.h"
#include "RemoteAdmin.h"
#include "GlobalMFCHelperFunc.h"
#include "GlobalHelperFunc.h"
#include "RemoteAdminDoc.h"
#include "MachineView.h"
#include "MainFrame.h"
#include "RemoteLogonDlg.h"
#include "MachineInfo.h"
#include "RemoteAdminView.h"
#include "RemoteProcessDlg.h"
#include "ProgressWndThread.h"
#include "Command.h"
#include "ConnectionThread.h"
#include <process.h>
#include <Tlhelp32.h>
#include <windows.h>

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


/////////////////////////////////////////////////////////////////////////////
// CMachineView

IMPLEMENT_DYNCREATE(CMachineView, CTreeView)

BEGIN_MESSAGE_MAP(CMachineView, CTreeView)
	//{{AFX_MSG_MAP(CMachineView)
	ON_WM_RBUTTONDOWN()
	ON_COMMAND(ID_MACHINEPROPERTIES_REFRESHPROCESS, OnRefreshProcess)
	ON_WM_LBUTTONDOWN()
	ON_COMMAND(ID_MACHINEPROPERTIES_EXECUTE_PROCESS, OnExecuteProcess)
	ON_COMMAND(ID_MACHINEPROPERTIES_SHUTDOWN_HALT, OnShutdownHalt)
    ON_COMMAND(ID_MACHINEOPTIONS_ADD_MACHINE, OnAddMachine)
	ON_COMMAND(ID_MACHINEOPTIONS_DELETE_MACHINE, OnDeleteMachine)
	ON_COMMAND(ID_MACHINEPROPERTIES_DELETE_MACHINE, OnDeleteMachine)
	ON_COMMAND(ID_MACHINEPROPERTIES_SHUTDOWN_ABORTSHUTDOWN, OnShutdownAbort)
    ON_COMMAND(ID_MACHINEPROPERTIES_SHUTDOWN_REBOOT, OnShutdownReboot)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMachineView construction/destruction

CMachineView::CMachineView()
{
	// TODO: add construction code here

}

CMachineView::~CMachineView()
{
}

BOOL CMachineView::PreCreateWindow(CREATESTRUCT& cs)
{
	cs.style |= TVS_HASLINES | 
                TVS_LINESATROOT | 
                TVS_HASBUTTONS | 
                TVS_SHOWSELALWAYS;

	return CTreeView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMachineView drawing

void CMachineView::OnDraw(CDC* pDC)
{
	CRemoteAdminDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	// TODO: add draw code for native data here
}


void CMachineView::OnInitialUpdate()
{
	CTreeView::OnInitialUpdate();
    
    // When being reused, reset the image list
    ImageList_Destroy(m_ilMachine.Detach());
    // When being reused, clean the tree control(view)
    GetTreeCtrl().DeleteAllItems();

	CreateEmptyTree();
    GetTreeCtrl().SetIndent(5);
}

/////////////////////////////////////////////////////////////////////////////
// CMachineView diagnostics

#ifdef _DEBUG
void CMachineView::AssertValid() const
{
	CTreeView::AssertValid();
}

void CMachineView::Dump(CDumpContext& dc) const
{
	CTreeView::Dump(dc);
}

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

void CMachineView::CreateEmptyTree()
{
    CreateImageList();
    CreateTreeRoot();
}

void CMachineView::CreateImageList()
{
    // Load various icons required for the machine view
    HICON hIconMachineOnNetwork = ::AfxGetApp()->LoadIcon(MAKEINTRESOURCE(IDI_MACHINE_ON_NETWORK));
    HICON hIconMachine  = ::AfxGetApp()->LoadIcon(MAKEINTRESOURCE(IDI_MACHINE));
        
    // The imageslist is created
    m_ilMachine.Create(20, 20, ILC_COLOR32 | ILC_MASK, 0, 1);

    // Add the icons in the imagelist
    m_ilMachine.Add(hIconMachineOnNetwork);
    m_ilMachine.Add(hIconMachine);

    // Add the imagelist to the tree control
    GetTreeCtrl().SetImageList(&m_ilMachine, TVSIL_NORMAL);
}

void CMachineView::CreateTreeRoot()
{
    CTreeCtrl& ptcVehicleView = GetTreeCtrl();
    TVINSERTSTRUCT tviHelper    = {0};
    CString str;
    
    //------------------------------------
    // Create the root of the vehicle view
    //------------------------------------
    tviHelper.hParent			  = TVI_ROOT;
	tviHelper.hInsertAfter		  = TVI_FIRST;
	tviHelper.item.mask			  = TVIF_TEXT |
                                    TVIF_IMAGE | 
                                    TVIF_SELECTEDIMAGE | 
                                    TVIF_PARAM;
    str.LoadString(IDS_MONITORED_MACHINES);
    tviHelper.item.pszText		  = str.GetBuffer(0);
	tviHelper.item.iImage		  = MACHINE_IMAGE_ON_NETWORK;
	tviHelper.item.iSelectedImage = MACHINE_IMAGE_ON_NETWORK;
	tviHelper.item.lParam		  = 0;
	
    m_hMachineViewRoot = ptcVehicleView.InsertItem(&tviHelper);
}

void CMachineView::OnRButtonDown(UINT nFlags, CPoint point) 
{
    CTreeView::OnLButtonDown(nFlags, point);
    
	HTREEITEM hTreeItem;
    CMenu menuContext;
    CPoint t_point;
    CRect t_Rect;
    TVITEM tvItem;
    TCHAR szText[_MAX_PATH] = _T("");
    tvItem.pszText = szText;

    t_point = point;
    SetFocus(); 
    
    hTreeItem = GetTreeCtrl().HitTest(t_point, &nFlags); // See if any item has been it?
    if(hTreeItem != NULL)
    {/*
        CString strIP = GetSelectedItemTextInTreeView();

        if (strIP != _T(""))
        {
            if (strIP == _T("0"))
            {
                ::GetRemoteAdminView()->GetListCtrl().DeleteAllItems();
            }
            else 
            {
                RefreshProcesses(strIP);
            }
        }*/
        // Check if the item clicked was not the root, it's associated LPARAM
        // value is zero.
        tvItem.hItem = hTreeItem;
        tvItem.mask = TVIF_PARAM | TVIF_HANDLE;

        GetTreeCtrl().GetItem(&tvItem);

        if (tvItem.lParam == 0) 
        {
            BOOL bResult = GetTreeCtrl().GetItemRect(hTreeItem, &t_Rect, TRUE);
            
            if(t_Rect.PtInRect(t_point))
            {
                GetTreeCtrl().SelectItem(hTreeItem);
                ClientToScreen(&t_point);
                MFC_DocView::GetRemoteAdminView()->GetListCtrl().DeleteAllItems();    
                menuContext.LoadMenu(IDR_MACHINE_OPTIONS);
                CMenu* pMenu = menuContext.GetSubMenu(0);
                pMenu->TrackPopupMenu(TPM_LEFTALIGN, t_point.x, t_point.y, this);
            }
        }
        else
        {   
            CString strIP = GetTreeCtrl().GetItemText(hTreeItem);
                       
            BOOL bResult = GetTreeCtrl().GetItemRect(hTreeItem, &t_Rect, TRUE);
            
            if(t_Rect.PtInRect(t_point))
            {
                GetTreeCtrl().SelectItem(hTreeItem);
                ClientToScreen(&t_point);
                
                // Refress the process when even right clicked
                RefreshProcesses(strIP);
                
                menuContext.LoadMenu(IDR_MACHINE_PROPERTIES);
                CMenu* pMenu = menuContext.GetSubMenu(0);
                pMenu->TrackPopupMenu(TPM_LEFTALIGN, t_point.x, t_point.y, this);
            }
        }
    }
    

}

void CMachineView::OnAddMachine()
{
    CRemoteLogonDlg dlgRemoteLogon;
    
    if (dlgRemoteLogon.DoModal() == IDOK)
    {
        BOOL bAddMachineSuccess = AddMachine(dlgRemoteLogon.m_strRemoteIP, dlgRemoteLogon.m_strPassword, GetDocument(), MFC_DocView::GetRemoteAdminView());   

		// Show visual progress, if successful
		if (bAddMachineSuccess)
		{
			MFC_DocView::GetAppDocument()->CreateVisualThread();
		}
    }
}


BOOL CMachineView::AddMachine(CString strIP, CString strPassword, CRemoteAdminDoc* pRemoteAdminDoc, CRemoteAdminView* pRemoteAdminView)
{
	if (GetDocument()->CheckIfMachinePresent(strIP) == FALSE)
    {
		SConnectInfo* pConnectInfo     = new SConnectInfo;
        pConnectInfo->pDoc             = pRemoteAdminDoc;
        pConnectInfo->pMachineView     = this;
        pConnectInfo->pRemoteAdminView = pRemoteAdminView;
        pConnectInfo->strIP            = strIP;
        pConnectInfo->strPwd           = strPassword;
            
        unsigned threadID = 0;
            
        // Check if a thread is not processing a machine
        if (pRemoteAdminDoc->IsConnectionPending(strIP) == FALSE)
        {
			::_beginthreadex(NULL, 0, ThreadConnection::ConnectToMachine, pConnectInfo, 0, &threadID);

			return TRUE;
        }
        else
        {
            CString strFormattedErrorMsg = ErrorHandling::ConvertStringTableIDToErrorMsg(strIP, IDS_CONNECTION_PENDING);
            ::AfxMessageBox(strFormattedErrorMsg);
        }
    }
    else
    {
        CString strFormattedErrorMsg = ErrorHandling::ConvertStringTableIDToErrorMsg(strIP, IDS_MACHINE_ALREADY_PRESENT);
        ::AfxMessageBox(strFormattedErrorMsg);
    }

	return FALSE;
        
}


void CMachineView::OnDeleteMachine()
{
	TCHAR szText[_MAX_PATH] = _T("");
    TVITEM tvItem = {0};
    HTREEITEM hMachineToBeDeleted = GetTreeCtrl().GetSelectedItem();

    /*tvItem.hItem   = hMachineToBeDeleted;
	tvItem.pszText = szText;
    tvItem.mask    = TVIF_PARAM | TVIF_HANDLE | TVIF_TEXT;
    GetTreeCtrl().GetItem(&tvItem);
	GetTreeCtrl().GetItemText(hMachineToBeDeleted);

    CMachineInfo* pMachineInfo = reinterpret_cast<CMachineInfo*>(tvItem.lParam);*/

	CString strIP = GetTreeCtrl().GetItemText(hMachineToBeDeleted);
	DeleteMachineFromBeingMonitored(strIP, MFC_DocView::GetRemoteAdminView());
}    
 

void CMachineView::DeleteMachineFromBeingMonitored(CString strIP, CRemoteAdminView* pRemoteAdminView)
{
    // Disconnects the machine and frees the machine info from the internal list
    GetDocument()->DeleteAndDisconnectMachine(strIP);

	// Get the HTREEITEM for the IP
	HTREEITEM hMachineToBeDeleted = GetTreeItemForText(strIP);

    // Remove from machine from the tree
    DeleteMachineFromTree(hMachineToBeDeleted);

    // Clear the processes in the list view
    //::GetRemoteAdminView()->GetListCtrl().DeleteAllItems();
	pRemoteAdminView->GetListCtrl().DeleteAllItems();

⌨️ 快捷键说明

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