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

📄 mainfrm.cpp

📁 大家好,好久没做什么东西了,前些日子偶然看到本论坛的一个FTP多线程断点续传的源代码
💻 CPP
字号:
// MainFrm.cpp : CMainFrame 类的实现
//

#include "stdafx.h"
#include "FTPTranser.h"


#include "MainFrm.h"
#include ".\mainfrm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#include "FTPClient.h"
#include "FTPTranserDoc.h"
#include "FileView.h"
#include "shlwapi.h"




// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	ON_WM_CREATE()
	ON_COMMAND(ID_32771, On32771)
	ON_MESSAGE(DBCLKITEM,OnDbclkItem)
	ON_MESSAGE(RECEIVEDATA,OnReceiveData)
	
	ON_WM_TIMER()
	ON_WM_DESTROY()
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // 状态行指示器
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

struct ThreadInfo
{
	CFTPClient * client;
	FileInfo * info;
	CString localFile;
};


DWORD WINAPI ThreadFunc(LPVOID p)
{
	ThreadInfo * info = (ThreadInfo *) p;
	info->client->DownLoad(*info->info,info->localFile);
	delete info;
	return 0;
}

// CMainFrame 构造/析构

CMainFrame::CMainFrame()
{
	WSADATA WsaData;	
	if(WSAStartup(MAKEWORD(2,2),&WsaData) == SOCKET_ERROR)
	{
		MessageBox("初始化SOCKET失败,程序将不能正常工作");
		return;
	}
	client = NULL;
	m_nTimer = NULL;
	hThread = NULL;
}

CMainFrame::~CMainFrame()
{
	if(client)
		delete client;
	WSACleanup();
	if(m_nTimer)
		KillTimer(m_nTimer);
}


int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("未能创建工具栏\n");
		return -1;      // 未能创建
	}

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("未能创建状态栏\n");
		return -1;      // 未能创建
	}

	// TODO: 如果不需要工具栏可停靠,则删除这三行
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);

	return 0;
}
void CMainFrame::AddMsg(CString msg)
{
	CFTPTranserDoc * doc = (CFTPTranserDoc*)GetActiveDocument();
	if(doc == NULL)
		return;
	doc->sec.Lock();

	if(doc->myList.GetSize() > MAXMSGLINE)
		doc->myList.RemoveHead();
	doc->myList.AddTail(msg);
	doc->sec.Unlock();	
	CView * view = (CView*)m_SplitterWnd.GetPane(1,1);

	doc->UpdateAllViews(view);
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: 在此处通过修改 CREATESTRUCT cs 来修改窗口类或
	// 样式

	return TRUE;
}


// CMainFrame 诊断

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

#endif //_DEBUG


// CMainFrame 消息处理程序

//点击下载连接那个菜单
void CMainFrame::On32771()
{	 
	if(client)
		delete client;
	client = new CFTPClient(this);
	if(!client->Login("192.168.0.46",21,"dhf","mushroom"))
	{
		AddMsg("登陆服务器失败\n");
		return;
	}
	client->List("");		
}

void CMainFrame::OnTimer(UINT nIDEvent)
{
	// TODO: Add your message handler code here and/or call default
	CFrameWnd::OnTimer(nIDEvent);
	CFTPTranserDoc* doc=(CFTPTranserDoc*)GetActiveDocument();
	if(doc->current < doc->count)
	{
		doc->current += 10;
		CView * view = (CView*)m_SplitterWnd.GetPane(0,0);
		doc->UpdateAllViews(view);
	}
	else
	{
		KillTimer(m_nTimer);
		m_nTimer = NULL;
	}

}


LRESULT CMainFrame::OnDbclkItem(WPARAM wParam,LPARAM lParam)
{
	FileInfo * info = (FileInfo *)lParam;
	if(info->isDir)
	{
		if(client->ChangeDirectory(info->mName))
			client->List("");
	}
	else
	{
		if(!client->busy)
		{
			CString filter = info->mType + " Files (*." + info->mType + ")|*." + info->mType + "|All Files (*.*)|*.*||";
			//char szFilters[]="MyType Files (*.my)|*.my|All Files (*.*)|*.*||";

			// Create an Open dialog; the default file name extension is ".my".
			CFileDialog fileDlg (FALSE, info->mType, info->mName,
				OFN_HIDEREADONLY, filter, this);
			   
			// Display the file dialog. When user clicks OK, fileDlg.DoModal() 
			// returns IDOK.
			if( fileDlg.DoModal ()==IDOK )
			{
				CString pathName = fileDlg.GetPathName();
				if(PathFileExists(pathName))
				{
					CString fileName = fileDlg.GetFileTitle ();				
					if(MessageBox("文件"+pathName+"已经存在,要覆盖吗?","覆盖",MB_YESNO) == IDNO)
					{
						return TRUE;
					}
				}

				ThreadInfo *threadInfo = new ThreadInfo();
				threadInfo->client = client;
				threadInfo->info = info;
				threadInfo->localFile = pathName;
				CFTPTranserDoc * pDoc = (CFTPTranserDoc *) GetActiveDocument();
				pDoc->count = info->mSize;
				pDoc->current = 0;
				pDoc->last = 0;
				CView * view = (CView*)m_SplitterWnd.GetPane(1,1);
				view->PostMessage(WM_SIZE);


				DWORD threadId;
				HANDLE hthread = ::CreateThread(NULL,0,ThreadFunc,(LPVOID)threadInfo,0,&threadId);
				if(hthread)
				{
					hThread = hthread;
				}
				//client->DownLoad(*info,pathName);
			}

		}
	}
	return TRUE;
}

LRESULT CMainFrame::OnReceiveData(WPARAM wParam,LPARAM lParam)
{	
	
	CView * view = (CView*)m_SplitterWnd.GetPane(1,0);
	CFTPTranserDoc * pDoc = (CFTPTranserDoc *) GetActiveDocument();
	sec.Lock();
	pDoc->current += (int)wParam;	
	pDoc->UpdateAllViews(view,1);	
	sec.Unlock();
	return true;
}

BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
	// create a splitter with 2 row, 1 columns
	if (!m_SplitterWnd.CreateStatic(this, 2, 2))
	{
		TRACE0("Failed to CreateStaticSplitter\n");
		return FALSE;
	}

	// add the first splitter pane - the default view in column 0
	if (!m_SplitterWnd.CreateView(0, 0,
		RUNTIME_CLASS(CFileView), CSize(100, 250), pContext))
	{
		TRACE0("Failed to create first pane\n");
		return FALSE;
	}
	if (!m_SplitterWnd.CreateView(0, 1,
		RUNTIME_CLASS(CFileView), CSize(0, 0), pContext))
	{
		TRACE0("Failed to create third pane\n");
		return FALSE;
	}
	if (!m_SplitterWnd.CreateView(1, 0,
		RUNTIME_CLASS(CMsgView), CSize(0, 0), pContext))
	{
		TRACE0("Failed to create third pane\n");
		return FALSE;
	}
	
	if (!m_SplitterWnd.CreateView(1, 1,
		pContext->m_pNewViewClass, CSize(0, 0), pContext))
	{
		TRACE0("Failed to create third pane\n");
		return FALSE;
	}

	m_SplitterWnd.SetRowInfo(0,250,100);
	m_SplitterWnd.SetColumnInfo(0,380,100);
	return TRUE;
	

	//return CFrameWnd::OnCreateClient(lpcs, pContext);
}

void CMainFrame::OnDestroy()
{
	if(hThread)
	{
		client->running = false;
		WaitForSingleObject(hThread,INFINITE);
		CloseHandle(hThread);
		hThread = NULL;
	}
	CFrameWnd::OnDestroy();

	// TODO: Add your message handler code here
}

⌨️ 快捷键说明

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