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

📄 mainwnd.cpp

📁 wince下面IE工程原代码精简功能版,比较不错的工程,可以直接修改成实际的应用
💻 CPP
📖 第 1 页 / 共 3 页
字号:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
/*++
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.

Module Name:  mainwnd.cpp

Abstract:  Implements the main window, the container for the webbrowser

Functions:

Notes: Most of the code resides here. The container and its interaction with the webbrowser control,
        commandbar, statusbar etc.


--*/
#include <windows.h>
// Include the automation definitions...
#include <exdisp.h>
#include <exdispid.h>
#include <mshtmdid.h> // AMBIENT_DLCONTROL

#include <objbase.h>

#include <tchar.h>

#include <wininet.h>
#include <afdfunc.h>	//ras stuff
#include <pkfuncs.h>	// GetOwnerProcess
#include <mshtml.h>
#include <commctrl.h>
#include <commdlg.h>
#include "MainWnd.h"
#include "resource.h"

#define INITGUID
#include "initguid.h"
#include <hshell.h>

#define START_FULLSCREEN // Remove this if you don't want IESIMPLE to be full-screen at startup

///////定义接口的GUID
DEFINE_GUID(CLSID_WebBrowser,       0x8856F961L, 0x340A, 0x11D0, 0xA9, 0x6B, 0x00, 0xC0, 0x4F, 0xD7, 0x05, 0xA2);
DEFINE_GUID(IID_IWebBrowser,        0xEAB22AC1L, 0x30C1, 0x11CF, 0xA7, 0xEB, 0x00, 0x00, 0xC0, 0x5B, 0xAE, 0x0B);
DEFINE_GUID(IID_IWebBrowser2,       0xD30C1661L, 0xCDAF, 0x11D0, 0x8A, 0x3E, 0x00, 0xC0, 0x4F, 0xC9, 0xE2, 0x6E);
DEFINE_GUID(DIID_DWebBrowserEvents, 0xEAB22AC2L, 0x30C1, 0x11CF, 0xA7, 0xEB, 0x00, 0x00, 0xC0, 0x5B, 0xAE, 0x0B);
DEFINE_GUID(DIID_DWebBrowserEvents2, 0x34A715A0L, 0x6587, 0x11D0, 0x92, 0x4A, 0x00, 0x20, 0xAF, 0xC7, 0xAC, 0x4D);
DEFINE_GUID(IID_IWebBrowserApp,     0x0002DF05L, 0x0000, 0x0000, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46);

const GUID SID_SDocHost = { 0xc6504990, 0xd43e, 0x11cf, { 0x89, 0x3b, 0x00, 0xaa, 0x00, 0xbd, 0xce, 0x1a}};

#define MAX(a,b)  (a > b) ? a : b
#define MIN(a,b)  (a > b) ? b : a

LONG        glThreadCount	= 0;        /////当前线程数
HWND		ghWndAddressEdit= NULL;     /////地址栏编辑框
HANDLE      ghExitEvent		= NULL;
HINSTANCE   g_hInstance		= NULL;     ////实例句柄
DWORD      g_dwMainWindowStackSize = 0x20000;  //////主窗口栈128KB

void GetProxyOption();    ////得到代理选项


static HRESULT FindString();    /////查找字符串
HRESULT HandleNewWindow2(LPTSTR lpszUrl, DISPPARAMS FAR* pdparams); /////处理新窗口
BOOL RegisterMainWnd();        //////注册主窗口

/////////线程函数
DWORD WINAPI NewWindow(LPVOID pParam)
{
    CMainWnd *pWnd = (CMainWnd *)pParam;
    MSG msg;
    BOOL fRet;

    HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); //////初始化COM库。多线程

    fRet = pWnd->Create();       ///////创建主窗口

    SetEvent(pWnd->hEvent);      //////此时HandleNewWindow2函数结束等待

    if (!fRet)
    {
        pWnd->_pBrowser = NULL;
        return 0;
    }

    while (GetMessage( &msg, NULL, 0, 0 ) )
    {
        if(msg.message == WM_QUIT)
            break;

        if (!pWnd->PreTranslateMessage(&msg) && !(msg.message == WM_CHAR && msg.wParam == VK_TAB))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    pWnd->Release();

    CoUninitialize();

    InterlockedDecrement(&glThreadCount);
    SetEvent(ghExitEvent);

    return msg.wParam;
}

////////////////****** 主函数
int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPTSTR lpCmdLine, int nCmdShow)
{
    INITCOMMONCONTROLSEX iccsex;     ///用于保存从DLL加载的控件的信息
    HKEY hKey;
    DWORD dwSize = sizeof(DWORD);
    MSG msg;

    // HKCU is where IE\Main settings are
	/////得到注册表中主窗口的栈大小
    if(ERROR_SUCCESS == RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("SOFTWARE\\Microsoft\\Internet Explorer\\Main"), 0, 0, &hKey))
    {
            RegQueryValueEx(hKey, TEXT("StackRes"), NULL, NULL, (LPBYTE)&g_dwMainWindowStackSize, &dwSize);
            RegCloseKey(hKey);
    }

    // provide a default stack size if the one given is too small or too large. 
    if(g_dwMainWindowStackSize < 0x10000 || g_dwMainWindowStackSize > 0x80000)
    {
            // default to 128k
            g_dwMainWindowStackSize = 0x20000;
    }
    RETAILMSG(1,(L"IESIMPLE Using Stack size: %x", g_dwMainWindowStackSize));

	//////初始化COM库
    HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
    if (FAILED(hr))
    {
        return FALSE;
    }

	//////注册主窗口
    if (!RegisterMainWnd())
            return FALSE;
    
    ghExitEvent = CreateEvent(NULL, FALSE, FALSE, NULL);   //////创建事件。自动重置
    if (!ghExitEvent)
    {
        return FALSE;
    }

	///////加载rebar控件
    iccsex.dwSize = sizeof(INITCOMMONCONTROLSEX);
    iccsex.dwICC = ICC_COOL_CLASSES;                      
    InitCommonControlsEx(&iccsex);
	////应用程序实例句柄
    g_hInstance = hInst;

    // Create  a message queue on this thread
    PeekMessage(&msg, NULL, 0,0,PM_NOREMOVE);  /////加入消息循环。 消息处理后不删除消息

    if(HandleNewWindow2(lpCmdLine, NULL))     //////打开新窗口
    {
        goto Cleanup;
    }

    while (glThreadCount > 0)    /////一直循环,直到所有线程都结束
    {
        WaitForSingleObject(ghExitEvent, INFINITE);
    }
    
Cleanup:    
    CoUninitialize();     //////程序退出处理

    RETAILMSG(1, (L"IESIMPLE exited. Cmdline was: %s\r\n",lpCmdLine ? lpCmdLine : L""));

    return TRUE;
}

////////////构造函数
CMainWnd::CMainWnd()
{
    _ulRefs = 1;
    _hWnd = NULL;
    _pBrowser = NULL;
    _pObject = NULL;
    _pCP = NULL;
    _lpszUrl = NULL;
    _fFullScreen = FALSE;    /////不全屏
    _fEmpty = FALSE;
    _szTitle[0] = 0;

    _wZoom = 2; // default zoom
    _wDLCCounter = 0; // counter for Download Completes
	
}

CMainWnd::~CMainWnd()
{

RETAILMSG(1,(L"IESIMPLE Exiting ~CMainWnd\r\n"));

        if(_pBrowser)
            _pBrowser->Release();

}   

/////注册主窗口。在winmain()中
BOOL RegisterMainWnd()
{
    WNDCLASS         wc;
    wc.style         = 0;         //////窗口风格
    wc.lpfnWndProc   = (WNDPROC)CMainWnd::MainWndProc;  /////主窗口处理函数
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = g_hInstance;                     /////应用程序实例句柄
    wc.hIcon         = NULL; // LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_IE));
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)GetSysColorBrush(COLOR_WINDOW);   ///////背景色
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = TEXT("IESIMPLE");                 /////窗口类名

    if (!(RegisterClass(&wc)))
        return FALSE;

    return TRUE;
}

