📄 ieembed.cpp
字号:
/* * Copyright (C) 2004 Sun Microsystems, Inc. All rights reserved. Use is * subject to license terms. * * This program is free software; you can redistribute it and/or modify * it under the terms of the Lesser GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA. */ #include <Ole2.h>#include "stdafx.h"#include "MsgServer.h"#include "BrowserWindow.h"#include "resource.h"#include "Message.h"#include "VariantWrapper.h"#include "Util.h"#define WM_SOCKET_MSG WM_USER + 1CComModule _Module;HWND gMainWnd;//array of browser windowsWBArray ABrowserWnd;//cached URL for POST datachar * mURL;void SocketMsgHandler(const char* pMsg){ char* msg = new char[strlen(pMsg) + 1]; strcpy(msg, pMsg); ::PostMessage(gMainWnd, WM_SOCKET_MSG, 0, (long)msg);}HRESULT LoadWebBrowserFromStream(BrowserWindow* pBrowserWnd, IStream* pStream){ HRESULT hr; CComPtr<IDispatch> pHTMLDoc; CComPtr<IPersistStreamInit> pPersistStreamInit; hr = pBrowserWnd->m_pWB->get_Document(&pHTMLDoc); if (SUCCEEDED(hr)) { hr = pHTMLDoc->QueryInterface(IID_IPersistStreamInit, (void**)&pPersistStreamInit); if (SUCCEEDED(hr)) { hr = pPersistStreamInit->Load(pStream); } } return hr;}void setContent(BrowserWindow* pBrowserWnd, char* pContent){ if (pContent == NULL) { return; } CComPtr<IStream> pStream; size_t contentLen = strlen(pContent); HGLOBAL hHTMLText; hHTMLText = GlobalAlloc(GMEM_MOVEABLE, contentLen); if (hHTMLText) { LPBYTE lpByte = (LPBYTE)GlobalLock(hHTMLText); memcpy(lpByte, (LPBYTE)pContent, contentLen * sizeof(char)); GlobalUnlock(hHTMLText); HRESULT hr = CreateStreamOnHGlobal(hHTMLText, TRUE, &pStream); if (SUCCEEDED(hr)) { LoadWebBrowserFromStream(pBrowserWnd, pStream); } GlobalFree(hHTMLText); } }LPSTR executeScript(BrowserWindow* pBrowserWnd, char* scriptCode){ //Get the IHTMLDocument Interface CComPtr<IDispatch> pIDDispatch; HRESULT hRes; hRes = pBrowserWnd->m_pWB->get_Document((struct IDispatch **)&pIDDispatch); if (SUCCEEDED(hRes)) { WBTRACE("IWebBrowser::get_Document()..."); } else { return NULL; } hRes = pIDDispatch->QueryInterface(IID_IHTMLDocument2, (void **)&(pBrowserWnd->m_pHD2)); if (SUCCEEDED(hRes)) { WBTRACE("IDispatch::QueryInterface(IID_IHTMLDocument2, void**)..."); } else { return NULL; } hRes = pIDDispatch->QueryInterface(IID_IHTMLDocument3, (void **)&(pBrowserWnd->m_pHD3)); if (SUCCEEDED(hRes)) { WBTRACE("IDispatch::QueryInterface(IID_IHTMLDocument3, void**)..."); } else { return NULL; } //Get the IHTMLWindow2 hRes = pBrowserWnd->m_pHD2->get_parentWindow((struct IHTMLWindow2 **)&(pBrowserWnd->m_pHW)); if (SUCCEEDED(hRes)) { WBTRACE("IHTMLDocument2::get_ParentWindow()..."); } else { return NULL; } //Execute the script with IHTMLWindow2::execScript() CComVariant varResult; CComBSTR vtCode; CComBSTR vtLanguage; varResult.Clear(); // Tune the given jscript to assign the returned value to a predefine // DOM property of the currently loaded webapge: // JDIC_BROWSER_INTERMEDIATE_PROP vtCode.Append(TuneJavaScript(scriptCode)); vtLanguage.Append("javascript"); hRes = pBrowserWnd->m_pHW->execScript((BSTR)vtCode, (BSTR)vtLanguage, &varResult); if (FAILED(hRes)) { void* pMsgBuf; ::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, hRes, MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US), (LPTSTR) &pMsgBuf, 0, NULL); WBTRACE((LPSTR)(pMsgBuf)); LocalFree(pMsgBuf); } //Try to get the return value of the script CComBSTR elementTag; //1. Get the head elements collection elementTag.Append("head"); CComPtr<IHTMLElementCollection> pElementCollection; hRes = pBrowserWnd->m_pHD3->getElementsByTagName(elementTag, (IHTMLElementCollection **)&pElementCollection); if (SUCCEEDED(hRes)) { WBTRACE("IHTMLDocument3::getElementsByTagName()..."); } else { return NULL; } //2. Get the # 0 element CComPtr<IDispatch> pElementDispatch; CComPtr<IHTMLElement> pHTMLElement; VARIANT itemIndex; itemIndex.vt = VT_I4; itemIndex.lVal = 0; hRes = pElementCollection->item(itemIndex, itemIndex, &pElementDispatch); if (SUCCEEDED(hRes)) { WBTRACE("IHTMLElementCollection::item()..."); } else { return NULL; } hRes = pElementDispatch->QueryInterface(IID_IHTMLElement, (void **)&pHTMLElement); if (SUCCEEDED(hRes)) { WBTRACE("IDispatch::QueryInterface(IID_IHTMLElement, void**)..."); } else { return NULL; } //3. Get the pre-defined attribute value CComBSTR attribName; CComVariant varValue; attribName.Append(JDIC_BROWSER_INTERMEDIATE_PROP); hRes = pHTMLElement->getAttribute(attribName, 0, &varValue); // Remove the intermedial attribute created by JDIC Browser. VARIANT_BOOL success; pHTMLElement->removeAttribute(attribName, 0, &success); if (SUCCEEDED(hRes)) { WBTRACE("IHTMLElement::getAttribute()"); } else { return NULL; } VariantWrapper varWrapper(&varValue); return varWrapper.ToString();}void CommandProc(char* pInputChar){ BrowserWindow * pBrowserWnd; HRESULT hRes; int instanceNum; int eventID; char eventMessage[1024]; // Decompose the socket message string. // NOTE: the "," character is used as the message field delimiter to // compose/decompose socket message strings. Which should be // identical between the Java side and native side. int i = sscanf(pInputChar, "%d,%d,%s", &instanceNum, &eventID, &eventMessage); if (i < 2) { delete pInputChar; return; } // In case that the last message string argument contains spaces, sscanf // returns before the first space. Below line returns the complete message // string. char* mMsgString = (char *)strchr(pInputChar, ','); mMsgString++; mMsgString = (char*)strchr(mMsgString, ','); mMsgString++; switch (eventID) { case JEVENT_INIT: break; case JEVENT_CREATEWINDOW: { // only create new browser window when the instance does not exist if (instanceNum < ABrowserWnd.GetSize() && (BrowserWindow *) ABrowserWnd[instanceNum] != NULL) break; RECT rect; if (i != 3) break; HWND hWnd = (HWND) atoi(mMsgString); pBrowserWnd = new BrowserWindow(); if (!pBrowserWnd) break; SetRect(&rect, 0, 0, 0, 0); HWND hWndClient = pBrowserWnd->Create(hWnd,rect, _T("about:blank"), WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, WS_EX_CLIENTEDGE); hRes = pBrowserWnd->QueryControl(&(pBrowserWnd->m_pWB)); if (pBrowserWnd->m_pWB == NULL) { WBTRACE("Failed to query pBrowserWnd->m_pWB!"); break; } hRes = pBrowserWnd->DispEventAdvise(pBrowserWnd->m_pWB);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -