📄 mgetpicdlg.cpp
字号:
// mgetpicDlg.cpp : implementation file
//
#include "stdafx.h"
#include "mgetpic.h"
#include "mgetpicDlg.h"
#include "direct.h"
#include "inifile.h"
#include "dirdialog.h"
#include "tear.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
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()
/////////////////////////////////////////////////////////////////////////////
// CMgetpicDlg dialog
CMgetpicDlg::CMgetpicDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMgetpicDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMgetpicDlg)
m_sProxyIP = _T("");
m_nProxyType = -1;
m_nProxyPort = 0;
m_sHtmlPath = _T("");
m_sPicPath = _T("");
m_nPage0 = 1;
m_nPage1 = 0;
m_sUrl = _T("");
m_nWildCardSize = 1;
m_sMsg = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMgetpicDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMgetpicDlg)
DDX_Text(pDX, IDC_PROXY_IP, m_sProxyIP);
DDX_CBIndex(pDX, IDC_PROXY_TYPE, m_nProxyType);
DDX_Text(pDX, IDC_PROXY_PORT, m_nProxyPort);
DDX_Text(pDX, IDC_HTML_PATH, m_sHtmlPath);
DDX_Text(pDX, IDC_PIC_HTML, m_sPicPath);
DDX_Text(pDX, IDC_PAGE0, m_nPage0);
DDX_Text(pDX, IDC_PAGE1, m_nPage1);
DDX_Text(pDX, IDC_URL, m_sUrl);
DDX_Text(pDX, IDC_WILDCARD_SIZE, m_nWildCardSize);
DDX_Text(pDX, IDC_MSG, m_sMsg);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMgetpicDlg, CDialog)
//{{AFX_MSG_MAP(CMgetpicDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_DOWN, OnDown)
ON_WM_CLOSE()
ON_BN_CLICKED(IDC_SAVE_PARA, OnSavePara)
ON_BN_CLICKED(IDC_HTML_SEL, OnHtmlSel)
ON_BN_CLICKED(IDC_PIC_SEL, OnPicSel)
ON_BN_CLICKED(IDC_DOWN2, OnDown2)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMgetpicDlg message handlers
BOOL CMgetpicDlg::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
// TODO: Add extra initialization here
TCHAR szPath[MAX_PATH];
::GetModuleFileName(NULL,szPath,MAX_PATH);
CString s=szPath;
m_sRunPath=s.Left(s.ReverseFind('\\'));
s=m_sRunPath+"\\htmls";
_mkdir(s);
s=m_sRunPath+"\\pics";
_mkdir(s);
ReadPara();
TE_InitLibrary();
return TRUE; // return TRUE unless you set the focus to a control
}
void CMgetpicDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CMgetpicDlg::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 CMgetpicDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMgetpicDlg::OnDown()
{
UpdateData();
CString s1,s2;
COleDateTime dt=COleDateTime::GetCurrentTime();
s1.Format("%d:%d:%d",dt.GetHour(),dt.GetMinute(),dt.GetSecond());
if(m_nProxyType!=0)
down.SetProxy(m_sProxyIP,m_nProxyPort,TRUE,FALSE,NULL,NULL,m_nProxyType);
CFileFind ff;
BOOL bWork=ff.FindFile(m_sHtmlPath+"\\topic*");
CString sPathFile,s,local;
CStringArray saImages;
int n;
while(bWork)
{
saImages.RemoveAll();
bWork=ff.FindNextFile();
sPathFile=ff.GetFilePath();
ParseHtml(sPathFile,saImages);
for(int i=0;i<saImages.GetSize();i++)
{
s=saImages[i];
n=s.ReverseFind('/');
local=s.Mid(n+1);
down.Download(s,m_sPicPath+"\\"+local);
}
DeleteFile(sPathFile);
}
dt=COleDateTime::GetCurrentTime();
s2.Format("%d:%d:%d",dt.GetHour(),dt.GetMinute(),dt.GetSecond());
AfxMessageBox("下载完成!"+s1+"---"+s2);
}
void CMgetpicDlg::OnClose()
{
TE_CleanupLibrary();
CDialog::OnClose();
}
void CMgetpicDlg::ParseHtml(CString sPathFile, CStringArray &saImags)
{
CStdioFile f;
//http://bbs.billwang.net/forum/attach
int n,n1;
CString s;
f.Open(sPathFile,CStdioFile::modeRead);
while(1)
{
if(!f.ReadString(s))
break;
n=s.Find("http://bbs.billwang.net/forum/attach",0);
if(n>=0)
{
n1=s.Find("'",n);
s=s.Mid(n,n1-n);
saImags.Add(s);
}
}
f.Close();
}
void CMgetpicDlg::OnSavePara()
{
UpdateData();
CIniFile f;
f.SetFileName(m_sRunPath+"\\mgetpic.ini");
f.WriteInt("PROXY","Type",m_nProxyType);
f.WriteString("PROXY","IP",m_sProxyIP);
f.WriteInt("PROXY","Port",m_nProxyPort);
f.WriteString("PATH","HtmlS",m_sHtmlPath);
f.WriteString("PATH","Pics",m_sPicPath);
}
void CMgetpicDlg::OnHtmlSel()
{
UpdateData();
CDirDialog dd;
if(dd.DoModal()==IDOK)
{
m_sHtmlPath=dd.GetPathname();
UpdateData(FALSE);
}
}
void CMgetpicDlg::OnPicSel()
{
UpdateData();
CDirDialog dd;
if(dd.DoModal()==IDOK)
{
m_sPicPath=dd.GetPathname();
UpdateData(FALSE);
}
}
void CMgetpicDlg::ReadPara()
{
CIniFile f;
f.SetFileName(m_sRunPath+"\\mgetpic.ini");
m_nProxyType=f.ReadInt("PROXY","Type",0);
m_sProxyIP=f.ReadString("PROXY","IP","192.166.3.3");
m_nProxyPort=f.ReadInt("PROXY","Port",8080);
// m_sHtmlPath=f.ReadString("PATH","HtmlS","c:\\downloads");
m_sHtmlPath=f.ReadString("PATH","Htmls",m_sRunPath+"\\htmls");
m_sPicPath=f.ReadString("PATH","Pics",m_sRunPath+"\\pics");
UpdateData(FALSE);
}
BOOL CMgetpicDlg::DownHtml(CString sUrl,CString sFile)
{
/*
if (argv[nIndex][1] == 'D' || argv[nIndex][1] == 'd')
dwAccessType = PRE_CONFIG_INTERNET_ACCESS;
else if (argv[nIndex][1] == 'L' || argv[nIndex][1] == 'l')
dwAccessType = LOCAL_INTERNET_ACCESS;
else if (argv[nIndex][1] == 'S' || argv[nIndex][1] == 's')
bStripMode = TRUE;
else if (argv[nIndex][1] == 'P' || argv[nIndex][1] == 'p')
bProgressMode = TRUE;
else if (argv[nIndex][1] == 'F' || argv[nIndex][1] == 'f')
dwHttpRequestFlags |= INTERNET_FLAG_RELOAD;
*/
int nRetCode = 0;
CTearSession session(_T("TEAR"), dwAccessType);
CHttpConnection* pServer = NULL;
CHttpFile* pFile = NULL;
try
{
CString strServerName;
CString strObject;
INTERNET_PORT nPort;
DWORD dwServiceType;
if (!AfxParseURL(sUrl, dwServiceType, strServerName, strObject, nPort) ||
dwServiceType != INTERNET_SERVICE_HTTP)
{
AfxMessageBox("必须是http开头的地址!");
return FALSE;
}
pServer = session.GetHttpConnection(strServerName, nPort);
pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_GET,
strObject, NULL, 1, NULL, NULL, dwHttpRequestFlags);
pFile->AddRequestHeaders(szHeaders);
pFile->SendRequest();
DWORD dwRet;
pFile->QueryInfoStatusCode(dwRet);
if (dwRet == HTTP_STATUS_DENIED)
{
DWORD dwPrompt;
dwPrompt = pFile->ErrorDlg(NULL, ERROR_INTERNET_INCORRECT_PASSWORD,
FLAGS_ERROR_UI_FLAGS_GENERATE_DATA | FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS, NULL);
if (dwPrompt != ERROR_INTERNET_FORCE_RETRY)
{
//cerr << _T("Access denied: Invalid password\n");
AfxMessageBox("拒绝访问:非法口令!");
return FALSE;
}
pFile->SendRequest();
pFile->QueryInfoStatusCode(dwRet);
}
CString strNewLocation;
pFile->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF, strNewLocation);
if (dwRet == HTTP_STATUS_MOVED ||
dwRet == HTTP_STATUS_REDIRECT ||
dwRet == HTTP_STATUS_REDIRECT_METHOD)
{
CString strNewLocation;
pFile->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF, strNewLocation);
int nPlace = strNewLocation.Find(_T("Location: "));
if (nPlace == -1)
{
//cerr << _T("Error: Site redirects with no new location") << endl;
AfxMessageBox("站点重定位没有设置目标地址!");
return FALSE;
}
strNewLocation = strNewLocation.Mid(nPlace + 10);
nPlace = strNewLocation.Find('\n');
if (nPlace > 0)
strNewLocation = strNewLocation.Left(nPlace);
pFile->Close();
delete pFile;
pServer->Close();
delete pServer;
if (!AfxParseURL(strNewLocation, dwServiceType, strServerName, strObject, nPort))
{
//cerr << _T("Error: the redirected URL could not be parsed.") << endl;
AfxMessageBox("无法解析重定位网址!");
return FALSE;
}
if (dwServiceType != INTERNET_SERVICE_HTTP)
{
//cerr << _T("Error: the redirected URL does not reference a HTTP resource.") << endl;
AfxMessageBox("重定位网址引用的并非HTTP资源!");
return FALSE;
}
pServer = session.GetHttpConnection(strServerName, nPort);
pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_GET,
strObject, NULL, 1, NULL, NULL, dwHttpRequestFlags);
pFile->AddRequestHeaders(szHeaders);
pFile->SendRequest();
pFile->QueryInfoStatusCode(dwRet);
if (dwRet != HTTP_STATUS_OK)
{
//cerr << _T("Error: Got status code ") << dwRet << endl;
AfxMessageBox("无法获得状态码!");
return FALSE;
}
}
TCHAR sz[10240];
CStdioFile f;
CString s;
f.Open(sFile,CStdioFile::modeCreate|CStdioFile::modeWrite);
while (pFile->ReadString(sz, 1023))
{
s=sz;
f.WriteString(s+"\n");
}
f.Close();
// NOTE: Since HTTP servers normally spit back plain text, the
// above code (which reads line by line) is just fine. However,
// other data sources (eg, FTP servers) might provide binary data
// which should be handled a buffer at a time, like this:
/*
while (nRead > 0)
{
sz[nRead] = '\0';
if (bStripMode)
StripTags(sz);
cout << sz;
nRead = pFile->Read(sz, 1023);
}
*/
pFile->Close();
pServer->Close();
}
catch (CInternetException* pEx)
{
// catch errors from WinINet
TCHAR szErr[1024];
pEx->GetErrorMessage(szErr, 1024);
AfxMessageBox(szErr);
nRetCode = 2;
pEx->Delete();
}
catch (CTearException* pEx)
{
// catch things wrong with parameters, etc
nRetCode = pEx->m_nErrorCode;
TRACE1("Error: Exiting with CTearException(%d)\n", nRetCode);
pEx->Delete();
}
if (pFile != NULL)
delete pFile;
if (pServer != NULL)
delete pServer;
session.Close();
return TRUE;
}
void CMgetpicDlg::OnDown2()
{
UpdateData();
if(m_nPage0>m_nPage1)
{
AfxMessageBox("结束页面不正确!");
return;
}
if(m_nProxyType!=0)
down.SetProxy(m_sProxyIP,m_nProxyPort,TRUE,FALSE,NULL,NULL,m_nProxyType);
int count=m_nPage1-m_nPage0;
CString s,local,s1,pg;
CStringArray saImages;
int n,n1;
n=m_sUrl.Find("pg=",0);
if(n<0)
{
AfxMessageBox("找不到标志字符!最后使用第2页的地址!");
return;
}
n1=m_sUrl.Find("&",n);
pg=m_sUrl.Mid(n,n1-n);
for(int i=m_nPage0;i<=m_nPage1;i++)
{
s=m_sUrl;
s1.Format("pg=%d",i);
s.Replace(pg,s1);
s1.Format("%s\\topic_show(%d)",m_sHtmlPath,i);
if(!DownHtml(s,s1))
break;
saImages.RemoveAll();
m_sMsg.Format("正在下载第 %d 页...",i);
UpdateData(FALSE);
ParseHtml(s1,saImages);
for(int j=0;j<saImages.GetSize();j++)
{
s=saImages[j];
n=s.ReverseFind('/');
local=s.Mid(n+1);
down.Download(s,m_sPicPath+"\\"+local);
}
DeleteFile(s1);
}
AfxMessageBox("下载完成!");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -