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

📄 netinfodadlg.cpp

📁 VisualC++通信编程工程实例精解 Chapter 2 Example 1 MSCOMM控件编程实例 Example 2 基于Windows API的虚拟终端实现 Example 3
💻 CPP
字号:
// NetInfoDaDlg.cpp : implementation file
//

#include "stdafx.h"
#include "OpcClientSpy.h"
#include "NetInfoDaDlg.h"
#include <winnetwk.h>

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

/////////////////////////////////////////////////////////////////////////////
// CNetInfoDaDlg property page

IMPLEMENT_DYNCREATE(CNetInfoDaDlg, CPropertyPage)

CNetInfoDaDlg::CNetInfoDaDlg() : CPropertyPage(CNetInfoDaDlg::IDD)
{
	//{{AFX_DATA_INIT(CNetInfoDaDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_pImageList = NULL;
	m_hGroupAry.SetSize(0);
}

CNetInfoDaDlg::~CNetInfoDaDlg()
{
	if( m_pImageList )
		delete m_pImageList;
}

void CNetInfoDaDlg::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CNetInfoDaDlg)
	DDX_Control(pDX, IDC_TREE1, m_tree);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CNetInfoDaDlg, CPropertyPage)
	//{{AFX_MSG_MAP(CNetInfoDaDlg)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNetInfoDaDlg message handlers

BOOL CNetInfoDaDlg::OnInitDialog() 
{
	CPropertyPage::OnInitDialog();
	
	//装入图标
	m_pImageList = new CImageList();
	m_pImageList->Create(16, 16, ILC_COLOR, 7, 1);
	CBitmap bitmap;
	bitmap.LoadBitmap(IDB_BITMAP_SERVER_ICON);
	m_pImageList->Add(&bitmap, (COLORREF)0x000000);
	bitmap.DeleteObject();
	m_tree.SetImageList(m_pImageList,LVSIL_NORMAL);
	//搜索所有计算机
	CWaitCursor wait;
	EnumerateNodes(NULL);
	for( int i=0;i<m_hGroupAry.GetSize();i++ )
		m_tree.Expand(m_hGroupAry[i],TVE_EXPAND);
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

//enum net resource
BOOL CNetInfoDaDlg::EnumerateNodes(LPNETRESOURCE lpnr)
{
   HANDLE hEnum = 0;
   DWORD dwResult = WNetOpenEnum(RESOURCE_GLOBALNET,
                     RESOURCETYPE_ANY,
                     RESOURCEUSAGE_CONTAINER,
                     lpnr,              // NULL first time this function is called
                     &hEnum);           // handle to resource

   if (dwResult != NO_ERROR)
      return FALSE;

   DWORD cbBuffer = 16384;      // 16K buffer
   LPNETRESOURCE lpnrLocal = (LPNETRESOURCE) GlobalAlloc(GPTR, cbBuffer);
   do
   {
      DWORD cEntries = 0xFFFFFFFF; // enumerate all possible entries
      dwResult = WNetEnumResource(hEnum,
                                  &cEntries,
                                  lpnrLocal,
                                  &cbBuffer);

      if (dwResult == NO_ERROR)
      {
         for( DWORD i = 0; i < cEntries; i++)
         {
            // If this NETRESOURCE is a container, call the function
            // recursively.
            if(RESOURCEUSAGE_CONTAINER ==
               (lpnrLocal[i].dwUsage & RESOURCEUSAGE_CONTAINER))
            {
               if(RESOURCEDISPLAYTYPE_SERVER == lpnrLocal[i].dwDisplayType )
               {
                  CString node(lpnrLocal[i].lpRemoteName);
                  int index = node.Find( _T("\\") );
                  if( index > -1 )
                     node = node.Mid(index+2);
				  //加入对应的组中
				  int total = m_hGroupAry.GetSize();
                  AddItemToTree(m_hGroupAry[total-1],3,node);
               }
               else//不是Server
               {
				  if( lpnr!=NULL )//lpfn=NULL说明是第一层"Microsoft 网络"
				  {
					  HTREEITEM hRoot = 
					      AddItemToTree(NULL,2,lpnrLocal[i].lpRemoteName);
					  m_hGroupAry.Add(hRoot);
				  }
                  EnumerateNodes(&lpnrLocal[i]);
                  //break;   // ONLY enumerate the first "Container"
               }
            }
         }//end of for...
      }//end of if (dwResult
      else if (dwResult != ERROR_NO_MORE_ITEMS)
      {
         break;
      }
   }
   while(dwResult != ERROR_NO_MORE_ITEMS);

   GlobalFree((HGLOBAL) lpnrLocal);
   dwResult = WNetCloseEnum(hEnum);
   if (dwResult != NO_ERROR)
      return FALSE;
   return TRUE;
}

//添加一个项到树中。
HTREEITEM CNetInfoDaDlg::AddItemToTree(HTREEITEM hItem,int imageIndex,CString strText)
{
	TVINSERTSTRUCT  curTreeItem;
	curTreeItem.hParent = hItem;
	curTreeItem.hInsertAfter = TVI_LAST;
	curTreeItem.item.iImage = imageIndex;
	curTreeItem.item.iSelectedImage = curTreeItem.item.iImage ;
	curTreeItem.item.pszText = strText.GetBuffer(strText.GetLength());
	curTreeItem.item.mask =	 TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_TEXT| TVIF_PARAM;
	curTreeItem.item.lParam = 0;
	HTREEITEM hRtn = m_tree.InsertItem(&curTreeItem);
	return hRtn;
}

⌨️ 快捷键说明

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