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

📄 mainfrm.cpp

📁 从FTP下载文件下载到本地
💻 CPP
📖 第 1 页 / 共 4 页
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "AccountInput.h"
#include "ReadFileDlg.h"
#include "MainFrm.h"
#include "SETFILEDLG.h"
#include "SETFTPDLG.h"
#include "LINKSQLDLG.h"
//#include "CSpreadSheet.h"
#include <direct.h>
#include <io.h>
#include "ReadFileDlg.h"
#include  <Atlbase.h>
#include  "AccountInputDoc.h"
#include "AccountInputView.h"
#include "StdioFileEx.h"
#include "ExportDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMainFrame
//系统托盘
//NOTIFYICONDATA nid; 
IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_COMMAND(ID_SETTINGSQL, OnSettingsql)
	ON_COMMAND(ID_FTPSETTING, OnFtpsetting)
	ON_COMMAND(ID_SETFILE, OnSetfile)
	ON_COMMAND(ID_CONNECT_SQL, OnConnectSql)
	ON_COMMAND(ID_CLOSE_SQL, OnCloseSql)
	ON_UPDATE_COMMAND_UI(ID_CONNECT_SQL, OnUpdateConnectSql)
	ON_UPDATE_COMMAND_UI(ID_CLOSE_SQL, OnUpdateCloseSql)
	ON_UPDATE_COMMAND_UI(ID_READFILE, OnUpdateReadfile)
	ON_COMMAND(ID_READFILE, OnReadfile)
	ON_WM_TIMER()
	ON_COMMAND(ID_EXPORT, OnExport)
	ON_MESSAGE(WM_STATUSMSG, OnSTATUSMSG)
	ON_UPDATE_COMMAND_UI(ID_EXPORT, OnUpdateExport)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	bFTPFileFind=FALSE;
}

CMainFrame::~CMainFrame()
{
}
#define MAX_CHARSET 1024
#define PAGE_CODE	CP_ACP

inline char* Unicode2Char( LPCTSTR w, char* c, int cp = PAGE_CODE )
{
	int nRt=WideCharToMultiByte( cp, 0, w, wcslen(w), c, MAX_CHARSET, NULL, NULL );
	c[nRt] = '\0';
	return c;
}

inline TCHAR* Char2Unicode( TCHAR* t, char* c, int cp = PAGE_CODE )
{
	int nRt = MultiByteToWideChar( cp, 0, c, strlen(c), t, MAX_CHARSET );
	t[nRt] = '\0';
	return t;
}
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	strText="";
	//////////////////////////////////////////////////////////////////////////
	//为工具栏按钮加图标
	//////////////////////////////////////////////////////////////////////////
       if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC))
		{
			TRACE0("Failed to create toolbar\n");
 
		}
		m_wndToolBar.SetButtons(NULL,9);
		bitmap.LoadBitmap(IDB_BITMAP2);
		int i=imageList.Create(32, 32, ILC_COLOR24, 13, 1);
		imageList.Add(&bitmap, (CBitmap*)NULL);
		m_wndToolBar.SendMessage(TB_SETIMAGELIST, 0, (LPARAM)imageList.m_hImageList);
		m_wndToolBar.SendMessage(TB_SETDISABLEDIMAGELIST, 0, (LPARAM)imageList.m_hImageList);
		imageList.Detach();
		bitmap.Detach();
		m_wndToolBar.SetSizes(CSize(40,40),CSize(32,32));
		m_wndToolBar.GetToolBarCtrl().SetButtonWidth(32,32);
		m_wndToolBar.SetButtonInfo(0, ID_CONNECT_SQL, TBSTYLE_BUTTON, 0);		
		m_wndToolBar.SetButtonInfo(1, ID_CLOSE_SQL, TBSTYLE_BUTTON, 1);
		m_wndToolBar.SetButtonInfo(2,0 , TBBS_SEPARATOR, 0);
		m_wndToolBar.SetButtonInfo(3, ID_READFILE, TBSTYLE_BUTTON, 2);
		m_wndToolBar.SetButtonInfo(4, ID_EXPORT, TBSTYLE_BUTTON, 3);		
        m_wndToolBar.SetButtonInfo(5, 0, TBBS_SEPARATOR, 0);
		m_wndToolBar.SetButtonInfo(6, ID_SETTINGSQL, TBSTYLE_BUTTON, 4);
		m_wndToolBar.SetButtonInfo(7, ID_FTPSETTING, TBSTYLE_BUTTON,5);	
		m_wndToolBar.SetButtonInfo(8, ID_SETFILE, TBSTYLE_BUTTON, 6);
	//状态栏
	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}
	
	// TODO: Delete these three lines if you don't want the toolbar to
	//  be dockable
	
	//m_pAccountView->ShowWindow(SW_SHOW);
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);
	AfxEnableControlContainer();
    bConnected=FALSE;
   //当前文件
	GetModuleFileName( NULL , szFileName , MAX_PATH );
    szConfig = szFileName;
	int nPos = szConfig.ReverseFind( '\\' );
	szConfig = szConfig.Left( nPos );
	szConfig += _T("\\MDT.ini") ;
	
    //当前目录
	GetModuleFileName( NULL , szFileName , MAX_PATH );
    strLocalDir = szFileName;
    nPos = strLocalDir.ReverseFind( '\\' );
	strLocalDir = strLocalDir.Left( nPos );
	m_log.SetLogDir(strLocalDir);
	//初始化OLE
	if(!AfxOleInit())
	{
		AfxMessageBox(_T("OLE初始化出錯!"));
		return  -1;
	}
	//数据库
	DWORD  nlength;
	nlength=50;
	LPTSTR strServerIp,strDataBase,strUserID,strUserPassword;
	strServerIp=new TCHAR[50];
	strDataBase=new TCHAR[50];
	strUserID=new TCHAR[50];
	strUserPassword=new TCHAR[50];	
	GetPrivateProfileString(_T("SQLSERVER"),_T("ServerIP"),_T("0"),strServerIp,nlength,szConfig);
	GetPrivateProfileString(_T("SQLSERVER"),_T("DataBase"),_T("0"),strDataBase,nlength,szConfig);
	GetPrivateProfileString(_T("SQLSERVER"),_T("USERID"),_T("0"),strUserID,nlength,szConfig);
	GetPrivateProfileString(_T("SQLSERVER"),_T("PASSWORD"),_T("0"),strUserPassword,nlength,szConfig);
	//保存SQL数据库信息
	m_DataSource =strServerIp;
	m_Initial_Catalog = strDataBase;
	m_UserID = strUserID;
	m_Password = strUserPassword;
	
	/////////////////////////////////////////////////////////////////////////////////////////
	//读取FTP设置
	LPTSTR pFtpServer,pFtpUser,pFtpPass,pFtpPort;
	pFtpServer=new TCHAR[50];
	pFtpUser=new TCHAR[50];
	pFtpPass=new TCHAR[50];
	pFtpPort=new TCHAR[50];
	GetPrivateProfileString(_T("FTP"),_T("FTPSERVER"),_T("0"),pFtpServer,nlength,szConfig);
	GetPrivateProfileString(_T("FTP"),_T("UserName"),_T("0"),pFtpUser,nlength,szConfig);
	GetPrivateProfileString(_T("FTP"),_T("PASSWORD"),_T("0"),pFtpPass,nlength,szConfig);
	GetPrivateProfileString(_T("FTP"),_T("PORT"),_T("0"),pFtpPort,nlength,szConfig);
	//保存FTP数据库信息
	strFtpServer =pFtpServer;
	strFtpUser = pFtpUser;
	strFtpPass = pFtpPass;
	strFtpPort = pFtpPort;
	//文件是否找到
	bFileFind=FALSE;


    ///初始化文件设置内容
		LPTSTR  pAutoRun,pReadOne,pFileSource,pLocalDir,pFTPDir,pFileName,pExportFile;
	pAutoRun=new TCHAR[50];
	pReadOne=new TCHAR[50];
	pFileSource=new TCHAR[50];
    pLocalDir=new TCHAR[50];
	pFTPDir=new TCHAR[50];
	pFileName=new TCHAR[50];
	pExportFile=new TCHAR[50];
	GetPrivateProfileString(_T("FileSetting"),_T("Auto"),_T("0"),pAutoRun,nlength,szConfig);
	GetPrivateProfileString(_T("FileSetting"),_T("ReadOne"),_T("0"),pReadOne,nlength,szConfig);
	GetPrivateProfileString(_T("FileSetting"),_T("FileSource"),_T("0"),pFileSource,nlength,szConfig);
	GetPrivateProfileString(_T("FileSetting"),_T("LocalDir"),_T("0"),pLocalDir,nlength,szConfig);
	GetPrivateProfileString(_T("FileSetting"),_T("FTPDir"),_T("0"),pFTPDir,nlength,szConfig);
	GetPrivateProfileString(_T("FileSetting"),_T("FileName"),_T("0"),pFileName,nlength,szConfig);
	GetPrivateProfileString(_T("FileSetting"),_T("ExportFile"),_T("0"),pExportFile,nlength,szConfig);
	CString  strAtuo,strSource,strReadOne,strFileSource;
    strAtuo.Format(_T("%s"),pAutoRun);
    strReadOne.Format(_T("%s"),pReadOne);
	strSource.Format(_T("%s"),pFileSource);
	m_LocalDir.Format(_T("%s"),pLocalDir);
	m_strFTPDir.Format(_T("%s"),pFTPDir);
	m_FileName.Format(_T("%s"),pFileName);
	m_strExportFile.Format(_T("%s"),pExportFile);
	if (strReadOne==_T("YES"))
	{
		bReadHeader=TRUE;
		
	}
	if (strReadOne==_T("NO"))
	{
		bReadHeader=FALSE;
		
	}
	if (strSource==_T("FTP"))
	{
		bFileSource=TRUE;
		
	}
	if (strSource==_T("DEFINE"))
	{
		bFileSource=FALSE;
	}
	
	if (strAtuo==_T("YES"))
	{
		bAutoRun=TRUE;
		//开启自动运行
        AutoRun( );		
	}
    else// (strAtuo=="NO")
	{
		bAutoRun=FALSE;
		//删除自动运行
		CloseAutoRun();
		
	}
   //::CoInitialize(NULL);
	
	m_nTimes=0;
	m_nSize=0;
	SetTimer(1,1000*60,NULL);
    return  0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

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

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

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers


void CMainFrame::OnSettingsql() 
{
	// TODO: Add your command handler code here
	CLINKSQLDLG  dlg;
	if (dlg.DoModal()==IDOK)
	{
		m_DataSource=dlg.m_DataSource ;
		m_Initial_Catalog=dlg.m_Initial_Catalog; 
		m_UserID=dlg.m_UserID ;
		m_Password=dlg.m_Password;
	}	
	WritePrivateProfileString( _T("SQLSERVER"), _T("ServerIP"), m_DataSource, szConfig );
	WritePrivateProfileString( _T("SQLSERVER"), _T("DataBase"), m_Initial_Catalog, szConfig );
	WritePrivateProfileString( _T("SQLSERVER"), _T("USERID"), m_UserID, szConfig );
    WritePrivateProfileString( _T("SQLSERVER"), _T("PASSWORD"), m_Password, szConfig );
	ReSetData();
}

void CMainFrame::OnFtpsetting() 
{
	// TODO: Add your command handler code here
    CSETFTPDLG  dlg;
 	if (dlg.DoModal()==IDOK)
	{
		strFtpServer=dlg.m_ServerName; 
		strFtpUser=dlg.m_FTP_UserName ;
		strFtpPass=dlg.m_Password;
		strFtpPort=dlg.m_PORT;
		WritePrivateProfileString( _T("FTP"), _T("FTPSERVER"), strFtpServer, szConfig );
		WritePrivateProfileString( _T("FTP"), _T("UserName"), strFtpUser, szConfig );
		WritePrivateProfileString( _T("FTP"), _T("PASSWORD"), strFtpPass, szConfig );
		WritePrivateProfileString( _T("FTP"), _T("PORT"), strFtpPort, szConfig );
	}
	ReSetData();
}

