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

📄 myftpclientdlg.cpp

📁 Visual C++ 网络通信编程实用案例精选 的配套光盘
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// FTPSOCKETDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MyFTPClient.h"
#include "MyFTPClientDlg.h"
#include "ListItem.h"


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

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

char szLocDirectory[MAX_PATH];
char szFtpDirectory[MAX_PATH];



char szAppName[256];
char szFtpName[256];
char szFtpUser[20];
char szFtpPassword[20];



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()

/////////////////////////////////////////////////////////////////////////////
// CMyFTPClientDlg dialog

CMyFTPClientDlg::CMyFTPClientDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMyFTPClientDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMyFTPClientDlg)
	m_PWD = _T("");
	m_Server = _T("");
	m_UID = _T("");
	m_Message = _T("");
	m_Port = 0;
	m_PASV = FALSE;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMyFTPClientDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyFTPClientDlg)
	DDX_Control(pDX, IDC_LIST_SERVERFILE, m_ctrlServerFile);
	DDX_Control(pDX, IDC_LIST_LOCALFILE, m_ctrlLocalFile);
	DDX_Control(pDX, IDC_COMBOBOXEX_SERVER, m_ctrlServer);
	DDX_Control(pDX, IDC_COMBOBOXEX_LOCAL, m_ctrlLocal);
	DDX_Text(pDX, IDC_EDIT_PWD, m_PWD);
	DDX_Text(pDX, IDC_EDIT_SERVER, m_Server);
	DDX_Text(pDX, IDC_EDIT_UID, m_UID);
	DDX_Text(pDX, IDC_EDIT_MESSAGE, m_Message);
	DDX_Text(pDX, IDC_EDIT_PORT, m_Port);
	DDX_Check(pDX, IDC_CHECK_PASV, m_PASV);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyFTPClientDlg, CDialog)
	//{{AFX_MSG_MAP(CMyFTPClientDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_CONNECT, OnButtonConnect)
	ON_NOTIFY(NM_DBLCLK, IDC_LIST_LOCALFILE, OnDblclkListLocalfile)
	ON_CBN_SELCHANGE(IDC_COMBOBOXEX_LOCAL, OnSelchangeComboboxexLocal)
	ON_NOTIFY(NM_DBLCLK, IDC_LIST_SERVERFILE, OnDblclkListServerfile)
//}}AFX_MSG_MAP

	ON_MESSAGE(WM_SETFILE,CMyFTPClientDlg::OnInsertLocFile)
	ON_MESSAGE(WM_SETREDRAWFLAG,OnSetLocRedrawFlag)
	ON_MESSAGE(WM_SETDIRECTORYDISPLAY,OnSetLocDirectoryDisplay)
	ON_MESSAGE(WM_REDISPLAYFILE,OnReDisplayLocFile)
	ON_MESSAGE(WM_SETDIR,OnSetLocDir)

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyFTPClientDlg message handlers

BOOL CMyFTPClientDlg::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

	//创建ImageList
    HIMAGELIST hImageList;
	//定义文件信息变量
	SHFILEINFO shFi;
	//获取文件图标信息
	hImageList = (HIMAGELIST)SHGetFileInfo("C:\\",0,&shFi,sizeof(shFi),
		SHGFI_SYSICONINDEX|SHGFI_SMALLICON);
	//将文件图象列表存入m_ctImageList
	m_ctImageList.m_hImageList = hImageList;
	

	// TODO: Add extra initialization here
	//设定客户端文件显示列表框的栏标题
	SetFileColumns(&m_ctrlLocalFile);
	SetServerFileColumns(&m_ctrlServerFile);

	SetImageList(&m_ctrlLocalFile,&m_ctrlLocal);
	SetImageList(&m_ctrlServerFile,NULL);

	m_ctrlLocalFile.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);



	//m_ctrlLocalFile.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
	//获取本地驱动器信息 

	GetLocDir();

	int i=m_ctrlLocal.FindStringExact(-1,"C:\\");

	m_ctrlLocal.SetCurSel(i);
	::SetCurrentDirectory("C:\\");
	GetLocCurrentDirectory();
	BeginFindLocFile();	
	//初始化,为了调试,设定初试值
	char host[100];
	gethostname(host,100);
	hostent * ht=gethostbyname(host);
	char * temp=ht->h_addr_list[0];
	struct   in_addr in;  
	memcpy((void*)&in,temp,sizeof(in));
	m_Server=inet_ntoa(in);
	m_Port=21;
	m_UID="anonymouse";
	m_PWD="anonymouse";
	UpdateData(false);


	return TRUE;  // 
}

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


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

