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

📄 ntsharesdemodlg.cpp

📁 此源码为设置本地文件夹为局域网内的共享文件夹
💻 CPP
字号:
#include "stdafx.h"
#include "NTSharesDemo.h"
#include "NTSharesDemoDlg.h"
#include "NTShareInfoDlg.h"

#include "LMShare.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CSharesDemoDlg dialog

CSharesDemoDlg::CSharesDemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSharesDemoDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CSharesDemoDlg::DoDataExchange(CDataExchange* pDX)
{
  CDialog::DoDataExchange(pDX);
  DDX_Control(pDX, IDC_LIST, m_lstList);
  DDX_Control(pDX, IDC_BTN_DELETE, m_btnDelete);
  DDX_Control(pDX, IDC_BTN_EDIT, m_btnEdit);
}

BEGIN_MESSAGE_MAP(CSharesDemoDlg, CDialog)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
  ON_BN_CLICKED(IDC_GETSHARES, OnBnClickedGetshares)
  ON_BN_CLICKED(IDC_BTN_DELETE, OnBnClickedDelete)
  ON_BN_CLICKED(IDC_BTN_ADD, OnBnClickedAdd)
  ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST, OnLvnItemchangedList)
  ON_BN_CLICKED(IDC_BTN_EDIT, OnBnClickedBtnEdit)
END_MESSAGE_MAP()

// CSharesDemoDlg message handlers

BOOL CSharesDemoDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here
  m_lstList.InsertColumn(0,_T("Name"),LVCFMT_LEFT,100);
  m_lstList.InsertColumn(1,_T("Path"),LVCFMT_LEFT,100);
  m_lstList.InsertColumn(2,_T("Comment"),LVCFMT_LEFT,100);
  m_lstList.InsertColumn(3,_T("Type"),LVCFMT_LEFT,100);
  m_lstList.InsertColumn(4,_T("Max"),LVCFMT_LEFT,100);
  m_lstList.InsertColumn(5,_T("Current"),LVCFMT_LEFT,100);
  m_lstList.InsertColumn(6,_T("Admin"),LVCFMT_LEFT,100);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CSharesDemoDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CSharesDemoDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CSharesDemoDlg::OnBnClickedGetshares()
{
  CString sServer;
  GetDlgItemText(IDC_SERVER,sServer);
  m_lstList.DeleteAllItems();
  TRY
  {
    m_Shares.First(sServer);
    while (m_Shares.Next())
    {
      AddToList();
    };
  }
  CATCH(CWin32Exception,e)
  {
    e->ReportError();
    return;
  }
  END_CATCH
}
    
void CSharesDemoDlg::AddToList()
{
  int index = m_lstList.InsertItem(0,m_Shares.m_sName);
  m_lstList.SetItemText(index,1,m_Shares.m_sPath);
  m_lstList.SetItemText(index,2,m_Shares.m_sRemark);
  switch (m_Shares.m_dwShareType)
  {                                                           
    case STYPE_DISKTREE :
      m_lstList.SetItemText(index,3,_T("Disk"));
      break;
    case STYPE_PRINTQ :
      m_lstList.SetItemText(index,3,_T("PrintQ"));
      break;
    case STYPE_DEVICE :
      m_lstList.SetItemText(index,3,_T("Device"));
      break;
    case STYPE_IPC :
      m_lstList.SetItemText(index,3,_T("IPC"));
      break;
  }
  int conn = m_Shares.m_nMaxConnections;
  CString sStr;
  if (conn == -1)
    sStr = _T("Unlimited");
  else
    sStr.Format(_T("%d"),conn);
  m_lstList.SetItemText(index,4,sStr);
  sStr.Format(_T("%d"),m_Shares.m_dwCurrentConnections);
  m_lstList.SetItemText(index,5,sStr);
  if (m_Shares.m_fIsAdmin)
    m_lstList.SetItemText(index,6,_T("Yes"));
  else
    m_lstList.SetItemText(index,6,_T("No"));
  m_lstList.SetItemData(index,(DWORD_PTR)m_Shares.m_fIsAdmin);
}

void CSharesDemoDlg::OnBnClickedDelete()
{
  CString sServer;
  CString sName;

  GetDlgItemText(IDC_SERVER,sServer);
  
  // Get index in share list
  int nItem = m_lstList.GetSelectionMark();
  BOOL fIsAdmin = (BOOL)m_lstList.GetItemData(nItem);
  if (fIsAdmin)
  {
    AfxMessageBox(_T("Not allowed to delete administrative shares"),MB_OK | MB_ICONERROR,0);
    return;
  }
  sName = m_lstList.GetItemText(nItem,0);
  TRY
  {
    m_Shares.Delete(sServer,sName);
  }
  CATCH(CWin32Exception,e)
  {
    e->ReportError();
    return;
  }
  END_CATCH

  m_lstList.DeleteItem(nItem);
}

void CSharesDemoDlg::OnBnClickedAdd()
{
  CShareInfoDlg dlg(this);
  CString sServer;
  GetDlgItemText(IDC_SERVER,sServer);

  if (dlg.DoModal() == IDOK)
  {
    int nMaxConnections;
    if (dlg.m_fUnlimited)
      nMaxConnections = -1;
    else
      nMaxConnections = dlg.m_nMaxConnections;
    
    TRY
    {
      m_Shares.Add(sServer,dlg.m_sName,dlg.m_sRemark,dlg.m_sPath,nMaxConnections);
    }
    CATCH(CWin32Exception,e)
    {
      e->ReportError();
      return;
    }
    END_CATCH
  
    AddToList();
  }
}

void CSharesDemoDlg::OnLvnItemchangedList(NMHDR *pNMHDR, LRESULT *pResult)
{
  m_btnDelete.EnableWindow(m_lstList.GetSelectionMark() != -1);
  m_btnEdit.EnableWindow(m_lstList.GetSelectionMark() != -1);
  *pResult = 0;
}

void CSharesDemoDlg::OnBnClickedBtnEdit()
{
  CShareInfoDlg dlg(this);
  CString sServer;
  int nMaxConnections;
  
  GetDlgItemText(IDC_SERVER,sServer);
  
  // Get selected index
  int nItem = m_lstList.GetSelectionMark();

  // Set share info
  dlg.m_sName = m_lstList.GetItemText(nItem,0);
  dlg.m_sPath = m_lstList.GetItemText(nItem,1);
  dlg.m_sRemark = m_lstList.GetItemText(nItem,2);
  CString sText;
  sText = m_lstList.GetItemText(nItem,4);
  if (sText[0] == _T('U')) nMaxConnections = -1;
  else nMaxConnections = _tstoi(sText);
  dlg.m_nMaxConnections = nMaxConnections;
  if (dlg.DoModal() == IDOK)
  {
    m_lstList.SetItemText(nItem,1,dlg.m_sPath);
    m_lstList.SetItemText(nItem,2,dlg.m_sRemark);
    nMaxConnections = dlg.m_fUnlimited ? -1 : dlg.m_nMaxConnections;
    if (nMaxConnections == -1)
      sText = _T("Unlimited");
    else
      sText.Format(_T("%d"),nMaxConnections);
    m_lstList.SetItemText(nItem,4,sText);
    TRY
    {
      m_Shares.Update(sServer,dlg.m_sName,dlg.m_sRemark,dlg.m_sPath,nMaxConnections);
    }
    CATCH(CWin32Exception,e)
    {
      e->ReportError();
      return;
    }
    END_CATCH
  }
}

⌨️ 快捷键说明

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