void CMainFrame::OnSetfile() 
{
	// TODO: Add your command handler code here
	CSETFILEDLG  dlg;
	if (dlg.DoModal()==IDOK)
	{

	}
	ReSetData();
}



void CMainFrame::OnConnectSql() 
{
	// TODO: Add your command handler code here
	//
	strTemp =_T("正在連接數據庫...");
	this->SendMessage( WM_STATUSMSG, (WPARAM)&strTemp );
	SetDoc(strTemp);
	if (m_pConnection!=NULL)
	{
		
		m_pConnection.Release();
		//m_pConnection->Close();	
	}
	HRESULT   hr;   
	hr=m_pConnection.CreateInstance(_T("ADODB.Connection"));
	if(SUCCEEDED(hr)<0)   
	{
		// 创建Connection对象失败  		
		bConnected=FALSE;
		strTemp =_T("创建Connection对象失败!");
		this->SendMessage( WM_STATUSMSG, (WPARAM)&strTemp );
		SetDoc(strTemp);
		return ;
	} 
	try
	{   
		CString  strSQL;
		strSQL=_T("Provider=SQLOLEDB.1;Data Source=");
		strSQL+=m_DataSource;
		strSQL+=_T(";Initial Catalog=");
		strSQL+=m_Initial_Catalog;
		strSQL+=_T(";User ID=");
		strSQL+=m_UserID;
		strSQL+=_T(";Password=");
		strSQL+=m_Password;
		m_pConnection->ConnectionTimeout = 5;
		m_pConnection->Open(_bstr_t(strSQL),_bstr_t(m_UserID),_bstr_t(m_Password),adModeUnknown);				
	}                                       
	catch(_com_error e)
	{	
		CString errormessage;
		errormessage.Format(_T("連接數據庫錯誤,原因:%s"),e.ErrorMessage());
		this->SendMessage( WM_STATUSMSG, (WPARAM)&errormessage );
		SetDoc(errormessage);
		bConnected=FALSE;
		return;
	}
	strTemp =_T("連接數據庫成功!");
	this->SendMessage( WM_STATUSMSG, (WPARAM)&strTemp );
	SetDoc(strTemp);
    bConnected=TRUE;
}

void CMainFrame::OnCloseSql() 
{
	// TODO: Add your command handler code here
	if (m_pConnection!=NULL)
	{
	    m_pConnection->Close();
        m_pConnection.Release();
		CString strClose;
		strClose.Format(_T("數據庫已經關閉!"));
		this->SendMessage( WM_STATUSMSG, (WPARAM)&strClose );
		SetDoc(strClose);
		bConnected=FALSE;
	}	
}


void CMainFrame::OnReadfile() 
{
	// TODO: Add your command handler code here
	if (bFileSource)
	{
		   if(!OnConnectFTP())
		   {
               
			   strTemp =_T("連接FTP服務器失败!");   
			   //AfxMessageBox(_T("連接FTP服務器失败!"));
			   this->SendMessage( WM_STATUSMSG, (WPARAM)&strTemp );
			   SetDoc(strTemp);
			   return;
		   }else
		   {
               strTemp =_T("連接FTP服務器成功!");   
			   //AfxMessageBox(_T("連接FTP服務器成功!"));
			   this->SendMessage( WM_STATUSMSG, (WPARAM)&strTemp );
			   SetDoc(strTemp);
			  
		   }
		   SelectFTPFile();
		   if(bFTPFileFind)
		   {
			   strTemp =_T("在FTP服務器中存在該文件!");  
			   this->SendMessage( WM_STATUSMSG, (WPARAM)&strTemp );
			   SetDoc(strTemp);		                  

⌨️ 快捷键说明

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