void CMyFTPClientDlg::OnButtonConnect() 
{
	int ndx=0;	
	CString str;
	//将控件中的数据更新到对应的变量中
	UpdateData(TRUE);
	m_Message=m_Message+"连接服务器:"+m_Server+"\r\n";
	m_Message=m_Message+"登录用户名:"+m_UID+"\r\n";
	UpdateData(false);

	if(!ftp.LogOnToServer(m_Server,m_Port,m_UID,m_PWD,"","","","",1080,0)) 
	{
		m_Message=m_Message+m_UID+"登录失败。"+"\r\n";
		UpdateData(false);

		return;
	}
	else
	{
		ftp.List ();
		while(ftp.GetLine(ndx))
		{
			AddLine(ftp.m_strLine );
			str="/";
			strcpy(szFtpDirectory,str);
			
			SetServerDir(str);
		}
		ftp.FTPcommand(""); 
	}
}



void CMyFTPClientDlg::SetFileColumns(CListCtrl *pListCtrl)
{
   	static struct
	{
		LPSTR pszText;
		int uiFormat;
	}columns[]={
		_T("文件名"),LVCFMT_LEFT,
		_T("日期"),LVCFMT_CENTER,
		_T("字节数"),LVCFMT_CENTER
	};
	for(int i=0,j=120;i<sizeof(columns)/sizeof(columns[0]);i++,j-=10)
		pListCtrl->InsertColumn(i,columns[i].pszText,columns[i].uiFormat,j);
}


void CMyFTPClientDlg::SetServerFileColumns(CListCtrl *pListCtrl)
{
   	static struct
	{
		LPSTR pszText;
		int uiFormat;
	}columns[]={
		_T("文件名"),LVCFMT_LEFT,
		_T("日期"),LVCFMT_CENTER,
		_T("权限"),LVCFMT_CENTER
	};
	for(int i=0,j=120;i<sizeof(columns)/sizeof(columns[0]);i++,j-=10)
		pListCtrl->InsertColumn(i,columns[i].pszText,columns[i].uiFormat,j);
}


BOOL CMyFTPClientDlg::SetImageList(CListCtrl* pListCtrl,CComboBoxEx* pCombo)
{
	pListCtrl->SetImageList(&m_ctImageList,LVSIL_SMALL);
	if(!pCombo)
		return TRUE;
	pCombo->SetImageList(&m_ctImageList);
	return TRUE;
}

void CMyFTPClientDlg::GetLocDir()
{
	int iIcon,iIconSel;
	char lpBuffer[MAX_PATH];
	char* ptr;
	CString str;
	m_iDirCount=0;
	::GetLogicalDriveStrings(MAX_PATH,lpBuffer);
	ptr=lpBuffer;
	while(*ptr)
	{
		m_iDirCount++;
		str=ptr;
		GetFileIcon(str,&iIcon,&iIconSel);
		InsertLocDir(str,iIcon,iIconSel);
		ptr+=strlen(ptr)+1;
	}
}


int CMyFTPClientDlg::InsertLocDir(CString &str, int iIcon, int iIconSel)
{
   int nItem=m_ctrlLocal.GetCount();
	COMBOBOXEXITEM Combo;
	Combo.mask=CBEIF_IMAGE|CBEIF_TEXT|CBEIF_SELECTEDIMAGE;
	Combo.iImage=iIcon;
	Combo.iSelectedImage=iIconSel;
	Combo.pszText=(LPTSTR)(LPCTSTR)str;
	Combo.iItem=nItem;
	m_ctrlLocal.InsertItem(&Combo);
	return nItem;
}

void CMyFTPClientDlg::GetFileIcon(CString& fileName,int* iIcon,int* iIconSel)
{
	CString str=fileName;
	if(str.Right(1)!="\\")
		str+="\\";
	SHFILEINFO shFi;
	if(!SHGetFileInfo(str,0,&shFi,sizeof(shFi),SHGFI_ICON|SHGFI_SMALLICON|SHGFI_DISPLAYNAME))
	{
		AfxMessageBox("111");
		return;
	}
	*iIcon=shFi.iIcon;
	DestroyIcon(shFi.hIcon);
	if(iIconSel)
	{
		if(!SHGetFileInfo(str,0,&shFi,sizeof(shFi),SHGFI_ICON|SHGFI_SMALLICON|SHGFI_OPENICON|SHGFI_DISPLAYNAME))
		{
			AfxMessageBox("222");
			return;
		}
		*iIconSel=shFi.iIcon;
		DestroyIcon(shFi.hIcon);
	}
	return ;
}

void CMyFTPClientDlg::GetLocCurrentDirectory()
{
    CString str;
	m_ctrlLocal.GetWindowText(str);
	strcpy(szLocDirectory,str);
}

BOOL CMyFTPClientDlg::BeginFindLocFile()
{
	m_ctrlLocalFile.DeleteAllItems();

	FindLocFile();
		return TRUE;
}

void  CMyFTPClientDlg::FindLocFile()
{

⌨️ 快捷键说明

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