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
|
#define UNICODE //Main.cpp - Web Browser Demo
#define _UNICODE
#include <windows.h>
#include <tchar.h>
#include <cstdio>
#include "Main.h"
#include "IWebBrowser.h"
const IID IID_WebExplorer = {0xEAB22AC1,0x30C1,0x11CF,{0xA7,0xEB,0x00,0x00,0xC0,0x5B,0xAE,0x0B}};
HINSTANCE hAtlIns = NULL;
struct StartOLEProcess // The reason this strange thing - 'StartOLEProcess' is here is that in testing this
{ // thing with Windows 2000/XP I was getting crashes at program termination, i.e., 'x'ing
StartOLEProcess() // out to end program, caused presumably by either CoUninitialize() or FreeLibrary() not
{ // synchronizing well with this process's termination code. So note here that an
TCHAR szBuffer[256]; // instance of StartOLEProcess named 'InitializeOLE' will be created before WinMain()
CoInitialize(NULL); // even starts, and its destructor will be called after WinMain() exits. I put the
GetCurrentDirectory(256,szBuffer); // CoInitialize(), LoadLibrary(), CoUninitialize(), and FreeLibrary() calls in this
_tcscat(szBuffer,_T("\\Atl71.dll")); // object's Constructor and Destructor calls, and that seemed to solve the problem. One
hAtlIns=LoadLibrary(szBuffer); // does what one must, I suppose!
}
~StartOLEProcess()
{
CoUninitialize();
if(hAtlIns)
FreeLibrary(hAtlIns);
}
}InitializeOLE;
long fnWndProc_OnCreate(lpWndEventArgs Wea)
{
PFNATLAXCREATECONTROL pAtlCreateControl=NULL;
PFNATLAXGETCONTROL pAtlAxGetControl=NULL;
PFNATLAXWININIT pAtlAxWinInit=NULL;
IUnknown* ppUnkContainer=NULL;
IWebBrowser* pWebBrowser=NULL;
IUnknown* pUnkIExplorer=NULL;
HWND hCtrl,hContainer;
BSTR strProgId;
HRESULT hr;
Wea->hIns=((LPCREATESTRUCT)Wea->lParam)->hInstance;
hContainer=CreateWindow(_T("static"),_T(""),WS_CHILD|WS_VISIBLE,0,35,1180,750,Wea->hWnd,(HMENU)ID_CONTAINER,Wea->hIns,0);
if(hAtlIns)
{
pAtlAxWinInit=(PFNATLAXWININIT)GetProcAddress(hAtlIns,"AtlAxWinInit");
if(pAtlAxWinInit)
{
hr=pAtlAxWinInit();
if(SUCCEEDED(hr))
{
pAtlCreateControl=(PFNATLAXCREATECONTROL)GetProcAddress(hAtlIns,"AtlAxCreateControl");
if(pAtlCreateControl)
{
strProgId=SysAllocString(_T("Shell.Explorer"));
hr=pAtlCreateControl(strProgId,hContainer,NULL,&ppUnkContainer);
if(SUCCEEDED(hr))
{
SetWindowLong(Wea->hWnd,0,(long)ppUnkContainer);
pAtlAxGetControl=(PFNATLAXGETCONTROL)GetProcAddress(hAtlIns,"AtlAxGetControl");
if(pAtlAxGetControl)
{
hr=pAtlAxGetControl(hContainer,&pUnkIExplorer);
if(SUCCEEDED(hr))
{
SetWindowLong(Wea->hWnd,4,(long)pUnkIExplorer);
hr=pUnkIExplorer->QueryInterface(IID_WebExplorer,(void**)&pWebBrowser);
if(SUCCEEDED(hr))
SetWindowLong(Wea->hWnd,8,(long)pWebBrowser);
else
return 0;
hCtrl=CreateWindowEx(WS_EX_CLIENTEDGE,_T("edit"),_T(""),WS_CHILD|WS_VISIBLE|ES_AUTOHSCROLL,5,5,1000,25,Wea->hWnd,(HMENU)ID_URL,Wea->hIns,0);
hCtrl=CreateWindow(_T("button"),_T("Navigate"),WS_CHILD|WS_VISIBLE,1050,5,80,25,Wea->hWnd,(HMENU)BTN_NAVIGATE,Wea->hIns,0);
}
}
}
}
}
}
}
else
MessageBox(Wea->hWnd,L"Couldn't Load Atl71.dll",L"Failure",MB_ICONERROR);
return 0;
}
long fnWndProc_OnCommand(lpWndEventArgs Wea)
{
if(LOWORD(Wea->wParam)==BTN_NAVIGATE)
{
TCHAR szBuffer[256];
HWND hEdit=GetDlgItem(Wea->hWnd,ID_URL);
GetWindowText(hEdit,szBuffer,256);
IWebBrowser* pWebBrowser=NULL;
pWebBrowser=(IWebBrowser*)GetWindowLong(Wea->hWnd,8);
BSTR strUrl;
strUrl=SysAllocString(szBuffer);
if(pWebBrowser)
pWebBrowser->Navigate(strUrl,NULL,NULL,NULL,NULL);
SysFreeString(strUrl);
}
return 0;
}
long fnWndProc_OnDestroy(lpWndEventArgs Wea)
{
IUnknown* pUnkIExplorer=NULL;
IUnknown* ppUnkContainer=NULL;
IWebBrowser* pWebBrowser=NULL;
pWebBrowser=(IWebBrowser*)GetWindowLong(Wea->hWnd,8);
if(pWebBrowser)
pWebBrowser->Release();
pUnkIExplorer=(IUnknown*)GetWindowLong(Wea->hWnd,4);
if(pUnkIExplorer)
pUnkIExplorer->Release();
ppUnkContainer=(IUnknown*)GetWindowLong(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)
{
TCHAR szClassName[]=_T("Form1");
WNDCLASSEX wc;
MSG messages;
HWND hWnd;
wc.lpszClassName=szClassName; wc.lpfnWndProc=fnWndProc;
wc.cbSize=sizeof (WNDCLASSEX); wc.style=CS_DBLCLKS;
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=12;
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);
}
return messages.wParam;
}
| |