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

📄 downieeedlg.cpp

📁 IEEEXplore自动抢线、批量论文下载工具
💻 CPP
字号:
// DownIEEEDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DownIEEE.h"
#include "DownIEEEDlg.h"
//#include <iostream.h>
#include <fstream.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CEdit m_wURL;
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

CWinThread *pAutoThreadHandel;

BOOL bKeepConnect;

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

/////////////////////////////////////////////////////////////////////////////
// CDownIEEEDlg dialog

CDownIEEEDlg::CDownIEEEDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDownIEEEDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDownIEEEDlg)
	m_sPath = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_pParent=pParent;
	m_sPath="";
}

void CDownIEEEDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDownIEEEDlg)
	DDX_Control(pDX, IDC_BUTTON1, m_wButtonStart);
	DDX_Control(pDX, IDC_STATIC_OUT, m_wOut);
	DDX_Control(pDX, IDC_EDIT2, m_wSavePath);
	DDX_Control(pDX, IDC_PROGRESS1, m_wProgress);
	DDX_Text(pDX, IDC_EDIT2, m_sPath);
	DDX_Control(pDX, IDC_EDIT1, m_wURL);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDownIEEEDlg, CDialog)
	//{{AFX_MSG_MAP(CDownIEEEDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_WM_CLOSE()
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_USER_TRAY_NOTIFICATION, OnNotifyIcon)
	ON_BN_CLICKED(IDM_EXIT, OnClose)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDownIEEEDlg message handlers

BOOL CDownIEEEDlg::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
//	AddTray();

//获取的html文章头样本:

//	char *pHtml="<strong>1.</strong> <div align=\"right\"></div></td><td width=\"96%\" class=\"bodyCopyBlackLargeSpaced\" valign=\"middle\">		<strong>Probability of error calculation of OFDM systems with \"frequency\\time\" offset</strong> Full Text: <a href=\"/iel5/26/20855/00966051.pdf?tp=&arnumber=966051&isnumber=20855\" class=\"bodyCopy\">PDF</a>";
//	FindArticleTitlePos(0,pHtml);

	
	// TODO: Add extra initialization here

//  默认路径

	ifstream iss("config.txt");
	char sPath[MAX_PATH]={0};
	if (!iss.fail())
	{
		iss>>sPath;
	}
	else
	{
		GetCurrentDirectory(200,m_sPath.GetBuffer(200));
		m_sPath.ReleaseBuffer();
	}
	iss.close();
	m_sPath=sPath;


	m_wSavePath.SetWindowText(m_sPath);
	m_wURL.SetWindowText("http://");

	bKeepConnect=TRUE;
	pAutoThreadHandel=AfxBeginThread(AutoConnectIEEE,this);
	
//	SetParent(FindWindow("ProgMan",NULL));
//	ShowWindow(SW_SHOW);
//   ShowWindow(SW_MINIMIZE);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CDownIEEEDlg::OnButton1() 
{
	CString s;
	m_wURL.GetWindowText(s);
	if (s=="htt://")
	{
		AfxMessageBox("输入页面地址");
		return;
	}
	
	m_wButtonStart.EnableWindow(FALSE);
	AfxBeginThread(DownIEEE,this);
}

int CDownIEEEDlg::DownSecurity(CStdioFile * pUrlFile, char* pChar, BOOL bDisplay)
{
	int i=0, nReadLen=0;
	while((i=(pUrlFile->Read(pChar+nReadLen,200)))>=1)
	{
//		if(bDisplay)
//		{
//			AppendText(pChar+nReadLen);
//		}
		nReadLen+=i;
	}
	return nReadLen;
}

BOOL CDownIEEEDlg::OnButton2() 
{
	char			lpszPath[MAX_PATH];
	LPMALLOC		lpMalloc;
	BROWSEINFO		sInfo;
	LPITEMIDLIST	lpidlRoot;
	LPITEMIDLIST	lpidlBrowse;
	memset(lpszPath,0,MAX_PATH);

	if(::SHGetMalloc(&lpMalloc) != NOERROR)
		return FALSE;

	if(m_sPath=="")
	{
		::GetCurrentDirectory(MAX_PATH,m_sPath.GetBuffer(MAX_PATH));
		m_sPath.ReleaseBuffer();
	}
	
	::SHGetSpecialFolderLocation(NULL, CSIDL_DRIVES, &lpidlRoot);	//取得选定的文件夹名

	::ZeroMemory(&sInfo, sizeof(BROWSEINFO));
	sInfo.pidlRoot		= lpidlRoot;
	sInfo.pszDisplayName = lpszPath;
	sInfo.lpszTitle		= _T("选择文件夹");
	sInfo.ulFlags		= BIF_RETURNONLYFSDIRS;
	sInfo.ulFlags	|= BIF_EDITBOX | BIF_NEWDIALOGSTYLE | BIF_USENEWUI;
	sInfo.lpfn			= BrowseCtrlCallback;
	sInfo.lParam		= (LPARAM)m_sPath.GetBuffer(MAX_PATH);
	m_sPath.ReleaseBuffer();
	
	lpidlBrowse = ::SHBrowseForFolder(&sInfo);			//显示文件夹选择对话框
	
	if(lpidlBrowse != NULL)
	{
		if(::SHGetPathFromIDList(lpidlBrowse,lpszPath))	//取得文件夹名		
		{
			m_sPath = lpszPath;
			ofstream oss("config.txt");
			if (!oss.fail())
			{
				oss<<m_sPath;
			}
			oss.close();
		}
		::CoTaskMemFree(lpidlBrowse);
	}
	else
	{
		lpMalloc->Release();
		return FALSE;
	}
	
	if(lpidlRoot != NULL)
		::CoTaskMemFree(lpidlRoot);
	
	lpMalloc->Release();
	m_wSavePath.SetWindowText(m_sPath);
	return TRUE;
}

int CALLBACK BrowseCtrlCallback(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
	if(uMsg == BFFM_INITIALIZED)
		::SendMessage(hwnd, BFFM_SETSELECTION, TRUE, lpData);
	
	return	0;
}

UINT DownIEEE(LPVOID pParam)
{
	CString sURL;
	CDownIEEEDlg* pDlg=(CDownIEEEDlg*)pParam;
	CFile *fout=new CFile;
//	CStdioFile * pPicOutFile;
	CInternetSession isSession;
	CStdioFile * pHtmlFile;

	m_wURL.GetWindowText(sURL);
	char *pHtml=(char *)malloc(1000000*sizeof(char));
	char *pOut=(char*)malloc(2000000*sizeof(char));
	memset(pHtml,0,1000000);
	memset(pOut,0,2000000);
	int nPics=0;

	pDlg->m_wSavePath.GetWindowText(pDlg->m_sPath);

	pDlg->m_wProgress.SetRange(1,99);
	pDlg->m_wProgress.SetStep(3);

	if(sURL=="")
	{
		AfxMessageBox("请输入网页的URL,方法如下:\n在需要下载图片的网页上点右键,选属性,复制url粘贴至此",IDOK);
		return 0;
	}
	
	pHtmlFile=isSession.OpenURL(sURL,1,INTERNET_FLAG_TRANSFER_BINARY);
	int nTextLen=pHtmlFile->GetLength();
	
	int nTextReadLen=pDlg->DownSecurity(pHtmlFile,pHtml,TRUE);


//	AppendText("\n\nHtml Gotten.\n");

	if(pDlg->OnButton2())
	{
		CString sSearch(pHtml);
	}
//	m_wURL.SetWindowText(sSearch);

	isSession.Close();

//获取的html文章头样本:
//		<strong>1.</strong> <div align="right"></div></td><td width="96%" class="bodyCopyBlackLargeSpaced" valign="middle">
//		<strong>Probability of error calculation of OFDM systems with "frequency/time" offset</strong>


//对应的文章样本
//		Full Text: <a href="/iel5/26/20855/00966051.pdf?tp=&arnumber=966051&isnumber=20855" class="bodyCopy">PDF</a>

	int nStart=-1, nEnd=0;
	CString sNewTempURL,sNewURL;
	CString temp;
	int nXie1=0;
	CString sCharactor;
	BOOL bFullURL=FALSE;
	
	nStart=pDlg->FindArticleTitlePos(0,pHtml);
	return	0;
}


