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

📄 cfileclientdlg.cpp

📁 一个关于下载文件客户程序的小程序
💻 CPP
字号:
// CFileClientDlg.cpp : implementation file
//

#include "stdafx.h"
#include "CFileClient.h"
#include "CFileClientDlg.h"
#include "ClientThread.h"
#include "Client.h"
#include "Global.h"
#include "ProgressDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCFileClientDlg dialog

CCFileClientDlg::CCFileClientDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCFileClientDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCFileClientDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_pClientThread = NULL;
}

void CCFileClientDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCFileClientDlg)
	DDX_Control(pDX, IDC_LIST_FILES, m_ctrlFileList);
	DDX_Control(pDX, IDC_SERVERIP, m_ctrlServerIP);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCFileClientDlg, CDialog)
	//{{AFX_MSG_MAP(CCFileClientDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_CONNECT, OnConnect)
	ON_BN_CLICKED(IDC_BROWSE, OnBrowse)
	ON_BN_CLICKED(IDC_DOWNLOAD, OnDownload)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_CLIENT_REP, OnClientReport)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCFileClientDlg message handlers

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

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 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_ctrlServerIP.SetAddress(127, 0, 0, 1);

	m_ctrlFileList.SetExtendedStyle(m_ctrlFileList.GetExtendedStyle()
		| LVS_EX_FULLROWSELECT | LVS_EX_FLATSB);
	m_ctrlFileList.InsertColumn(0, _T("文件名称"), LVCFMT_LEFT, 115);
	m_ctrlFileList.InsertColumn(1, _T("大  小"), LVCFMT_CENTER, 70);
	m_ctrlFileList.InsertColumn(2, _T("日  期"), LVCFMT_CENTER, 130);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CCFileClientDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 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 CCFileClientDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (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 to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CCFileClientDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CCFileClientDlg::OnBrowse() 
{
	ASSERT(m_pClientThread);
	m_pClientThread->PostThreadMessage(WM_CLIENT_CMD, CMD_BROWSE, 0);
}

void CCFileClientDlg::OnDownload() 
{
	// TODO: Add your control notification handler code here
	if(m_ctrlFileList.GetSelectedCount() <= 0)
	{
		if(m_ctrlFileList.GetItemCount() <= 0)
			AfxMessageBox(_T("无文件可供下载!"));
		else
			AfxMessageBox(_T("请先选择需要下载的文件!"));
		return;
	}
	
	ASSERT(m_pClientThread);
	CClient* pClient = ((CClientThread*)m_pClientThread)->m_pClient;
	ASSERT(pClient);
	pClient->m_listExpectedFiles.RemoveAll();

	POSITION pos = m_ctrlFileList.GetFirstSelectedItemPosition();
	while(pos)
	{
		int nItem = m_ctrlFileList.GetNextSelectedItem(pos);
		pClient->m_listExpectedFiles.AddTail(m_ctrlFileList.GetItemText(nItem, 0));
	}

	BROWSEINFOA bi = {0}; 	
	bi.hwndOwner = m_hWnd;	
	bi.pszDisplayName = m_strDestDir.GetBuffer(MAX_PATH);	
	bi.ulFlags	 = BIF_RETURNONLYFSDIRS|BIF_STATUSTEXT;	
	bi.lpszTitle = _T("请选择存盘路径:");
	bi.lpfn		 = NULL ;

	LPITEMIDLIST lpIL = SHBrowseForFolder(&bi);
	if(lpIL)		
	{		
		SHGetPathFromIDList(lpIL, bi.pszDisplayName);		
	}
	m_strDestDir.ReleaseBuffer();
	if(m_strDestDir.Right(1) != "\\")
		m_strDestDir += "\\";

	m_pClientThread->PostThreadMessage(WM_CLIENT_CMD, CMD_DOWNLOAD, 0);

	CProgressDlg dlg;
	dlg.m_pOwner = this;
	if(dlg.DoModal() == IDCANCEL)
		m_pClientThread->PostThreadMessage(WM_CLIENT_CMD, CMD_CANCELDOWNLOAD, 0);
}

void CCFileClientDlg::OnConnect() 
{
	// TODO: Add your control notification handler code here

	CClientThread*& pClientThread = (CClientThread*&)m_pClientThread;

	if(pClientThread == NULL)
	{
		if((pClientThread = (CClientThread*)AfxBeginThread(RUNTIME_CLASS(CClientThread))) == NULL)
			return;
		else
			pClientThread->m_pParentWnd = this;
	}
	ASSERT(pClientThread != NULL);

	GetDlgItem(IDC_CONNECT)->EnableWindow(FALSE);

	if(pClientThread->m_pClient == NULL || pClientThread->m_pClient->m_hSocket == INVALID_SOCKET)
	{//连接服务器
		DWORD dwServerIP;
		CString strServerIP;
		m_ctrlServerIP.GetAddress(dwServerIP);
		strServerIP.Format("%d.%d.%d.%d", HIBYTE(HIWORD(dwServerIP)),
			LOBYTE(HIWORD(dwServerIP)), HIBYTE(LOWORD(dwServerIP)), LOBYTE(LOWORD(dwServerIP)));
		
		pClientThread->m_strServerIP = strServerIP;
		pClientThread->PostThreadMessage(WM_CLIENT_CMD, CMD_CONNECT, 0);
	}
	else//断开服务器
	{
		pClientThread->PostThreadMessage(WM_CLIENT_CMD, CMD_DISCONNECT, 0);
	}
}

LRESULT CCFileClientDlg::OnClientReport(WPARAM wParam, LPARAM lParam)
{
	switch(wParam)
	{
	case CMD_CONNECT:
		GetDlgItem(IDC_CONNECT)->EnableWindow(TRUE);
		if((BOOL)lParam == TRUE)
		{
			GetDlgItem(IDC_BROWSE)->EnableWindow(TRUE);
			GetDlgItem(IDC_DOWNLOAD)->EnableWindow(TRUE);
			GetDlgItem(IDC_CONNECT)->SetWindowText(_T("断    开"));
			m_ctrlFileList.DeleteAllItems();
		}
		else
			AfxMessageBox(_T("连接下载服务器失败!"));
		break;

	case CMD_DISCONNECT:
		GetDlgItem(IDC_CONNECT)->EnableWindow(TRUE);
		GetDlgItem(IDC_BROWSE)->EnableWindow(FALSE);
		GetDlgItem(IDC_DOWNLOAD)->EnableWindow(FALSE);
		GetDlgItem(IDC_CONNECT)->SetWindowText(_T("连    接"));
		break;
	
	case CMD_BROWSE:
		if(lParam == (LPARAM)RV_CLEAN)
		{
			m_ctrlFileList.DeleteAllItems();
		}
		else if(lParam == (LPARAM)RV_ADD)
		{
			CClientThread* pThread = (CClientThread*)m_pClientThread;
			if(pThread != NULL && pThread->m_pClient != NULL)
			{
				ASSERT(!pThread->m_pClient->m_listAvailableFiles.IsEmpty());
				CClient::FILE_INFO* pInfo = (CClient::FILE_INFO*)pThread->m_pClient->m_listAvailableFiles.GetTail();
				if(pInfo)
				{
					int nCount = m_ctrlFileList.GetItemCount();

					CString strFileSize = _T("KB"), strTemp;
					DWORD dwFileSize, dwTemp;
					dwFileSize = pInfo->dwFileSize;
					while(dwFileSize >= 1000)
					{
						dwTemp = dwFileSize % (DWORD)1000;
						strTemp.Format(",%lu", dwTemp);
						strFileSize = strTemp + strFileSize;
						dwFileSize = dwFileSize / (DWORD)1000;
					}
					strTemp.Format("%lu", dwFileSize);
					strFileSize = strTemp + strFileSize;

					m_ctrlFileList.InsertItem(nCount, pInfo->strFileName);
					m_ctrlFileList.SetItemText(nCount, 1, strFileSize);
					m_ctrlFileList.SetItemText(nCount, 2, pInfo->tmFileCreated.Format("%Y-%m-%d %H:%M:%S"));
				}
			}
		}
		break;

	default:
		break;
	}

	return 0L;
}

void CCFileClientDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	if(m_pClientThread)
	{
		m_pClientThread->PostThreadMessage(WM_CLIENT_CMD, CMD_TERMINATE, 0);
		if(::WaitForSingleObject(m_pClientThread->m_hThread, 2000) != WAIT_OBJECT_0)
			::TerminateThread(m_pClientThread->m_hThread, 0);
		m_pClientThread = NULL;
	}
}

⌨️ 快捷键说明

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