www.gusucode.com > 获取网页中的密码和文本输入框的内容C++源码程序 > 获取网页中的密码和文本输入框的内容/AutoRefresh/AutoRefresh/AutoRefreshDlg.cpp

    // AutoRefreshDlg.cpp : implementation file
//

#include "stdafx.h"
#include "AutoRefresh.h"
#include "AutoRefreshDlg.h"
#include <atlbase.h>
#include <Mshtml.h>
#include <winuser.h>
#include <comdef.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()

/////////////////////////////////////////////////////////////////////////////
// CAutoRefreshDlg dialog
CAutoRefreshDlg::CAutoRefreshDlg(CWnd* pParent /*=NULL*/)
: CDialog(CAutoRefreshDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAutoRefreshDlg)
	m_uFreq = 1;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	
	m_spSHWinds = NULL;
	m_uTimerRefresh = 0;
}

void CAutoRefreshDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAutoRefreshDlg)
	DDX_Control(pDX, IDC_LIST_IE, m_ctrlIE);
	DDX_Text(pDX, IDC_EDIT_FREQ, m_uFreq);
	DDV_MinMaxUInt(pDX, m_uFreq, 1, 10000);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAutoRefreshDlg, CDialog)
//{{AFX_MSG_MAP(CAutoRefreshDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_REFRESH, OnButtonRefresh)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_BUTTON_SETTIMER, OnButtonSettimer)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAutoRefreshDlg message handlers

BOOL CAutoRefreshDlg::OnInitDialog()
{
	CoInitialize(NULL);
	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
	if (m_spSHWinds == NULL){
		if (m_spSHWinds.CreateInstance(__uuidof(SHDocVw::ShellWindows)) != S_OK){
			MessageBox("Failed");
			CoUninitialize();
			EndDialog(1);
		}
	}
	
	m_ctrlIE.SetExtendedStyle(LVS_EX_CHECKBOXES|LVS_EX_FLATSB|LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
	m_ctrlIE.InsertColumn(0, "IE", LVCFMT_LEFT, 500, -1);
	OnButtonRefresh();
	
	m_uTimerRefresh = SetTimer(1, m_uFreq*1000, NULL);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CAutoRefreshDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if (nID == SC_CLOSE){
		DoClose();
	}
	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 CAutoRefreshDlg::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 CAutoRefreshDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}


void CAutoRefreshDlg::OnButtonRefresh() 
{
	
	VARIANT id, index;
	CComPtr<IDispatch> spDispatch;
	CComQIPtr<IHTMLDocument2, &IID_IHTMLDocument2> pDoc2;
	CComPtr<IHTMLElement> pElement;
	CComPtr<IHTMLElementCollection> pElementCol;
	CComPtr<IHTMLFormElement> pFormElement;
	CComPtr<IHTMLInputTextElement> pInputElement;
	// TODO: Add your control notification handler code here
	int n = m_ctrlIE.GetItemCount();//GetCount();
	
	for (int i = 0; i < n; i ++){
		IWebBrowser2 *pBrowser = (IWebBrowser2 *)m_ctrlIE.GetItemData(i);
		if (pBrowser){
			pBrowser->Release();
		}
	}
	
	m_ctrlIE.DeleteAllItems();
	
	if (m_spSHWinds){
		int n = m_spSHWinds->GetCount();
		for (int i = 0; i < n; i++){
			_variant_t v = (long)i;
			IDispatchPtr spDisp = m_spSHWinds->Item(v);
			
			
			SHDocVw::IWebBrowser2Ptr spBrowser(spDisp);   //生成一个IE窗口的智能指针
			if (spBrowser){
				
                if (SUCCEEDED(spBrowser->get_Document( &spDispatch)))
					pDoc2 = spDispatch;
				if(pDoc2!=NULL)
				{
					{
						if (SUCCEEDED(pDoc2->get_forms(&pElementCol)))
						{
							//	AfxMessageBox("IHTMLElementCollection");
							long p=0;
							if(SUCCEEDED(pElementCol->get_length(&p)))
							if(p!=0)
							{   
								// AfxMessageBox("1");
								for(long i=0;i<=(p-1);i++)
								{
									
									V_VT(&id) = VT_I4;
									V_I4(&id) = i;
									V_VT(&index) = VT_I4;
									V_I4(&index) = 0;
									
									if(SUCCEEDED(pElementCol->item(id,index, &spDispatch)))
										// AfxMessageBox("2");
										if(SUCCEEDED(spDispatch->QueryInterface(IID_IHTMLFormElement,(void**)&pFormElement)))
										{
											// AfxMessageBox("IHTMLFormElement");
											long q=0;
											if(SUCCEEDED(pFormElement->get_length(&q)))
											for(long j=0;j<=(q-1);j++)
											{
												V_VT(&id) = VT_I4;
												V_I4(&id) = j;
												V_VT(&index) = VT_I4;
												V_I4(&index) = 0;
												if(SUCCEEDED(pFormElement->item(id,index, &spDispatch)))
													
													if(SUCCEEDED(spDispatch->QueryInterface(IID_IHTMLInputTextElement,(void**)&pInputElement)))
													{
														//AfxMessageBox("IHTMLInputTextElement");
														CComBSTR value;
														CComBSTR type;
														
														pInputElement->get_type(&type);
														
														CString strtype(type);
														strtype.MakeUpper();
														if(strtype.Find("TEXT")!=-1)
														{
															pInputElement->get_value(&value);
															CString str(value);
															if(!str.IsEmpty())
															m_ctrlIE.InsertItem(0, _bstr_t(value)+_bstr_t("  【可能是用户名或其他需提交的内容】"));
														
														}
													    else if(strtype.Find("PASSWORD")!=-1)
														{
															pInputElement->get_value(&value);
															CString str(value);
															if(!str.IsEmpty())
				                                            m_ctrlIE.InsertItem(0, _bstr_t(value) + _bstr_t("  【应该是密码】"));
														}
													}
											}
										}
								}
							}
						}
						
					}
					
				}
			//	_bstr_t bsName = spBrowser->GetLocationName();   //窗口名称
			//	int nPos = m_ctrlIE.InsertItem(0, bsName);
			//	spBrowser->AddRef();
			//	void * pData = spBrowser;
			//	m_ctrlIE.SetItemData(nPos, (DWORD)(pData));
			}
		}
	}
	
}


void CAutoRefreshDlg::DoClose()
{
	int n = m_ctrlIE.GetItemCount();//GetCount();
	
	for (int i = 0; i < n; i ++){
		IWebBrowser2 *pBrowser = (IWebBrowser2 *)m_ctrlIE.GetItemData(i);
		if (pBrowser){
			pBrowser->Release();
		}
	}
	
	m_ctrlIE.DeleteAllItems();
	
	if (m_spSHWinds){
		m_spSHWinds.Release();
		m_spSHWinds = 0;
	}
	CoUninitialize();
	EndDialog(0);
}

void CAutoRefreshDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if (nIDEvent == 1){
		int n = m_ctrlIE.GetItemCount();
		for (int i = 0; i < n; i++){
			if (m_ctrlIE.GetCheck(i)){
				IWebBrowser2 *pBrowser = (IWebBrowser2 *)m_ctrlIE.GetItemData(i);
				if (pBrowser){
					pBrowser->Refresh();
				}
			}
		}
	}
	OnButtonRefresh();
	CDialog::OnTimer(nIDEvent);
}


void CAutoRefreshDlg::OnButtonSettimer() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	if (m_uTimerRefresh){
		KillTimer(m_uTimerRefresh);
		m_uTimerRefresh = SetTimer(1, 3*1000, NULL);
	}
}