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

📄 net_transfer_file_debugdlg.cpp

📁 刚学VC时写的文件传输程序,全部用SocketAPI编写
💻 CPP
字号:
// Net_Transfer_File_DebugDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DLL.h"
#include "Net_Transfer_File_Debug.h"
#include "Net_Transfer_File_DebugDlg.h"
#include "../Net_Transfer_File/TransferFile.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
}




HWND			dev_FormHandle;
CString			dev_Status = "";
RECT			dev_Rect;
CDC	*			dev_DC;
WORD			dev_Width = 400;
HBRUSH			dev_Brush;
PROGRESS		dev_Progress;
DWORD			dev_Current_Pos = 0;


DWORD WINAPI net_ShowProgress(PPROGRESS progress)
{
	memcpy(&dev_Progress, progress, sizeof(PROGRESS));
	SendMessage(dev_FormHandle, 1111, (DWORD)&dev_Progress, 0);
	return 0;
}




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

/////////////////////////////////////////////////////////////////////////////
// CNet_Transfer_File_DebugDlg dialog

CNet_Transfer_File_DebugDlg::CNet_Transfer_File_DebugDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CNet_Transfer_File_DebugDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CNet_Transfer_File_DebugDlg)
		// 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);
}

CNet_Transfer_File_DebugDlg::~CNet_Transfer_File_DebugDlg()
{
	net_Finalization();
}

void CNet_Transfer_File_DebugDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CNet_Transfer_File_DebugDlg)
	DDX_Control(pDX, IDC_LST_CLIENT, m_Client);
	DDX_Control(pDX, IDC_LST_SERVER, m_Server);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CNet_Transfer_File_DebugDlg, CDialog)
	//{{AFX_MSG_MAP(CNet_Transfer_File_DebugDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTN_INIT, OnBtnInit)
	ON_BN_CLICKED(IDC_BTN_SENDFILE, OnBtnSendFile)
	ON_MESSAGE(CM_SOCKETMESSAGE, net_Message_Event)
	ON_MESSAGE(1111, ShowProgress)
	ON_BN_CLICKED(IDC_BTN_ADD_FILE, OnBtnAddFile)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNet_Transfer_File_DebugDlg message handlers

BOOL CNet_Transfer_File_DebugDlg::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
	SetDlgItemText(IDC_ED_PORT, "2046");
	SetDlgItemText(IDC_ED_SERVER_PORT, "2046");
	SetDlgItemText(IDC_IP_SERVER, "127.0.0.1");
	dev_FormHandle = m_hWnd;
	dev_Rect.left = 20;
	dev_Rect.right = dev_Rect.left + dev_Width;
	dev_Rect.top = 200;
	dev_Rect.bottom =dev_Rect.top + 20;
	dev_Brush = (HBRUSH)RGB(255, 0, 255);
	dev_DC = GetDC();
	SetDlgItemText(IDC_ED_SERVER_DIRECTORY, "F:\\testdir\\");

	DWORD dwStyle = GetWindowLong(m_Server.GetSafeHwnd(), GWL_STYLE);
	dwStyle &= ~LVS_TYPEMASK;
	dwStyle |= LVS_REPORT;
	SetWindowLong(m_Server.GetSafeHwnd(), GWL_STYLE, dwStyle);
	m_Server.InsertColumn(0, "IP", LVCFMT_LEFT, 120);
	m_Server.InsertColumn(1, "Port", LVCFMT_LEFT, 64);


	dwStyle = GetWindowLong(m_Client.GetSafeHwnd(), GWL_STYLE);
	dwStyle &= ~LVS_TYPEMASK;
	dwStyle |= LVS_REPORT;
	SetWindowLong(m_Client.GetSafeHwnd(), GWL_STYLE, dwStyle);
	m_Client.InsertColumn(0, "File Name", LVCFMT_LEFT, 120);
	m_Client.InsertColumn(1, "Size", LVCFMT_LEFT, 90);
	m_Client.InsertColumn(2, "Progress", LVCFMT_LEFT, 90);
	m_Client.InsertColumn(3, "Percent", LVCFMT_LEFT, 90);
	m_Client.InsertColumn(4, "Speed", LVCFMT_LEFT, 90);
	m_Client.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);

	m_Client.SetItemCount(100);
	LV_ITEM lvi;
	lvi.mask = LVIF_TEXT | LVIF_PARAM;
	lvi.iItem = 0;
	lvi.iSubItem = 0;
	lvi.lParam = 0;//
	lvi.pszText = NULL;



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

void CNet_Transfer_File_DebugDlg::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 CNet_Transfer_File_DebugDlg::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 CNet_Transfer_File_DebugDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}



void CNet_Transfer_File_DebugDlg::OnBtnInit() 
{
	// TODO: Add your control notification handler code here
	CString sport, sdir;
	WORD port;
	GetDlgItemText(IDC_ED_PORT, sport);
	port = atoi(sport);
	GetDlgItemText(IDC_ED_SERVER_DIRECTORY, sport);
	PBYTE buf = new BYTE[sport.GetLength() + 1];
	memset(buf, 0, sport.GetLength() + 1);
	memcpy(buf, sport, sport.GetLength());
	DWORD ret = net_Initialization(buf, m_hWnd, port);
	sport.Format("Status: %u", ret);
	SetDlgItemText(IDC_LB_STATUS, sport);
	delete []buf;
}


void CNet_Transfer_File_DebugDlg::net_Message_Event(NETSOCKETMESSAGE msg)
{
	net_Event(&msg);
}

void CNet_Transfer_File_DebugDlg::ShowProgress(PPROGRESS progress)
{
	dev_Status.Format("%u", progress->totallen);
	m_Client.SetItemText(progress->index, 1, dev_Status);
	dev_Status.Format("%u", progress->position);
	m_Client.SetItemText(progress->index, 2, dev_Status);
	dev_Status.Format("%u", progress->per);
	m_Client.SetItemText(progress->index, 3, dev_Status + '%');
	if (progress->position >= progress->totallen)
	{
		m_Client.SetItemText(progress->index, 3, "100%");
		dev_Status = "Completed.";
	}
	else
	{
		DWORD time = GetTickCount() - dev_Current_Pos;
		time = progress->position / time;
		dev_Status.Format("%u kb/s", time);
	}
	m_Client.SetItemText(progress->index, 4, dev_Status);
}

void CNet_Transfer_File_DebugDlg::OnBtnSendFile() 
{
	// TODO: Add your control notification handler code here
	CString sip, sport, sfile;
	PCHAR cip, cfile;
	WORD iport;
	WORD i;
	GetDlgItemText(IDC_IP_SERVER, sip);
	GetDlgItemText(IDC_ED_SERVER_PORT, sport);
	cip = new CHAR[sip.GetLength() + 1];
	memset(cip, 0, sip.GetLength() + 1);
	memcpy(cip, sip, sip.GetLength());
	iport = atoi(sport);
	dev_Current_Pos = GetTickCount();
	for (i = 0; i < m_Client.GetItemCount(); i ++)
	{
		sfile = m_Client.GetItemText(i, 0);
		cfile = new CHAR[sfile.GetLength() + 1];
		memset(cfile, 0, sfile.GetLength() + 1);
		memcpy(cfile, sfile, sfile.GetLength());
		net_SendFile(cip, iport, cfile, &net_ShowProgress, i);
	}
}

void CNet_Transfer_File_DebugDlg::OnBtnAddFile() 
{
	// TODO: Add your control notification handler code here
	CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY|OFN_ALLOWMULTISELECT, "All Files(*.*)|*.*||", this);
	if(dlg.DoModal() == IDOK)
	{
		POSITION pos = dlg.GetStartPosition();

		while(pos != NULL)
		{
			CString strPathName = dlg.GetNextPathName(pos);
			CFile file;
			
			int index = m_Client.GetItemCount();
			LV_ITEM lvi;
			lvi.mask = LVIF_TEXT|LVIF_PARAM;
			lvi.iItem = index;
			lvi.iSubItem = 0;
			lvi.lParam = 0;
			lvi.pszText = NULL;

			m_Client.InsertItem(&lvi);
			m_Client.SetItemText(index, 0, strPathName);
			m_Client.SetItemText(index, 1, "0");
			m_Client.SetItemText(index, 2, "0");
			m_Client.SetItemText(index, 3, "0");
			m_Client.SetItemText(index, 4, "0 / s");
		}
	}
}

⌨️ 快捷键说明

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