📄 mainfrm.cpp
字号:
// MainFrm.cpp : implmentation of the CMainFrame class
//
/////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "resource.h"
#include <WinInet.h>
#include "AboutDlg.h"
#include "FileTransferKitView.h"
#include "MainFrm.h"
#include "ConfigManager.h"
BOOL CMainFrame::PreTranslateMessage(MSG* pMsg)
{
if(CFrameWindowImpl<CMainFrame>::PreTranslateMessage(pMsg))
return TRUE;
return view_.PreTranslateMessage(pMsg);
}
BOOL CMainFrame::OnIdle()
{
//UIUpdateToolBar();
return FALSE;
}
LRESULT CMainFrame::OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
//// create command bar window
//HWND hWndCmdBar = cmdBar_.Create(m_hWnd, rcDefault, NULL, ATL_SIMPLE_CMDBAR_PANE_STYLE);
//// attach menu
//cmdBar_.AttachMenu(GetMenu());
//// load command bar images
//cmdBar_.LoadImages(IDR_MAINFRAME);
// remove old menu
SetMenu(NULL);
//HWND hWndToolBar = CreateSimpleToolBarCtrl(m_hWnd, IDR_MAINFRAME, FALSE, ATL_SIMPLE_TOOLBAR_PANE_STYLE);
//CreateSimpleReBar(ATL_SIMPLE_REBAR_NOBORDER_STYLE);
//AddSimpleReBarBand(hWndCmdBar);
//AddSimpleReBarBand(hWndToolBar, NULL, TRUE);
//CreateSimpleStatusBar();
m_hWndClient = view_.Create(m_hWnd, rcDefault);
//UIAddToolBar(hWndToolBar);
//UISetCheck(ID_VIEW_TOOLBAR, 1);
//UISetCheck(ID_VIEW_STATUS_BAR, 1);
// register object for message filtering and idle updates
CMessageLoop* pLoop = _Module.GetMessageLoop();
ATLASSERT(pLoop != NULL);
pLoop->AddMessageFilter(this);
pLoop->AddIdleHandler(this);
// Transfer the specified file.
_beginthread(TransferThread, 0, reinterpret_cast<void *>(this));
return 0;
}
/**
Transfer the specified file and report transfer information.
*/
void CMainFrame::Transfer()
{
static const int MaxBufferSize = 1024;
TCHAR buffer[MaxBufferSize]; // Message buffer to store server response information.
DWORD errorCode; // Error code returned by server.
DWORD length = MaxBufferSize; // The max length of the buffer.
BOOL ret; // The return value of ftp operations.
// Load config from file.
ConfigManager config(IDS_PROFILE_FILENAME);
CString domain = config(_T("FTP Address"), _T("domain"));
CString user = config(_T("FTP Address"), _T("user"));
CString password = config(_T("FTP Address"), _T("password"));
CString transferType = config(_T("FTP Address"), _T("transfer-type"));
CString localFile = config(_T("Local File"), _T("filename"));
CString fileType = config(_T("Local File"), _T("file-type"));
CString remotePath = config(_T("Remote Path"), _T("path"));
ATLTRACE(domain + _T(";") + user + _T(";") + password + _T("\n"));
ATLTRACE(_T("Local filename: ") + localFile + _T("\n"));
ATLTRACE(_T("Remote path: ") + remotePath + _T("\n"));
ATLTRACE(_T("Transfer type: ") + transferType + _T("\n"));
// If config file is exist.
if (::PathFileExists(config.Filename()))
{
ReportOperation(_T("Load config file: ") + config.Filename());
}
else
{
ReportException(_T("Fail to open config file \"") + config.Filename() + _T("\"!"));
return;
}
// If domain is empty, domain name is invalid.
if (domain.IsEmpty())
{
ReportException(_T("Domain name is empty!"));
return;
}
// If local file is not exist, report exception.
if (!::PathFileExists(localFile))
{
ReportException(_T("Local file is not exist!"));
return;
}
// Remote filename is the same as local file.
int left = localFile.ReverseFind(_T('\\')) + 1;
CString remoteFile = localFile.Mid(left, localFile.GetLength() - left);
ATLTRACE(_T("Remote filename: ") + remoteFile + _T("\n"));
// Try to create a internet session.
CString agent;
agent.LoadString(IDR_MAINFRAME);
HINTERNET session = ::InternetOpen(agent, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
if (!session)
{
ReportException(_T("Fail to create a new internet session!"));
return;
}
// Try to connect to ftp server.
ReportOperation(_T("Try to connect \"") + domain + _T("\" with username \"") + user + _T("\"."));
ReportClientToServer(_T("Login as \"") + user + _T("\"."));
int flag = (transferType.CompareNoCase(_T("port")) ? INTERNET_FLAG_PASSIVE : 0);
HINTERNET ftp = ::InternetConnect(session, domain, INTERNET_DEFAULT_FTP_PORT,
user, password, INTERNET_SERVICE_FTP, flag, 0);
if (!ftp)
{
ReportException(_T("Fail to connect to FTP server. Do you use right username and password?"));
return;
}
// Get welcome message.
length = MaxBufferSize;
::InternetGetLastResponseInfo(&errorCode, buffer, &length);
ReportServerToClient(buffer);
ReportOperation(_T("Remote FTP server is connected!"));
// Change current directory.
ReportOperation(_T("Try to change current directory to \"") + remotePath + _T("\"."));
ret = ::FtpSetCurrentDirectory(ftp, remotePath);
length = MaxBufferSize;
::InternetGetLastResponseInfo(&errorCode, buffer, &length);
ReportServerToClient(buffer);
if (!ret)
{
// Fail to change current directory.
ReportException(_T("Fail to change current directory. Is that a right directory?"));
return;
}
// Upload file to server.
ReportOperation(_T("Try to upload file \"") + localFile + _T("\" to server."));
ReportClientToServer(_T("Uploading..."));
int type = fileType.CompareNoCase(_T("ascii")) ? FTP_TRANSFER_TYPE_BINARY : FTP_TRANSFER_TYPE_ASCII;
ret = ::FtpPutFile(ftp, localFile, remoteFile, type, 0);
length = MaxBufferSize;
::InternetGetLastResponseInfo(&errorCode, buffer, &length);
ReportServerToClient(buffer);
if (!ret)
{
// Fail to upload file.
ReportException(_T("Fail to upload file to server. Do you have enough access right?"));
return;
}
// Close internet session.
::InternetCloseHandle(ftp);
::InternetCloseHandle(session);
ReportOperation(_T("Congratulations! Transfer successfully!"));
}
//
//LRESULT CMainFrame::OnFileExit(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
//{
// PostMessage(WM_CLOSE);
// return 0;
//}
//
//LRESULT CMainFrame::OnFileNew(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
//{
// // TODO: add code to initialize document
//
// return 0;
//}
//
//LRESULT CMainFrame::OnViewToolBar(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
//{
// static BOOL bVisible = TRUE; // initially visible
// bVisible = !bVisible;
// CReBarCtrl rebar = m_hWndToolBar;
// int nBandIndex = rebar.IdToIndex(ATL_IDW_BAND_FIRST + 1); // toolbar is 2nd added band
// rebar.ShowBand(nBandIndex, bVisible);
// UISetCheck(ID_VIEW_TOOLBAR, bVisible);
// UpdateLayout();
// return 0;
//}
//
//LRESULT CMainFrame::OnViewStatusBar(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
//{
// BOOL bVisible = !::IsWindowVisible(m_hWndStatusBar);
// ::ShowWindow(m_hWndStatusBar, bVisible ? SW_SHOWNOACTIVATE : SW_HIDE);
// UISetCheck(ID_VIEW_STATUS_BAR, bVisible);
// UpdateLayout();
// return 0;
//}
//
//LRESULT CMainFrame::OnAppAbout(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
//{
// CAboutDlg dlg;
// dlg.DoModal();
// return 0;
//}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -