www.pudn.com > DSPSample.rar > SpiderWnd.cpp


// SpiderWnd.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "SpiderWnd.h" 
#include "DSP.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// SpiderWnd 
#define  IDC_BTNABORT			56789					//放弃按钮的ID 
#define	 MSG_ASSIGNNEWSECT		12345 
#define  MSG_CREATERECVTHREAD	12346 
 
SpiderWnd*	SpiderWnd::pLastWnd	=NULL; 
 
SpiderWnd::SpiderWnd() 
{ 
} 
 
SpiderWnd::~SpiderWnd() 
{ 
	if(this->m_btnAbort.m_hWnd!=NULL) 
	{ 
		this->m_btnAbort.DestroyWindow(); 
		this->m_staCurrLen.DestroyWindow(); 
		this->m_staFilename.DestroyWindow(); 
		this->m_staPrompt.DestroyWindow(); 
		this->m_progress.DestroyWindow(); 
	} 
} 
 
 
 
BEGIN_MESSAGE_MAP(SpiderWnd, CWnd) 
	//{{AFX_MSG_MAP(SpiderWnd) 
		// NOTE - the ClassWizard will add and remove mapping macros here. 
	//}}AFX_MSG_MAP 
	ON_MESSAGE(DSPSpider::MSG_SENDSECT,OnUpdateSect) 
	ON_MESSAGE(DSPSpider::MSG_RECVSECT,OnUpdateSect) 
	ON_MESSAGE(DSPSpider::MSG_SENDCOMPLETE,OnSendComplete) 
	ON_MESSAGE(DSPSpider::MSG_SETFILENAMESIZE,OnSetFilenameSize) 
	ON_MESSAGE(DSPSpider::MSG_SHOWPROMPT,OnShowPrompt) 
	ON_MESSAGE(MSG_ASSIGNNEWSECT,OnAssignNewSect) 
	ON_MESSAGE(MSG_CREATERECVTHREAD,OnCreateRecvThread) 
//	ON_COMMAND(IDC_BTNABORT,OnAbort) 
END_MESSAGE_MAP() 
 
 
///////////////////////////////////////////////////////////////////////////// 
// SpiderWnd message handlers 
 
BOOL SpiderWnd::Create(int offx,int offy,int width, CWnd *pParentWnd) 
{ 
	//创建窗体本身 
	CRect rect(offx,offy,offx+width,offy+WND_HEIGHT); 
	if(!CWnd::CreateEx(WS_EX_TOPMOST|WS_EX_STATICEDGE|WS_EX_TOOLWINDOW, 
			AfxRegisterWndClass(CS_SAVEBITS,NULL,NULL,NULL), 
			NULL,WS_POPUP|WS_DLGFRAME,rect,pParentWnd,NULL,NULL)) 
		return FALSE; 
 
	CRect rectWnd; 
	this->m_font.CreateFont(10,				// nHeight 
					4,						// nWidth 
					0,						// nEscapement 
					0,						// nOrientation 
					FW_MEDIUM,				// nWeight 
					0,						// bItalic 
					0,						// bUnderline 
					0,						// bStrikeOut 
					0,						// nCharSet 
					OUT_DEFAULT_PRECIS,		// nOutPrecision 
					CLIP_DEFAULT_PRECIS,	// nClipPrecision 
					PROOF_QUALITY,			// nQuality 
					0,						// nPitchAndFamily 
					"Terminal");			// lpszFacename 
	this->SetFont(&this->m_font); 
	this->GetClientRect(rectWnd); 
	this->ReviseContent(rectWnd); 
	return TRUE; 
} 
 
void SpiderWnd::ReviseContent(CRect &rect) 
{ 
	CRect rectProg,rectName,rectSize,rectCurr,rectPrompt,rectCancel; 
	 
	//名字、进度条与窗体的宽度相同 
	rectName	=rectProg	=rect; 
 
	//计算名字的位置 
	rectName.bottom		=WND_FONT_HEIGHT; 
 
	//计算进度条的位置 
	rectProg.top		=rectName.bottom; 
	rectProg.bottom		=rectProg.top+8; 
 
	//计算下载字节数提示的位置 
	rectCurr.left		=rect.left; 
	rectCurr.top		=rectProg.bottom; 
	rectCurr.bottom		=rectCurr.top+WND_FONT_HEIGHT; 
	rectCurr.right		=rect.Width()/2; 
	rectSize.left		=rectCurr.right; 
	rectSize.right		=rect.right; 
	rectSize.top		=rectCurr.top; 
	rectSize.bottom		=rectCurr.bottom; 
 
	//计算提示信息框的位置,放弃按钮在提示信息框的右边 
	rectPrompt.top		=rectSize.bottom; 
	rectPrompt.bottom	=rectPrompt.top+WND_BTN_WIDTH; 
	rectPrompt.left		=rect.left; 
	rectCancel.right	=rect.right; 
	rectCancel.left		=rect.right-WND_BTN_WIDTH; 
	rectCancel.top		=rectPrompt.top; 
	rectCancel.bottom	=rectPrompt.bottom; 
	rectPrompt.right	=rectCancel.left; 
 
	if(this->m_progress.m_hWnd==NULL) 
	{ 
		//如果按钮没有被创建 
		this->m_staFilename.Create(NULL,WS_CHILD|WS_VISIBLE,rectName,this); 
		this->m_progress.Create(WS_CHILD|WS_VISIBLE|PBS_SMOOTH,rectProg,this,0xFFFF); 
		this->m_staPrompt.Create("start recv......",WS_CHILD|WS_VISIBLE,rectPrompt,this); 
		this->m_btnAbort.Create("X",WS_CHILD|WS_VISIBLE,rectCancel,this,IDC_BTNABORT); 
		this->m_staCurrLen.Create("(0K) 0",WS_CHILD|WS_VISIBLE|SS_RIGHT,rectCurr,this); 
		this->m_staFilesize.Create("/0 (0K)",WS_CHILD|WS_VISIBLE,rectSize,this); 
		this->m_staFilename.SetFont(&this->m_font,FALSE); 
		this->m_staPrompt.SetFont(&this->m_font,FALSE); 
		this->m_staCurrLen.SetFont(&this->m_font,FALSE); 
		this->m_staFilesize.SetFont(&this->m_font,FALSE); 
		this->m_btnAbort.SetFont(&this->m_font); 
	} 
} 
 
LRESULT SpiderWnd::OnUpdateSect(WPARAM wParam, LPARAM lParam) 
{ 
	CString str; 
	DWORD dwSize	=this->m_spider.OnUpdateSect(wParam,lParam); 
	this->m_progress.StepIt(); 
	str.Format("(%dK) %d",dwSize/1024,dwSize); 
	this->m_staCurrLen.SetWindowText(str); 
	return 0; 
}; 
 
LRESULT SpiderWnd::OnSetFilenameSize(WPARAM wParam, LPARAM lParam) 
{ 
	//设置文件的名字和大小 
	DSPSpider::_FILEDETAIL* lphfd	=(DSPSpider::_FILEDETAIL*)wParam; 
 
	//显示文件名 
	this->m_staFilename.SetWindowText(lphfd->strPathname); 
 
	//显示总的长度 
	CString str; 
	str.Format("/ %d (%dK)",lphfd->dwFilesize,lphfd->dwFilesize/1024); 
	this->m_staFilesize.SetWindowText(str); 
 
	//初始化当前接收的长度 
	this->m_staCurrLen.SetWindowText("0K"); 
 
	//初始化进度显示 
	this->m_progress.SetRange32(0,lphfd->dwFilesize); 
	this->m_progress.SetStep(this->m_spider.GetProgressStep()); 
	this->m_progress.SetPos(this->m_spider.GetCurrSize()); 
	return 0; 
} 
 
LRESULT SpiderWnd::OnSendComplete(WPARAM wParam, LPARAM lParam) 
{ 
	LRESULT l	=this->m_spider.OnSendComplete(wParam,lParam); 
	switch(l) 
	{ 
		case 1: 
			{ 
				//禁止关闭窗体按钮 
				this->m_btnAbort.EnableWindow(FALSE); 
 
				//已全部下载完 
				::AfxMessageBox("文件接收完成!"); 
			} 
		case 2: 
			//所有的线程都已经终止 
			this->m_btnAbort.EnableWindow(TRUE); 
			break; 
	} 
	return 0; 
} 
 
LRESULT SpiderWnd::OnAbort(WPARAM wParam, LPARAM lParam) 
{ 
	if(this->m_spider.OnAbort(wParam,lParam)) 
	{ 
		//如果已经没有线程在运行啦,则关闭窗体 
		SpiderWnd::pLastWnd	=NULL; 
		this->DestroyWindow(); 
		delete this; 
	} 
	else 
	{ 
		//如果有线程在运行,则终止运行的线程 
		this->m_btnAbort.EnableWindow(FALSE); 
	} 
	return 0; 
}; 
 
LRESULT SpiderWnd::OnAssignNewSect(WPARAM wParam, LPARAM lParam) 
{ 
	return this->m_spider.OnAssignNewSect(wParam,lParam); 
}; 
 
LRESULT SpiderWnd::OnCreateRecvThread(WPARAM wParam, LPARAM lParam) 
{ 
	return this->m_spider.OnCreateRecvThread(wParam,lParam); 
}; 
 
LRESULT SpiderWnd::OnShowPrompt(WPARAM wParam,LPARAM lParam) 
{ 
	CString str; 
	switch((WORD)wParam) 
	{ 
		case DSPSpider::PROMPT_RECVSLEEP: 
			str.Format("Recv:%d ,Sleep:%d.",(BYTE)(wParam>>16),(BYTE)(wParam>>24)); 
			break; 
		case DSPSpider::PROMPT_MORETHAN: 
			str.Format("Recv:%d ,Sleep:%d, cann't create more than.",(BYTE)(wParam>>16),(BYTE)(wParam>>24)); 
			break; 
		case DSPSpider::PROMPT_NOIP: 
			str.Format("Recv:%d ,Sleep:%d, No IP",(BYTE)(wParam>>16),(BYTE)(wParam>>24)); 
			break; 
		case DSPSpider::PROMPT_CONNECTNEWIP: 
			str.Format("connect to IP %s!",(char*)lParam);  
			break; 
		case DSPSpider::PROMPT_CANNOTCONNECT: 
			str.Format("connect to IP %s Failed!",(char*)lParam);  
			break; 
		case DSPSpider::PROMPT_RECVABORT: 
			str	="Recv abort.All Thread have been stoped."; 
			break; 
		case DSPSpider::PROMPT_RECVEND: 
			str	="File recv successful.Clear up file."; 
	} 
	this->m_staPrompt.SetWindowText(str); 
	return 0; 
}; 
 
BOOL SpiderWnd::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)  
{ 
	// TODO: Add your specialized code here and/or call the base class 
	if(nCode==0 && nID==IDC_BTNABORT) 
	{ 
		this->OnAbort(0,0); 
		return true; 
	} 
	return CWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo); 
}