So easy,可以参考我写的一个简单的类#pragma once #ifndef __BROWSERUTILS_H__ #define __BROWSERUTILS_H__#include <ddeml.h> #include <string> #include <Exdisp.h> #include <comdef.h> class IEBrowser { public: explicit IEBrowser(); ~IEBrowser(); /** * Shows IE window, without toolbar, menubar. * * @param url - target url to be opened in popup window * @param width - popup width * @param height - popup height * @return void */ void Popup(const std::string& url, const int width, const int height); /** * navigates browser to specified url * * @param browser object (by pointer) * @param url to naviate (by ref) * * @return void */ void Navigate(IWebBrowser2Ptr& browser, const std::string& url); /** * Helper method. Gets pointer to browser * */ HRESULT getWebBrowser(IUnknown* sitePtr, IWebBrowser2Ptr& pBrwowser); /** * opens the url in a new window or tab * * @param url to open (by ref) */ void OpenUrlNewWindow(const std::string& url); void GetWebBrowser2Ptr(OUT IWebBrowser2Ptr* browser);}; #endif // ! __BROWSERUTILS_H__ // ***************************************************************************** // // Written by Jeffery Jiang ([email protected]) // Copyright (c) 2013-2016. // // This code may be used in compiled form in any way you desire. This // file may be redistributed unmodified by any means PROVIDING it is // not sold for profit without the authors written consent, and // providing that this notice and the authors name is included. // // This file is provided "as is" with no expressed or implied warranty. // The author accepts no liability if it causes any damage to you or your // computer whatsoever. It's free, so don't hassle me about it. // // Expect bugs. // // Please use and enjoy. Please let me know of any bugs/mods/improvements // that you have found/implemented and I will fix/incorporate them into this // file. // // *****************************************************************************#include "StdAfx.h" #include "IEBrowser.h" using namespace std; #include <exdisp.h> // Defines of stuff like IWebBrowser2. This is an include file with Visual C 6 and above #include <mshtml.h> // Defines of stuff like IHTMLDocument2. This is an include file with Visual C 6 and above #include <mshtmhst.h> // Defines of stuff like IDocHostUIHandler. This is an include file with Visual C 6 and above #include <oleidl.h> #pragma warning(disable : 4146) #pragma warning(disable : 4278) void IEBrowser::GetWebBrowser2Ptr( OUT IWebBrowser2Ptr* browser ) { //try to instantiate IE instance HRESULT res = CoCreateInstance(CLSID_InternetExplorer, NULL, CLSCTX_LOCAL_SERVER, IID_IWebBrowser2, (void**)browser); } void IEBrowser::Popup(const string& url, const int width, const int height) { // browser instance IWebBrowser2Ptr browser; //try to instantiate IE instance HRESULT res = CoCreateInstance(CLSID_InternetExplorer, NULL, CLSCTX_LOCAL_SERVER, IID_IWebBrowser2, (void**)&browser); if (browser == NULL) { return; } //hide menu bar const HRESULT putMenuBarRes = browser->put_MenuBar(VARIANT_FALSE); if (FAILED(putMenuBarRes)) { _com_raise_error(putMenuBarRes); } //hide toolbars const HRESULT putToolBarRes = browser->put_ToolBar(VARIANT_FALSE); if (FAILED(putToolBarRes)) { _com_raise_error(putToolBarRes); } //make not resizable const HRESULT putResizableRes = browser->put_Resizable(VARIANT_FALSE); if (FAILED(putResizableRes)) { _com_raise_error(putResizableRes); } //hide status bar const HRESULT putStatusRes = browser->put_StatusBar(VARIANT_FALSE); if (FAILED(putStatusRes)) { _com_raise_error(putStatusRes); } //hide address bar const HRESULT putAddressBarRes = browser->put_AddressBar(VARIANT_FALSE); if (FAILED(putAddressBarRes)) { _com_raise_error(putAddressBarRes); } //specify width and height const HRESULT putWidthRes = browser->put_Width(width); if (FAILED(putWidthRes)) { _com_raise_error(putWidthRes); } const HRESULT putHeigthRes = browser->put_Height(height); if (FAILED(putHeigthRes)) { _com_raise_error(putHeigthRes); } //show browser const HRESULT putVisibleRes = browser->put_Visible(VARIANT_TRUE); if (FAILED(putVisibleRes)) { _com_raise_error(putVisibleRes); } //put it on the front HWND handle; const HRESULT getHandleRes = browser->get_HWND((SHANDLE_PTR*)&handle); if (SUCCEEDED(getHandleRes)) { ::SetForegroundWindow(handle); } Navigate(browser, url);}void IEBrowser::Navigate(IWebBrowser2Ptr& browser, const string& url) { _variant_t empty; _variant_t targetUrl = _bstr_t(url.c_str()); const HRESULT navigateRes = browser->Navigate2(&targetUrl, &empty, &empty, &empty, &empty); if (FAILED(navigateRes)) { _com_raise_error(navigateRes); } } HRESULT IEBrowser::getWebBrowser(IUnknown* sitePtr, IWebBrowser2Ptr& pBrwowser) { if (!sitePtr) { return E_FAIL; } IUnknownPtr site(sitePtr); IServiceProviderPtr serviceProvider; HRESULT hr = site.QueryInterface(IID_IServiceProvider, &serviceProvider); if (FAILED(hr)) { return hr; } hr = serviceProvider->QueryService(SID_SWebBrowserApp, &pBrwowser); return hr; }// dummy dde callback function. HDDEDATA CALLBACK DdeCallbackFunction(UINT uType, UINT uFmt, HCONV hconv, HDDEDATA hsz1, HDDEDATA hsz2, HDDEDATA hdata, HDDEDATA dwData1, HDDEDATA dwData2 ) { UNREFERENCED_PARAMETER(uType); UNREFERENCED_PARAMETER(uFmt); UNREFERENCED_PARAMETER(hconv); UNREFERENCED_PARAMETER(hsz1); UNREFERENCED_PARAMETER(hsz2); UNREFERENCED_PARAMETER(hdata); UNREFERENCED_PARAMETER(dwData1); UNREFERENCED_PARAMETER(dwData2); return (HDDEDATA) NULL; }void IEBrowser::OpenUrlNewWindow(const string& url) { // perform a DDE command that will open new tab in IE (or another default action) // the same as defayut system handling DWORD instance = 0; UINT res = DdeInitialize(&instance, (PFNCALLBACK)(&DdeCallbackFunction), APPCMD_CLIENTONLY, 0); if (res != DMLERR_NO_ERROR) { return; } HSZ applicationName = DdeCreateStringHandle(instance, _T("IExplore"), CP_WINUNICODE); HSZ topic = DdeCreateStringHandle(instance, _T("WWW_OpenURLNewWindow"), CP_WINUNICODE); //open connection to the ie instance HCONV conversation = DdeConnect(instance, applicationName, topic, NULL); if (conversation == 0) { return; } string message = "\"" + url + "\",,-1,0,,,,"; // send the needed command DdeClientTransaction((LPBYTE)message.c_str(), (message.size() + 1) * sizeof(TCHAR), conversation, 0, 0, XTYP_EXECUTE, 60000, NULL); // free all the allocated resources DdeDisconnect(conversation); DdeFreeStringHandle(instance, topic); DdeFreeStringHandle(instance, applicationName); DdeUninitialize(instance); }IEBrowser::IEBrowser() { CoInitialize(NULL); }IEBrowser::~IEBrowser() { CoUninitialize(); }多少给点分呀
#ifndef __BROWSERUTILS_H__
#define __BROWSERUTILS_H__#include <ddeml.h>
#include <string>
#include <Exdisp.h>
#include <comdef.h>
class IEBrowser
{
public:
explicit IEBrowser();
~IEBrowser();
/**
* Shows IE window, without toolbar, menubar.
*
* @param url - target url to be opened in popup window
* @param width - popup width
* @param height - popup height
* @return void
*/
void Popup(const std::string& url, const int width, const int height); /**
* navigates browser to specified url
*
* @param browser object (by pointer)
* @param url to naviate (by ref)
*
* @return void
*/
void Navigate(IWebBrowser2Ptr& browser, const std::string& url); /**
* Helper method. Gets pointer to browser
*
*/
HRESULT getWebBrowser(IUnknown* sitePtr, IWebBrowser2Ptr& pBrwowser); /**
* opens the url in a new window or tab
*
* @param url to open (by ref)
*/
void OpenUrlNewWindow(const std::string& url); void GetWebBrowser2Ptr(OUT IWebBrowser2Ptr* browser);};
#endif // ! __BROWSERUTILS_H__
// *****************************************************************************
//
// Written by Jeffery Jiang ([email protected])
// Copyright (c) 2013-2016.
//
// This code may be used in compiled form in any way you desire. This
// file may be redistributed unmodified by any means PROVIDING it is
// not sold for profit without the authors written consent, and
// providing that this notice and the authors name is included.
//
// This file is provided "as is" with no expressed or implied warranty.
// The author accepts no liability if it causes any damage to you or your
// computer whatsoever. It's free, so don't hassle me about it.
//
// Expect bugs.
//
// Please use and enjoy. Please let me know of any bugs/mods/improvements
// that you have found/implemented and I will fix/incorporate them into this
// file.
//
// *****************************************************************************#include "StdAfx.h"
#include "IEBrowser.h"
using namespace std;
#include <exdisp.h> // Defines of stuff like IWebBrowser2. This is an include file with Visual C 6 and above
#include <mshtml.h> // Defines of stuff like IHTMLDocument2. This is an include file with Visual C 6 and above
#include <mshtmhst.h> // Defines of stuff like IDocHostUIHandler. This is an include file with Visual C 6 and above
#include <oleidl.h>
#pragma warning(disable : 4146)
#pragma warning(disable : 4278) void IEBrowser::GetWebBrowser2Ptr( OUT IWebBrowser2Ptr* browser )
{
//try to instantiate IE instance
HRESULT res = CoCreateInstance(CLSID_InternetExplorer, NULL, CLSCTX_LOCAL_SERVER,
IID_IWebBrowser2, (void**)browser);
}
void IEBrowser::Popup(const string& url, const int width, const int height)
{
// browser instance
IWebBrowser2Ptr browser; //try to instantiate IE instance
HRESULT res = CoCreateInstance(CLSID_InternetExplorer, NULL, CLSCTX_LOCAL_SERVER,
IID_IWebBrowser2, (void**)&browser); if (browser == NULL) {
return;
} //hide menu bar
const HRESULT putMenuBarRes = browser->put_MenuBar(VARIANT_FALSE);
if (FAILED(putMenuBarRes))
{
_com_raise_error(putMenuBarRes);
} //hide toolbars
const HRESULT putToolBarRes = browser->put_ToolBar(VARIANT_FALSE);
if (FAILED(putToolBarRes))
{
_com_raise_error(putToolBarRes);
} //make not resizable
const HRESULT putResizableRes = browser->put_Resizable(VARIANT_FALSE);
if (FAILED(putResizableRes))
{
_com_raise_error(putResizableRes);
} //hide status bar
const HRESULT putStatusRes = browser->put_StatusBar(VARIANT_FALSE);
if (FAILED(putStatusRes))
{
_com_raise_error(putStatusRes);
} //hide address bar
const HRESULT putAddressBarRes = browser->put_AddressBar(VARIANT_FALSE);
if (FAILED(putAddressBarRes))
{
_com_raise_error(putAddressBarRes);
} //specify width and height
const HRESULT putWidthRes = browser->put_Width(width);
if (FAILED(putWidthRes))
{
_com_raise_error(putWidthRes);
} const HRESULT putHeigthRes = browser->put_Height(height);
if (FAILED(putHeigthRes))
{
_com_raise_error(putHeigthRes);
} //show browser
const HRESULT putVisibleRes = browser->put_Visible(VARIANT_TRUE);
if (FAILED(putVisibleRes))
{
_com_raise_error(putVisibleRes);
} //put it on the front
HWND handle;
const HRESULT getHandleRes = browser->get_HWND((SHANDLE_PTR*)&handle);
if (SUCCEEDED(getHandleRes))
{
::SetForegroundWindow(handle);
}
Navigate(browser, url);}void IEBrowser::Navigate(IWebBrowser2Ptr& browser, const string& url)
{
_variant_t empty;
_variant_t targetUrl = _bstr_t(url.c_str()); const HRESULT navigateRes = browser->Navigate2(&targetUrl,
&empty, &empty, &empty, &empty); if (FAILED(navigateRes)) {
_com_raise_error(navigateRes);
}
}
HRESULT IEBrowser::getWebBrowser(IUnknown* sitePtr, IWebBrowser2Ptr& pBrwowser)
{
if (!sitePtr)
{
return E_FAIL;
} IUnknownPtr site(sitePtr); IServiceProviderPtr serviceProvider;
HRESULT hr = site.QueryInterface(IID_IServiceProvider, &serviceProvider);
if (FAILED(hr))
{
return hr;
} hr = serviceProvider->QueryService(SID_SWebBrowserApp, &pBrwowser);
return hr;
}// dummy dde callback function.
HDDEDATA CALLBACK DdeCallbackFunction(UINT uType, UINT uFmt, HCONV hconv,
HDDEDATA hsz1, HDDEDATA hsz2,
HDDEDATA hdata, HDDEDATA dwData1, HDDEDATA dwData2
)
{
UNREFERENCED_PARAMETER(uType);
UNREFERENCED_PARAMETER(uFmt);
UNREFERENCED_PARAMETER(hconv);
UNREFERENCED_PARAMETER(hsz1);
UNREFERENCED_PARAMETER(hsz2);
UNREFERENCED_PARAMETER(hdata);
UNREFERENCED_PARAMETER(dwData1);
UNREFERENCED_PARAMETER(dwData2); return (HDDEDATA) NULL;
}void IEBrowser::OpenUrlNewWindow(const string& url)
{
// perform a DDE command that will open new tab in IE (or another default action)
// the same as defayut system handling
DWORD instance = 0;
UINT res = DdeInitialize(&instance,
(PFNCALLBACK)(&DdeCallbackFunction), APPCMD_CLIENTONLY, 0);
if (res != DMLERR_NO_ERROR)
{
return;
}
HSZ applicationName = DdeCreateStringHandle(instance,
_T("IExplore"), CP_WINUNICODE);
HSZ topic = DdeCreateStringHandle(instance,
_T("WWW_OpenURLNewWindow"), CP_WINUNICODE);
//open connection to the ie instance
HCONV conversation = DdeConnect(instance, applicationName, topic, NULL);
if (conversation == 0)
{
return;
}
string message = "\"" + url + "\",,-1,0,,,,";
// send the needed command
DdeClientTransaction((LPBYTE)message.c_str(),
(message.size() + 1) * sizeof(TCHAR), conversation, 0, 0,
XTYP_EXECUTE, 60000, NULL);
// free all the allocated resources
DdeDisconnect(conversation);
DdeFreeStringHandle(instance, topic);
DdeFreeStringHandle(instance, applicationName);
DdeUninitialize(instance);
}IEBrowser::IEBrowser()
{
CoInitialize(NULL);
}IEBrowser::~IEBrowser()
{
CoUninitialize();
}多少给点分呀
依然看不懂……不好意思
如果看不懂,建议先学一下com,推荐一本书《com本质论》
依然看不懂……不好意思
如果看不懂,建议先学一下com,推荐一本书《com本质论》
OK谢谢