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

📄 awebbrowserdlg.cpp

📁 使用webbrowser控件
💻 CPP
字号:
// aWebBrowserDlg.cpp : implementation file
//

#include "stdafx.h"
#include "aWebBrowser.h"
#include "aWebBrowserDlg.h"

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

#ifndef __AFXEXT_H__
#include <afxext.h>
#endif
 #include <afxhtml.h>
#include "Mshtml.h"
#include <io.h>
#include <fcntl.h>
#include <SYS\STAT.H>
#include <stdlib.h>
#include "Base64.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAWebBrowserDlg dialog

CAWebBrowserDlg::CAWebBrowserDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CAWebBrowserDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAWebBrowserDlg)
	//m_strUrl = _T("www.google.com");
	m_strUrl = _T("http://192.168.13.175:8080/ServletTest/pangbing");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CAWebBrowserDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAWebBrowserDlg)
	DDX_Control(pDX, IDC_PROGRESS1, m_progress);
	DDX_Control(pDX, IDC_EXPLORER1, m_WebBrowser);
	DDX_Text(pDX, IDC_EDIT1, m_strUrl);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAWebBrowserDlg, CDialog)
	//{{AFX_MSG_MAP(CAWebBrowserDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAWebBrowserDlg message handlers

BOOL CAWebBrowserDlg::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
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CAWebBrowserDlg::OnButton1() 
{
	DWORD nFlags;

	UpdateData(TRUE);
	nFlags = navNoWriteToCache | navNoReadFromCache;
    this->m_WebBrowser.Navigate(m_strUrl,NULL,NULL,NULL,NULL);
	
}

BEGIN_EVENTSINK_MAP(CAWebBrowserDlg, CDialog)
    //{{AFX_EVENTSINK_MAP(CAWebBrowserDlg)
	ON_EVENT(CAWebBrowserDlg, IDC_EXPLORER1, 250 /* BeforeNavigate2 */, OnBeforeNavigate2Explorer1, VTS_DISPATCH VTS_PVARIANT VTS_PVARIANT VTS_PVARIANT VTS_PVARIANT VTS_PVARIANT VTS_PBOOL)
	ON_EVENT(CAWebBrowserDlg, IDC_EXPLORER1, 259 /* DocumentComplete */, OnDocumentCompleteExplorer1, VTS_DISPATCH VTS_PVARIANT)
	ON_EVENT(CAWebBrowserDlg, IDC_EXPLORER1, 108 /* ProgressChange */, OnProgressChangeExplorer1, VTS_I4 VTS_I4)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

void CAWebBrowserDlg::OnBeforeNavigate2Explorer1(LPDISPATCH pDisp, VARIANT FAR* URL, VARIANT FAR* Flags, VARIANT FAR* TargetFrameName, VARIANT FAR* PostData, VARIANT FAR* Headers, BOOL FAR* Cancel) 
{
	// TODO: Add your control notification handler code here
	//TRUE:停止FALSE:继续添加测试代码
// BSTR str = URL->bstrVal;
// char s[256];
// for(int i=0;str;++i)
// s = (char)str;
// s = 0;
// *Cancel = TRUE;
//	 MessageBoxW(m_hWnd,URL->bstrVal,0,0);

	///more
	///获取接口,更改HTML内容[color=crimson]OnButton2() 
	//{
	// TODO: Add your control notification handler code here
	IHTMLDocument2* pHTMLFocument2;
	LPDISPATCH lpDispatch;
	lpDispatch = m_WebBrowser.GetDocument();
	if(lpDispatch)
	{
		HRESULT hr;
		hr = lpDispatch->QueryInterface(IID_IHTMLDocument2,(LPVOID*)&pHTMLFocument2);
		lpDispatch->Release();
		IHTMLElement* pBody;
		hr = pHTMLFocument2->get_body(&pBody);
		UpdateData(TRUE);
		BSTR bstr = m_strUrl.AllocSysString();
		// pBody->get_innerHTML(bstr);
		pBody->put_innerHTML(bstr); 
		SysFreeString(bstr);
		pBody->Release();
	}

}

void CAWebBrowserDlg::OnDocumentCompleteExplorer1(LPDISPATCH pDisp, VARIANT FAR* URL) 
{
		IHTMLDocument2* pHTMLFocument2;
	LPDISPATCH lpDispatch;
	lpDispatch = m_WebBrowser.GetDocument();
	if(lpDispatch)
	{
		HRESULT hr;
		hr = lpDispatch->QueryInterface(IID_IHTMLDocument2,(LPVOID*)&pHTMLFocument2);
		lpDispatch->Release();
		IHTMLElement* pBody;
		hr = pHTMLFocument2->get_body(&pBody);
		UpdateData(TRUE);
		BSTR bstr = m_strUrl.AllocSysString();
		 pBody->get_innerHTML(&bstr);


         int OutputFile;
         CString szBody = _T(bstr);
		 szBody.Remove('\r');
		 szBody.Remove('\n');
		 Base64Decode(szBody,"c:\\aaa.zip");

//		
//
//
//		OutputFile = _open("c:\\aaa.zip",_O_BINARY|_O_WRONLY |_O_TRUNC |_O_CREAT,_S_IWRITE);
//		if ( OutputFile == -1 )
//		{
//			_close(OutputFile);
//			return ;
//		}
//
//		_write(OutputFile,szBody,strlen(szBody) );
//
//  		_close(OutputFile);
	
		//pBody->put_innerHTML(bstr); 
		SysFreeString(bstr);
		pBody->Release();
	}
	
}

BOOL CAWebBrowserDlg::Base64Decode(const char* p_pSourceBuff, const char* p_pDecodedFileName)
{
	CBase64 Base64;

	int OutputFile;

	OutputFile = _open(p_pDecodedFileName,_O_BINARY|_O_WRONLY |_O_TRUNC |_O_CREAT,_S_IWRITE);
	if ( OutputFile == -1 ){
		return false;
	}

	char *pOutputBuffer = (char*) malloc (strlen(p_pSourceBuff));

	unsigned int DecodedBytes = 0;

	DecodedBytes = Base64.DecodeBuffer ((char*)p_pSourceBuff, pOutputBuffer);
	_write(OutputFile,pOutputBuffer,DecodedBytes );

	_close(OutputFile);
	return 0;
//	BOOL bRtn = FALSE;
//
//	HINSTANCE hDLL;               // Handle to DLL
//	typedef unsigned int (CALLBACK* LPDecodeBuff)(LPCSTR p_pSourceFileName,LPCSTR p_pEncodedFileName);
//	LPDecodeBuff lpDecodeBuff;    // Function pointer
// 
//    CString g_WorkPath="c:\\aWebBrowser\\";
//	hDLL = LoadLibrary(g_WorkPath + "Base64.dll");
//	if (hDLL != NULL){
//		lpDecodeBuff = (LPDecodeBuff)GetProcAddress(hDLL,"DecodeBuff");
//		if ( lpDecodeBuff ){
//			if( lpDecodeBuff(p_pSourceBuff,(LPCSTR)szDesFilename) == 0 ){
//				bRtn = TRUE;
//			}
//		}
//		FreeLibrary(hDLL);
//	}
//
// 	return bRtn;
}

void CAWebBrowserDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
   // CHtmlView view;	
}

void CAWebBrowserDlg::OnProgressChangeExplorer1(long Progress, long ProgressMax) 
{
	// TODO: Add your control notification handler code here
    m_progress.SetRange(0,ProgressMax);
	m_progress.SetPos(Progress);
}

⌨️ 快捷键说明

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