?? mainwnd.cpp
字號:
//
// 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
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;
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);
fRet = pWnd->Create();
// SetEvent(pWnd->hEvent);
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;
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));
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;
}
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(FAILED(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();
}
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);
_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);
GetWindowRect(_hWnd, &_rcWnd);
/*
// create a progress bar
_rcProgress.left = 5;
_rcProgress.right = _rcProgress.left + (_rcWnd.right - _rcWnd.left)/3;
_rcProgress.top = 5;
_rcProgress.bottom = _rcProgress.top + 15;
_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);
// /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;
}
void GetProxyOption()
{
/* INTERNET_PER_CONN_OPTION_LIST iOptionList;
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);
}
*/
return;
}
HWND CMainWnd::CreateBrowser()
{
HRESULT hr;
IUnknown *pUnk = NULL;
IOleObject *pObject = NULL;
if (!_pBrowser)
{
GetProxyOption();
hr = CoCreateInstance(CLSID_WebBrowser, NULL,
CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER,
IID_IUnknown, (LPVOID *)(&pUnk));
if (FAILED(hr))
return FALSE;
hr = pUnk->QueryInterface(IID_IOleObject, (LPVOID *)(&pObject));
if (FAILED(hr))
goto Cleanup;
DWORD dwFlags;
hr = pObject->GetMiscStatus(DVASPECT_CONTENT, &dwFlags);
if (FAILED(hr))
goto Cleanup;
if (dwFlags & OLEMISC_SETCLIENTSITEFIRST)
{
IOleClientSite *pClientSite;
hr = QueryInterface(IID_IOleClientSite, (LPVOID *)(&pClientSite));
if (FAILED(hr))
goto Cleanup;
hr = pObject->SetClientSite(pClientSite);
pClientSite->Release();
if (FAILED(hr))
goto Cleanup;
}
hr = Activate(pObject);
if (FAILED(hr))
goto Cleanup;
hr = _pObject->QueryInterface(IID_IWebBrowser2, (void **)&_pBrowser);
if (FAILED(hr))
goto Cleanup;
// See if there might be a url in lpszUrl
hr = pUnk->QueryInterface(IID_IOleInPlaceActiveObject, (LPVOID *)(&_pIPActiveObj));
if (FAILED(hr))
_pIPActiveObj = NULL;
//hr = S_FALSE;
//if (_fEmpty)
{
// lijh test code
TCHAR cPictFileName[MAX_PATH] = TEXT("\\IMG_0615.JPG");
//BSTR bstrURL = SysAllocString(_lpszUrl);
BSTR bstrURL = SysAllocString( cPictFileName );
if (bstrURL && bstrURL[0])
_pBrowser->Navigate(bstrURL, NULL, NULL, NULL, NULL);
//else
// _pBrowser->GoHome();
SysFreeString(bstrURL);
}
// hr = InitEvents();
}
Cleanup:
if (pUnk)
pUnk->Release();
if (pObject)
pObject->Release();
IOleWindow *pWnd = NULL;
HWND hwndBrowser = NULL;
if (_pBrowser)
{
hr = _pBrowser->QueryInterface(IID_IOleWindow, (LPVOID *)(&pWnd));
if (FAILED(hr))
return NULL;
}
if (pWnd)
{
hr = pWnd->GetWindow(&hwndBrowser);
pWnd->Release();
}
return hwndBrowser;
}
HRESULT CMainWnd::Activate(IOleObject *pObject)
{
_pObject = pObject;
_pObject->AddRef();
RECT rc;
::GetClientRect(_hWnd, &rc);
HRESULT hr;
hr = _pObject->DoVerb( OLEIVERB_UIACTIVATE, NULL, this, 0, _hWnd, &rc);
if (FAILED(hr))
goto Cleanup;
Cleanup:
return hr;
}
HRESULT CMainWnd::InitEvents()
{
HRESULT hr;
IConnectionPointContainer *pCPCont = NULL;
DWebBrowserEvents *pEvents = NULL;
if (!_pBrowser)
return S_FALSE;
hr = _pBrowser->QueryInterface(IID_IConnectionPointContainer, (LPVOID *)&pCPCont);
if (FAILED(hr))
return S_FALSE;
hr = pCPCont->FindConnectionPoint(DIID_DWebBrowserEvents2, &_pCP);
if (FAILED(hr))
{
_pCP = NULL;
goto Cleanup;
}
hr = QueryInterface(DIID_DWebBrowserEvents2, (LPVOID *)(&pEvents));
if (FAILED(hr))
goto Cleanup;
hr = _pCP->Advise(pEvents, &(_dwEventCookie));
if (FAILED(hr))
goto Cleanup;
Cleanup:
if (pCPCont)
pCPCont->Release();
if (pEvents)
pEvents->Release();
return hr;
}
HRESULT HandleNewWindow2(LPTSTR lpszUrl, DISPPARAMS FAR* pdparams)
{
HANDLE hThread;
CMainWnd *pNewWnd;
IDispatch *pDispatch;
HRESULT hr = S_OK;
pNewWnd = new CMainWnd;
if (!pNewWnd)
{
return E_OUTOFMEMORY;
}
pNewWnd->_lpszUrl = lpszUrl;
if(!pdparams)
pNewWnd->_fEmpty = TRUE;
pNewWnd->hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
if (!pNewWnd->hEvent)
{
}
NewWindow( (LPVOID)pNewWnd );
/*
InterlockedIncrement(&glThreadCount);
hThread = CreateThread(NULL, g_dwMainWindowStackSize, NewWindow, (LPVOID)pNewWnd, STACK_SIZE_PARAM_IS_A_RESERVATION, NULL);
if (!hThread)
{
delete pNewWnd;
InterlockedDecrement(&glThreadCount);
return E_OUTOFMEMORY;
}
WaitForSingleObject(pNewWnd->hEvent, INFINITE);
CloseHandle(hThread);
*/
if(pdparams)
{
if (pNewWnd->_pBrowser)
{
hr = pNewWnd->_pBrowser->QueryInterface(IID_IDispatch, (LPVOID *)(&pDispatch));
}
else
{
hr = E_FAIL;
pDispatch = NULL;
}
*(pdparams->rgvarg[0].pboolVal) = 0;
*(pdparams->rgvarg[1].ppdispVal) = pDispatch;
}
return hr;
}
STDMETHODIMP CMainWnd::GetHostInfo(DOCHOSTUIINFO *pInfo)
{
pInfo->cbSize = sizeof(DOCHOSTUIINFO);
//pInfo->dwFlags = DOCHOSTUIFLAG_NO3DBORDER|DOCHOSTUIFLAG_FLAT_SCROLLBAR;
//pInfo->dwFlags |= DOCHOSTUIFLAG_SCROLL_NO;
return S_OK;
}
STDMETHODIMP CMainWnd::Invoke (
DISPID dispidMember,
REFIID riid,
LCID lcid,
WORD wFlags,
DISPPARAMS FAR* pdparams,
VARIANT FAR* pvarResult,
EXCEPINFO FAR* pexcepinfo,
UINT FAR* puArgErr
)
{
switch (dispidMember)
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -