1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
// Main.cpp - Web Browser Demo
// g++ Main.cpp IWebBrowserEvents.cpp -lKernel32 -lgdi32 -luser32 -lole32 -loleaut32 -luuid -latl -oWebBrowser.exe -mwindows -m64 -s -Os
// cl Main.cpp IWebBrowserEvents.cpp Kernel32.lib User32.lib Gdi32.lib ole32.lib oleaut32.lib uuid.lib atl.lib /FeWebBrowser /O1 /Os /MT /GA
#ifndef UNICODE
#define UNICODE
#endif
#ifndef _UNICODE
#define _UNICODE
#endif
#include <windows.h>
#include <ocidl.h>
#include <cstdio>
#include "Main.h"
#include "IWebBrowser.h"
#include "IWebBrowserEvents.h"
EXTERN_C HRESULT __stdcall AtlAxWinInit();
EXTERN_C HRESULT __stdcall AtlAxGetControl(HWND hWnd, IUnknown** pp);
EXTERN_C HRESULT __stdcall AtlAxCreateControl(LPCOLESTR lpszName, HWND hWnd, IStream* pStream, IUnknown** ppUnkContainer);
const IID LIBID_WebBrowser = {0xEAB22AC0,0x30C1,0x11CF,{0xA7,0xEB,0x00,0x00,0xC0,0x5B,0xAE,0x0B}};
const IID IID_IWebBrowser = {0xEAB22AC1,0x30C1,0x11CF,{0xA7,0xEB,0x00,0x00,0xC0,0x5B,0xAE,0x0B}};
const IID IID_IWebBrowserEvents = {0x34A715A0,0x6587,0x11D0,{0x92,0x4A,0x00,0x20,0xAF,0xC7,0xAC,0x4D}};
const IID IID_IConnectionPointContainer = {0xB196B284,0xBAB4,0x101A,{0xB6,0x9C,0x00,0xAA,0x00,0x34,0x1D,0x07}};
long fnWndProc_OnCreate(lpWndEventArgs Wea) // What's Stored In NDCLASSEX::cbWndExtra Bytes (x64)
{ //
IConnectionPointContainer* pConnectionPointContainer = NULL; // Offset Bytes What's Stored There
IConnectionPoint* pConnectionPoint = NULL; // ==================================================
IUnknown* ppUnkContainer = NULL; //0 0 - 7 ppUnkContainer
IWebBrowser* pWebBrowser = NULL; //1 8 - 15 pUnkIExplorer
IUnknown* pUnkIExplorer = NULL; //2 16 - 23 pWebBrowser
CEventSink* pEventSink = NULL; //3 24 - 31 pConnectionPoint
HWND hContainer; //4 32 - 39 dwCookie
DWORD dwCookie;
BSTR strProgId;
HRESULT hr;
Wea->hIns=((LPCREATESTRUCT)Wea->lParam)->hInstance;
hContainer=CreateWindow((wchar_t*)L"static",(wchar_t*)L"",WS_CHILD|WS_VISIBLE,0,35,1180,750,Wea->hWnd,(HMENU)ID_CONTAINER,Wea->hIns,0);
hr=AtlAxWinInit();
if(SUCCEEDED(hr))
{
strProgId=SysAllocString((wchar_t*)L"Shell.Explorer");
hr=AtlAxCreateControl(strProgId,hContainer,NULL,&ppUnkContainer);
SysFreeString(strProgId);
if(SUCCEEDED(hr))
{
SetWindowLongPtr(Wea->hWnd,0,(LONG_PTR)ppUnkContainer);
hr=AtlAxGetControl(hContainer,&pUnkIExplorer);
if(SUCCEEDED(hr))
{
SetWindowLongPtr(Wea->hWnd,1*sizeof(void*),(LONG_PTR)pUnkIExplorer);
hr=pUnkIExplorer->QueryInterface(IID_IWebBrowser,(void**)&pWebBrowser);
if(SUCCEEDED(hr))
SetWindowLongPtr(Wea->hWnd,2*sizeof(void*),(LONG_PTR)pWebBrowser);
else
return -1;
hr=pUnkIExplorer->QueryInterface(IID_IConnectionPointContainer,(void**)&pConnectionPointContainer);
if(SUCCEEDED(hr))
{
hr=pConnectionPointContainer->FindConnectionPoint(IID_IWebBrowserEvents,&pConnectionPoint);
if(SUCCEEDED(hr))
{
pConnectionPointContainer->Release();
pConnectionPointContainer=NULL;
SetWindowLongPtr(Wea->hWnd,3*sizeof(void*),(LONG_PTR)pConnectionPoint);
pEventSink=new CEventSink;
if(pEventSink)
{
pEventSink->StoreGridPointer(pWebBrowser);
hr=pConnectionPoint->Advise((IUnknown*)pEventSink, &dwCookie);
if(SUCCEEDED(hr))
SetWindowLongPtr(Wea->hWnd,4*sizeof(void*),(LONG_PTR)dwCookie);
else
return -1;
}
else
return -1;
}
else
return -1;
}
else
return -1;
CreateWindowEx(WS_EX_CLIENTEDGE,(wchar_t*)L"edit",(wchar_t*)L"www.cplusplus.com",WS_CHILD|WS_VISIBLE|ES_AUTOHSCROLL,5,5,1000,25,Wea->hWnd,(HMENU)ID_URL,Wea->hIns,0);
CreateWindow((wchar_t*)L"button",(wchar_t*)L"Navigate",WS_CHILD|WS_VISIBLE,1050,5,80,25,Wea->hWnd,(HMENU)BTN_NAVIGATE,Wea->hIns,0);
}
else
return -1;
}
else
return -1;
}
else
return -1;
return 0;
}
long fnWndProc_OnCommand(lpWndEventArgs Wea)
{
if(LOWORD(Wea->wParam)==BTN_NAVIGATE)
{
wchar_t szBuffer[256];
HWND hEdit=GetDlgItem(Wea->hWnd,ID_URL);
GetWindowText(hEdit,szBuffer,256);
IWebBrowser* pWebBrowser=NULL;
pWebBrowser=(IWebBrowser*)GetWindowLongPtr(Wea->hWnd,2*sizeof(void*));
BSTR strUrl;
strUrl=SysAllocString(szBuffer);
if(pWebBrowser)
pWebBrowser->Navigate(strUrl,NULL,NULL,NULL,NULL);
SysFreeString(strUrl);
}
return 0;
}
long fnWndProc_OnDestroy(lpWndEventArgs Wea)
{
IConnectionPoint* pConnectionPoint = NULL;
IUnknown* ppUnkContainer = NULL;
IWebBrowser* pWebBrowser = NULL;
IUnknown* pUnkIExplorer = NULL;
DWORD dwCookie = 0;
pConnectionPoint=(IConnectionPoint*)GetWindowLongPtr(Wea->hWnd,3*sizeof(void*));
dwCookie=(DWORD)GetWindowLongPtr(Wea->hWnd,4*sizeof(void*));
if(pConnectionPoint)
{
pConnectionPoint->Unadvise(dwCookie);
pConnectionPoint->Release();
}
pWebBrowser=(IWebBrowser*)GetWindowLongPtr(Wea->hWnd,2*sizeof(void*));
if(pWebBrowser)
pWebBrowser->Release();
pUnkIExplorer=(IUnknown*)GetWindowLongPtr(Wea->hWnd,1*sizeof(void*));
if(pUnkIExplorer)
pUnkIExplorer->Release();
ppUnkContainer=(IUnknown*)GetWindowLongPtr(Wea->hWnd,0);
if(ppUnkContainer)
ppUnkContainer->Release();
PostQuitMessage(0);
return 0;
}
LRESULT CALLBACK fnWndProc(HWND hwnd, unsigned int msg, WPARAM wParam, LPARAM lParam)
{
WndEventArgs Wea;
for(unsigned int i=0; i<dim(EventHandler); i++)
{
if(EventHandler[i].iMsg==msg)
{
Wea.hWnd=hwnd, Wea.lParam=lParam, Wea.wParam=wParam;
return (*EventHandler[i].fnPtr)(&Wea);
}
}
return (DefWindowProc(hwnd, msg, wParam, lParam));
}
int WINAPI WinMain(HINSTANCE hIns, HINSTANCE hPrevIns, LPSTR lpszArgument, int iShow)
{
wchar_t szClassName[]=L"WebBrowser";
WNDCLASSEX wc;
MSG messages;
HWND hWnd;
CoInitialize(NULL);
wc.lpszClassName=szClassName; wc.lpfnWndProc=fnWndProc;
wc.cbSize=sizeof (WNDCLASSEX); wc.style=0;
wc.hIcon=LoadIcon(NULL,IDI_APPLICATION); wc.hInstance=hIns;
wc.hIconSm=LoadIcon(NULL, IDI_APPLICATION); wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=(HBRUSH)COLOR_BTNSHADOW; wc.cbWndExtra=5*sizeof(void*);
wc.lpszMenuName=NULL; wc.cbClsExtra=0;
RegisterClassEx(&wc);
hWnd=CreateWindowEx(0,szClassName,szClassName,WS_OVERLAPPEDWINDOW,100,15,1200,830,HWND_DESKTOP,0,hIns,0);
ShowWindow(hWnd,iShow);
while(GetMessage(&messages,NULL,0,0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
CoUninitialize();
return messages.wParam;
}
|