📄 viewdial.cpp
字号:
// viewDial.cpp : implementation file
//
#include "stdafx.h"
#include "ftpclient.h"
#include "viewDial.h"
#include "FtpGet.h"
#include "connectDial.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
CString host;
int port;
CString user;
CString password;
BOOL pasv;
connectDial a;
int count=0;
#endif
/////////////////////////////////////////////////////////////////////////////
// viewDial dialog
viewDial::viewDial(CWnd* pParent /*=NULL*/)
: CDialog(viewDial::IDD, pParent)
{
//{{AFX_DATA_INIT(viewDial)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void viewDial::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(viewDial)
DDX_Control(pDX, IDC_PROGRESS1, m_progress);
DDX_Control(pDX, IDC_LIST1, m_FtpFile);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(viewDial, CDialog)
//{{AFX_MSG_MAP(viewDial)
ON_LBN_SELCHANGE(IDC_LIST1, OnSelchangeList1)
ON_BN_CLICKED(IDDOWN, OnDown)
ON_BN_CLICKED(IDUP, OnUp)
ON_NOTIFY(NM_OUTOFMEMORY, IDC_PROGRESS1, OnOutofmemoryProgress1)
ON_NOTIFY(NM_DBLCLK, IDC_LIST1, OnDblclkListFile)
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// viewDial message handlers
BOOL viewDial::OnInitDialog()
{ CString dir;
DWORD dwStyle;
a.DoModal();
host=a.host;
port=a.port;
user=a.user;
password=a.password;
pasv=a.pasv;
if(a.flag==-1) {CDialog::OnInitDialog(); CDialog::OnCancel();return FALSE;}
CDialog::OnInitDialog();
dwStyle = m_FtpFile.GetStyle();
dwStyle |= LVS_EX_GRIDLINES |LVS_EX_FULLROWSELECT|LVS_SHOWSELALWAYS ;
m_FtpFile.SetExtendedStyle(dwStyle);
m_FtpFile.SetBkColor(RGB(0xec,0xf1,0xfd));
m_FtpFile.SetTextBkColor(RGB(0xfe,0xFF,0xc6));
/*
// TODO: Add extra initialization here
//设置CListCtrl对象的属性
*/
//m_FtpFile.SetBkColor(RGB(22,100,100));
m_FtpFile.InsertColumn(0,"文件名",LVCFMT_CENTER,150);
m_FtpFile.InsertColumn(1,"日期",LVCFMT_CENTER,100);
m_FtpFile.InsertColumn(2,"时间",LVCFMT_CENTER,100);
m_FtpFile.InsertColumn(3,"字节数",LVCFMT_CENTER,100);
dir=CFtpGet::DirList(a.hCtrlSock,a.port,a.host ,"./",pasv);
ListContent( dir) ;
CWnd::ShowWindow(SW_SHOW);
return TRUE;
// return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void viewDial::ListContent(CString dir)
{
CString filename="..";
CString fileday="";
CString filetime="";
CString filelength="<DIR>";
CString variable,a;
CString c1="\n",c2=" ";
int pos=0,i=0,v=0;
int line=1;
m_FtpFile.DeleteAllItems();
m_FtpFile.InsertItem(0,filename,0);
m_FtpFile.SetItemText(0,1,fileday);
m_FtpFile.SetItemText(0,2,filetime);
m_FtpFile.SetItemText(0,3,filelength);
while(1)
{
pos=dir.Find(c1, i);
if(pos==-1)
{
int s=0,e1=0,e2=0;
variable=dir.Mid(i);
for(int j=0;j<4;j++)
{
e1=variable.Find(c2,s);
while((e1-e2)==1){ s=e1+1;e2=e1;e1=dir.Find(c2,s);}
if(j==3) {filename=variable.Mid(s);break;}
if(j==0) fileday=variable.Mid(s,e1-s);
if(j==1) filetime=variable.Mid(s,e1-s);
if(j==2) filelength=variable.Mid(s,e1-s);
s=e1+1;
e2=e1;
}
i=pos+1;
m_FtpFile.InsertItem(line,filename,0);
m_FtpFile.SetItemText(line,1,fileday);
m_FtpFile.SetItemText(line,2,filetime);
m_FtpFile.SetItemText(line,3,filelength);
line++;
return;
}
if(pos-i!=1)
{ int s=0,e1=0,e2=0;
variable=dir.Mid(i,pos-i);
for(int j=0;j<4;j++)
{
e1=variable.Find(c2,s);
while((e1-e2)==1){s=e1+1;e2=e1;e1=variable.Find(c2,s);}
if(e1==-1) {filename=variable.Mid(s,pos-i-s-1);break;}
if(j==0) {fileday=variable.Mid(s,e1-s);}
if(j==1) {filetime=variable.Mid(s,e1-s);}
if(j==2) {filelength=variable.Mid(s,e1-s);}
s=e1+1;
e2=e1;
}
}
i=pos+1;
int myflag=0;
int num=0;
for(int t=0;t<filelength.GetLength();t++)
{num=filelength.Find(" ",t);
if(num==t);
else {myflag=1;break;}
}
if(!myflag) {filelength="<DIR>";}
m_FtpFile.InsertItem(line,filename,0);
m_FtpFile.SetItemText(line,1,fileday);
m_FtpFile.SetItemText(line,2,filetime);
m_FtpFile.SetItemText(line,3,filelength);
filename.Empty();
fileday.Empty();
filetime.Empty();
filelength.Empty();
line++;
}
}
void viewDial::OnSelchangeList1()
{
}
void viewDial::OnOK()
{ CString str=CFtpGet::myCloseSocket(a.hCtrlSock);
AfxMessageBox(str);
CDialog::OnOK();
}
void viewDial::OnCancel()
{
// TODO: Add extra cleanup here
CDialog::OnCancel();
}
void viewDial::OnDown()
{ CFtpGet a;
int i=m_FtpFile.GetNextItem(-1,LVNI_SELECTED);
if (i==-1)
{
AfxMessageBox("没有选择文件!",MB_OK | MB_ICONQUESTION);
}
else
{
CString filelength=m_FtpFile.GetItemText(i,3); //得到选择项的类型
if (filelength!="<DIR>") //选择的是文件
{
CString strDestName;
CString strSourceName;
strSourceName = m_FtpFile.GetItemText(i,0);//得到所要下载的文件名
CFileDialog dlg(FALSE,"",strSourceName);
if (dlg.DoModal()==IDOK)
{
//获得下载文件在本地机上存储的路径和名称
strDestName=dlg.GetPathName();
AfxBeginThread(thread1,(void *)this,0,0,NULL);
//调用CFtpConnect类中的GetFile函数下载文件
if (a.FtpDownLoad(
host,
port,
user,
password,
strSourceName,
strDestName,
5,
pasv))
{AfxMessageBox("下载成功!",MB_OK|MB_ICONINFORMATION);
m_progress.SetPos(0);}
else
{AfxMessageBox("下载失败!",MB_OK|MB_ICONSTOP);
m_progress.SetPos(0);}
}
}
else
{
//选择的是目录
AfxMessageBox("不能下载目录!\n请重选!",MB_OK|MB_ICONSTOP);
}
}
// MessageBox(str);
}
void viewDial::OnUp()
{ CFtpGet a;
CString strDestName;
CString strSourceName;
CFileDialog dlg(TRUE,"","*.*");
if (dlg.DoModal()==IDOK)
{
//获得待上传的本地机文件路径和文件名
strSourceName = dlg.GetPathName();
strDestName=dlg.GetFileName();
count=0;
//调用CFtpConnect类中的PutFile函数上传文件
CFtpSect*my=new CFtpSect[1];
my->szHostAddr=host;
my->nPort=port;
my->szUserName=user;
my->szPassword=password;
my->szDesFilename=strSourceName;
my->szFtpFilename=strDestName;
my->bPasvMode=pasv;
AfxBeginThread(thread1,(void *)this,0,0,NULL);
if (a.FtpUpLoad(my->szHostAddr,my->nPort,my->szUserName,my->szPassword,my->szFtpFilename,my->szDesFilename,my->bPasvMode))
{ AfxMessageBox("上传成功!",MB_OK|MB_ICONINFORMATION);
m_progress.SetPos(0);
}
else
{AfxMessageBox("上传失败!",MB_OK|MB_ICONSTOP);
m_progress.SetPos(0);
}
}
}
void viewDial::OnOutofmemoryProgress1(NMHDR* pNMHDR, LRESULT* pResult)
{
}
void viewDial::OnDblclkListFile(NMHDR* pNMHDR, LRESULT* pResult)
{
CFtpGet a;
int i=m_FtpFile.GetNextItem(-1,LVNI_SELECTED);
CString strSub = m_FtpFile.GetItemText(i,0);
if (i==-1)
{
AfxMessageBox("没有选择目录!",MB_OK | MB_ICONQUESTION);
}
else
{ //判断是不是目录
if ("<DIR>"!=m_FtpFile.GetItemText(i,3))
{
CString strDestName;
CString strSourceName;
strSourceName = m_FtpFile.GetItemText(i,0);//得到所要下载的文件名
CFileDialog dlg(FALSE,"",strSourceName);
if (dlg.DoModal()==IDOK)
{
//获得下载文件在本地机上存储的路径和名称
strDestName=dlg.GetPathName();
AfxBeginThread(thread1,(void *)this,THREAD_PRIORITY_HIGHEST,0,NULL);
//调用CFtpConnect类中的GetFile函数下载文件
if (a.FtpDownLoad(
host,
port,
user,
password,
strSourceName,
strDestName,
5,
pasv))
{AfxMessageBox("下载成功!",MB_OK|MB_ICONINFORMATION);
m_progress.SetPos(0);}
else
{AfxMessageBox("下载失败!",MB_OK|MB_ICONSTOP);
m_progress.SetPos(0);}
}
}
else
{ SOCKET hCtrlSock=
CFtpGet::ConnectFtp( host, port, user, password);
//设置当前目录
CString dir=CFtpGet::DirList(hCtrlSock,port,host ,strSub,pasv);
//对当前目录进行查询
ListContent( dir) ;
}
}
*pResult = 0;
}
void viewDial::OnPaint()
{ CPaintDC dc(this);
CRect rect;
GetClientRect(&rect);//得到窗体的大小
CDC dcMem;
dcMem.CreateCompatibleDC(&dc);
CBitmap bmpBackground;
bmpBackground.LoadBitmap(IDB_BITMAP3);//加载背景图片
BITMAP bitMap;
bmpBackground.GetBitmap(&bitMap);
CBitmap *pbmpOld=dcMem.SelectObject(&bmpBackground);
dc.StretchBlt(0,0,rect.Width(),rect.Height(),&dcMem,0,0,bitMap.bmWidth, bitMap.bmHeight,SRCCOPY);//该函数给对话框贴上位图
}
UINT viewDial::thread1(void *p)
{
viewDial *object=(viewDial *)p;
object->m_progress.SetPos(0);
object->m_progress.SetRange(1,101);
object->m_progress.SetStep(1);
for(int i=0;i<100;i++)
{
object->m_progress.StepIt();
Sleep(3);
}
/*object->m_progress.SetPos(0); */
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -