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

📄 webitdlg.cpp

📁 《Windows CE 权威指南》(作者:(美)CHRIS MUENCH
💻 CPP
字号:
// WebItDlg.cpp : implementation file
//

#include "stdafx.h"
#include "WebIt.h"
#include "WebItDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CWebItDlg dialog

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

void CWebItDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CWebItDlg)
	DDX_Control(pDX, IDC_PAGE, m_Page);
	DDX_Control(pDX, IDC_HEADER, m_Header);
	DDX_Text(pDX, IDC_URL, m_URL);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CWebItDlg, CDialog)
	//{{AFX_MSG_MAP(CWebItDlg)
	ON_BN_CLICKED(IDC_GO, OnGo)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWebItDlg message handlers

BOOL CWebItDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 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
	
	CenterWindow(GetDesktopWindow());	// center to the hpc screen

	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}


// <BOOK_ADDON Chapter 9.7.2> *******************************************
void CWebItDlg::OnGo() 
{
HINTERNET hOpen = NULL, 
          hConnect  = NULL, 
          hRequest = NULL;
DWORD	    dwSize = 0, 
          dwFlags = INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE; 
char 		*lpBufferA,*lpHeadersA;
TCHAR 	*lpBufferW, *lpHeadersW;
// We accept only "Text" pages.
LPTSTR 	AcceptTypes[2] = {TEXT("*/*"), NULL}; 


DWORD err;
  // Initializes the use of the Windows CE Internet functions.
	hOpen = InternetOpen (TEXT("CeHttp"),
    INTERNET_OPEN_TYPE_PRECONFIG,
	                      NULL, 0, 0);	// In case you need a proxy
// server you can enter it here
	if (!hOpen) 	return;	// Error handling
	UpdateData(TRUE); // get the URL from the Dialog Box
	// Opens an HTTP session for a given site by lpszServer. 
	if (!(hConnect = InternetConnect (hOpen, m_URL,
	                                  INTERNET_INVALID_PORT_NUMBER, 
	                                  NULL, NULL, 
	                                  INTERNET_SERVICE_HTTP, 
	                                  0, 0)))
		goto exit; // Error handling
	
	// Opens an HTTP request handle. 
	if (!(hRequest = HttpOpenRequest (hConnect,TEXT("GET"), 
	                                  NULL, HTTP_VERSION, 
	                                  NULL, (LPCTSTR*)AcceptTypes, 
	                                  dwFlags, 0)))
		goto exit;	// Error handling
	
	// Sends a request to the HTTP server. 
	if (!HttpSendRequest (hRequest, NULL, 0, NULL, 0))
	{
		err=GetLastError();
		goto exit;
	}
	
	// Call HttpQueryInfo to find out the size of the headers.
	HttpQueryInfo (hRequest, HTTP_QUERY_RAW_HEADERS_CRLF, NULL, &dwSize, NULL);
	
	// Allocates a block of memory for lpHeadersA.
	lpHeadersA = new CHAR [dwSize];
	
	// Call HttpQueryInfo again to get the headers.
	if (!HttpQueryInfo (hRequest, HTTP_QUERY_RAW_HEADERS_CRLF, 
	                    (LPVOID) lpHeadersA, &dwSize, NULL))
	{
		err=GetLastError();
		goto exit;
	}

	// Terminate headers with NULL.
	lpHeadersA [dwSize] = '\0';
  
// Get the required size of the buffer receives the UNICODE string. 
	dwSize= MultiByteToWideChar (CP_ACP, 0, lpHeadersA, -1, NULL, 0);
 	// Allocates a block of memory for lpHeadersW.
	lpHeadersW = new TCHAR [dwSize];
	// Convert headers from ASCII to UNICODE
	MultiByteToWideChar (CP_ACP, 0, lpHeadersA, -1,lpHeadersW, dwSize);    
  
	// Put the headers in the edit control.
	m_Header.SetWindowText(lpHeadersW);
     
	// Free the blocks of memory.
	delete[] lpHeadersA;
	delete[] lpHeadersW;

	// Now do the same thing for the body of the Page
	lpBufferA = new CHAR [32000];
	do
	{
		if (!InternetReadFile (hRequest, (LPVOID)lpBufferA, 32000, &dwSize))
			goto exit;
		if (dwSize != 0)
		{
			lpBufferA [dwSize] = '\0';                 
			dwSize = MultiByteToWideChar (CP_ACP, 0, lpBufferA, -1, NULL, 0);
			lpBufferW = new TCHAR [dwSize];
			MultiByteToWideChar (CP_ACP, 0, lpBufferA, -1, lpBufferW, dwSize);
			m_Page.SetWindowText(lpBufferW);
			delete[] lpBufferW;    
		}
	} while (dwSize);
	
	delete[] lpBufferA;    
exit:
	// Closes the internet handles.
	if (hOpen) InternetCloseHandle (hOpen);
	if (hConnect) InternetCloseHandle (hConnect);
	if (hRequest) InternetCloseHandle (hRequest);
	return;
}
// <BOOK_ADDON Chapter 9.7.2> *******************************************

⌨️ 快捷键说明

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