/////主窗口的创建函数
BOOL CMainWnd::Create()
{
    RECT rcArea;

    DWORD dwStyle = WS_VISIBLE | WS_OVERLAPPED | WS_THICKFRAME | WS_SYSMENU;  ////窗口风格
		/*|WS_OVERLAPPED|WS_BORDER|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX*/
    DWORD dwExStyle = 0/*WS_EX_OVERLAPPEDWINDOW*/;                            ////扩展窗口风格

    HMENU hMenu = NULL;

    SystemParametersInfo(SPI_GETWORKAREA, 0, &rcArea, 0);    /////得到整个屏幕尺寸到rcArea
	//////创建主窗口
    _hWnd = ::CreateWindowEx(dwExStyle,
                             TEXT("IESIMPLE"),
                             (WCHAR *)LoadString(g_hInstance, IDS_IE, NULL, 0),
                             dwStyle,
                             rcArea.left + 20,
                             rcArea.top + 20,
                             rcArea.right - rcArea.left - 40,
                             rcArea.bottom - rcArea.top - 30,
                             NULL, hMenu, g_hInstance, 0);
    if (!_hWnd)
        return FALSE;

    SetWindowLong(_hWnd, GWL_USERDATA, (DWORD)this);    ////保存变量this,也就是CMainWnd的指针
    GetWindowRect(_hWnd, &_rcWnd);                      ////得到窗口尺寸到_rcWnd

	// 创建进度条窗口
	_rcProgress.left = 5;
	_rcProgress.right = _rcProgress.left + (_rcWnd.right - _rcWnd.left)/3;
	_rcProgress.top = 5;
	_rcProgress.bottom = _rcProgress.top  + 15;
	//////创建进度条     WS_EX_NOACTIVATE风格表示非激活窗口,单击此窗口其父窗口不会被激活
	_hwndProgress = CreateWindowEx(WS_EX_NOACTIVATE, PROGRESS_CLASS, _T(""), WS_CHILD|PBS_SMOOTH|WS_BORDER,
		                           _rcProgress.left, _rcProgress.top, 
		                           _rcProgress.right-_rcProgress.left, _rcProgress.bottom-_rcProgress.top,
		                           _hWnd, NULL, g_hInstance, NULL);
	if(_hwndProgress)
        SendMessage(_hwndProgress, PBM_SETRANGE32, 0, 1000); ////进度范围0到1000
	// /progress bar
    

    if (!(_hWndBrowser = CreateBrowser()))     /////创建浏览器控件窗口
        return FALSE;

    // LONG lStyle = GetWindowLong(_hWndBrowser, GWL_STYLE);
   // SetWindowLong(_hWndBrowser, GWL_STYLE, lStyle|WS_BORDER);
 
	SetFocus(_hWnd);

    _hAccelTbl = LoadAccelerators(g_hInstance, MAKEINTRESOURCE(IDR_ACCELERATOR));

#ifdef START_FULLSCREEN
    HandleCommand( ID_FULLSCREEN, 0 );   //////满屏显示
#endif

    return TRUE;
}

//////得到Internet代理连接选项
void GetProxyOption()
{
	INTERNET_PER_CONN_OPTION_LIST	iOptionList;      //////Internet连接选项
	INTERNET_PER_CONN_OPTION		iOptions[3];
	ULONG							uSize = sizeof(iOptionList);

	iOptionList.dwSize			= uSize;
	iOptionList.pszConnection	= NULL;
	iOptionList.dwOptionCount	= 3;
	iOptionList.pOptions		= iOptions;

	// set proxy type direct or proxy server
	iOptions[0].dwOption	= INTERNET_PER_CONN_FLAGS;
	iOptions[1].dwOption	= INTERNET_PER_CONN_PROXY_SERVER;
	iOptions[2].dwOption	= INTERNET_PER_CONN_PROXY_BYPASS;
	
	if(InternetQueryOption(NULL,INTERNET_OPTION_PER_CONNECTION_OPTION ,(LPVOID)(&iOptionList),&uSize))
	{
		GlobalFree(iOptionList.pOptions[1].Value.pszValue);
		GlobalFree(iOptionList.pOptions[2].Value.pszValue);
	}
}

//////////创建浏览器控件
HWND CMainWnd::CreateBrowser()
{
    HRESULT hr;
    IUnknown *pUnk = NULL;
    IOleObject *pObject = NULL;

    if (!_pBrowser)
    {
		GetProxyOption();   //////得到代理连接选项
		///////创建WebBrowser组件对象
        hr = CoCreateInstance(CLSID_WebBrowser, NULL,
                              CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER,
                              IID_IUnknown, (LPVOID *)(&pUnk));
        if (hr)
            return FALSE;

        hr = pUnk->QueryInterface(IID_IOleObject, (LPVOID *)(&pObject)); ////得到接口IOleObject的指针到pObject
        if (hr)
            goto Cleanup;

        DWORD dwFlags;
        hr = pObject->GetMiscStatus(DVASPECT_CONTENT, &dwFlags);   /////得到OleObject的状态信息
        if (hr)
            goto Cleanup;

        if (dwFlags & OLEMISC_SETCLIENTSITEFIRST)   ////需要设置客户端站点
        {
            IOleClientSite *pClientSite;
            hr = QueryInterface(IID_IOleClientSite, (LPVOID *)(&pClientSite));
            if (hr)
                goto Cleanup;

            hr = pObject->SetClientSite(pClientSite);  //设置客户端站点
            pClientSite->Release();
            if (hr)
                goto Cleanup;
        }

        hr = Activate(pObject);       ///激活IWebBrowser控件 ,这时控件的界面显示出来
        if (hr)
            goto Cleanup;

        hr = _pObject->QueryInterface(IID_IWebBrowser2, (void **)&_pBrowser);  /// _pBrowser保存了IWebBrowser2的指针
        if (hr)
            goto Cleanup;

        // See if there might be a url in lpszUrl
        hr = pUnk->QueryInterface(IID_IOleInPlaceActiveObject, (LPVOID *)(&_pIPActiveObj));
        if (S_OK!=hr)
			_pIPActiveObj = NULL;

        hr = S_FALSE;

⌨️ 快捷键说明

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