int CDownIEEEDlg::FindNoCase(CString sSrc, CString s2Find1, CString s2Find2, int nStart)
{
	int nLower=sSrc.Find(s2Find1,nStart);
	int nUpper=sSrc.Find(s2Find2,nStart);

	if(nLower<nStart)
		nStart=nUpper;
	else
	{
		if(nUpper<nStart)
			nStart=nLower;
		else
			nStart=min(nLower,nUpper);
	}
	return nStart;
}

int CDownIEEEDlg::CheckValid(CString& sFN)
{
	char* pInvalid="\\/:*?\"><|";
	int nInvalidPos=-1;
	for(int i=0; i<9; i++)
	{
		while((nInvalidPos=sFN.Find(pInvalid[i]))>=0)
		{
			CString sRight=(nInvalidPos>=sFN.GetLength())?(""):sFN.Mid(nInvalidPos+1);
			sFN=sFN.Mid(0,nInvalidPos-1)+sRight;
		}
	}
	return nInvalidPos;
}

void AppendText(CString s)
{
	m_wURL.SetSel(99999,99999);
	m_wURL.ReplaceSel(s);
}

int CDownIEEEDlg::FindArticleTitlePos(int nStart, const char *sHTML)
{
	CString sSrc(sHTML+nStart);
	int nLen=sSrc.GetLength();
	int nOffset=0;
	char sNum[4]={0};
	int nNum=0;
	CStdioFile * pURLOutFile;

	while (nOffset<nLen&&nOffset>=0)
	{
		nOffset=sSrc.Find("<strong>",nOffset+1);
		if (nOffset<0)
		{
			return -1;
		}
		memcpy(sNum,sHTML+nStart+nOffset+8,3);
		sscanf(sNum,"%d",&nNum);
		BOOL bMatch=FALSE;
		if (nNum>0&&nNum<=9)//说明是数字
		{
			if (CString(sHTML+nStart+nOffset+9).Find(".</strong>")==0)
			{
				bMatch=TRUE;
			}
		}
		else if (nNum>=10&&nNum<=25)//说明是数字
		{
			if (CString(sHTML+nStart+nOffset+10).Find(".</strong>")==0)
			{
				bMatch=TRUE;
			}
		}
		if (bMatch)//找到文件名标记,找文件名,由<strong></strong>标记
		{
			nOffset=sSrc.Find("<strong>",nOffset+16);
			int nOffset1=sSrc.Find("</strong>",nOffset);
			CString sName=sSrc.Mid(nOffset+8,nOffset1-nOffset-8);
			ValidateFileName(&sName);
			sName=m_sPath+"\\"+sName+".pdf";
			//找文件链接
			nOffset=sSrc.Find("Full Text:",nOffset+16);
			nOffset=sSrc.Find("href=\"",nOffset+8);
			nOffset1=sSrc.Find("\"",nOffset+8);
			CString sFileURL=sSrc.Mid(nOffset+6,nOffset1-nOffset-6);
			sFileURL="http://ieeexplore.ieee.org"+sFileURL;

			CInternetSession isFileSession;
			pURLOutFile=isFileSession.OpenURL(sFileURL,1,INTERNET_FLAG_TRANSFER_BINARY);
			int nFileLen=pURLOutFile->GetLength();

			int nFileReadLen=0;
			BYTE pB[10001]={0};
			CFile fl;
//			AppendText("\n"+sName+"\n");
			m_wOut.SetWindowText(sName);
			
			fl.Open(sName,CFile::modeCreate|CFile::modeWrite);

			int nRead;
			while((nRead=pURLOutFile->Read(pB,10000))>=1)
			{
				fl.Write(pB,nRead);
				nFileReadLen+=nRead;
				m_wProgress.StepIt();
			}
			fl.Close();
			pURLOutFile->Close();
			isFileSession.Close();
		}
	}
	m_wProgress.SetPos(0);
	return nOffset;
}

void CDownIEEEDlg::ValidateFileName(CString *pName)
{
	int nLen=pName->GetLength();
	for (int i=0; i<nLen; i++)
	{
		char c=pName->GetAt(i);
		if (c=='\\'||c=='/'||c==':'||c=='*'||c=='?'||c=='\"'||c=='>'||c=='<'||c=='|'||c<32||c>127)
		{
			pName->SetAt(i,'_');
		}
	}
}


UINT AutoConnectIEEE(LPVOID pParam)
{
	CDownIEEEDlg* pDlg=(CDownIEEEDlg*)pParam;
	CInternetSession isSession;
	CStdioFile * pHtmlFile;
	while (bKeepConnect)
	{
		AppendText("\n\r\n\r自动刷新...");
		pHtmlFile=isSession.OpenURL("http://ieeexplore.ieee.org/iel5/26/29703/01350936.pdf?tp=&arnumber=1350936&isnumber=29703",1,INTERNET_FLAG_TRANSFER_BINARY);
		char cin[11]={0};
		pHtmlFile->Read(cin,8);
		int nWait=0;
		if (CString(cin)==CString("%PDF-1.3"))
		{
			AppendText("在线。2分钟后刷新\r\n");
			nWait=1000*2*60;
		}
		else
		{
			AppendText("掉线。1分钟后重试\r\n");
			nWait=1000*1*60;
		}
		pHtmlFile->Close();
		Sleep(nWait);//每5分钟自动刷新一下保持激活
	}
	return 0;
}


void CDownIEEEDlg::OnNotifyIcon(WPARAM wParam, LPARAM lParam)
{
    CMenu menu, *pSubMenu;
    CWnd* pTarget = AfxGetMainWnd();
	// 响应在托盘图标上的单击
	if ((wParam == IDR_MAINFRAME)&&(lParam == WM_LBUTTONDOWN))
	{
		ShowWindow(SW_SHOW);
	}
	if ((wParam == IDR_MAINFRAME)&&(lParam == WM_RBUTTONDOWN))
	{
        if (!menu.LoadMenu(IDR_MENU1)) return;
        if (!(pSubMenu = menu.GetSubMenu(0))) return;

        CPoint pos;
        GetCursorPos(&pos);

        pTarget->SetForegroundWindow();  
        ::TrackPopupMenu(pSubMenu->m_hMenu, 0, pos.x, pos.y, 0, 
                         pTarget->GetSafeHwnd(), NULL);

        pTarget->PostMessage(WM_NULL, 0, 0);

        menu.DestroyMenu();		
	}
}

void CDownIEEEDlg::AddTray()
{
	nd.cbSize	= sizeof (NOTIFYICONDATA);
	nd.hWnd	= m_hWnd;
	nd.uID	= IDR_MAINFRAME;
	nd.uFlags	= NIF_ICON|NIF_MESSAGE|NIF_TIP;
	nd.uCallbackMessage= WM_USER_TRAY_NOTIFICATION;
	nd.hIcon	= m_hIcon;

	strcpy(nd.szTip, "anfysky");
	
	Shell_NotifyIcon(NIM_ADD, &nd);
}

void CDownIEEEDlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	bKeepConnect=FALSE;
	::TerminateThread(pAutoThreadHandel,0);
	DestroyWindow();

	CDialog::OnClose();
}

void CDownIEEEDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	Shell_NotifyIcon(NIM_DELETE, &nd);
	
	// TODO: Add your message handler code here
	
}

⌨️ 快捷键说明

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