Webbrowser控件指定所使用IE内核版本

xingyun86 2018-4-19 2191

msdn: https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/general-info/ee330730(v=vs.85)#browser_emulation

虽然电脑上安装了IE8或者更高版本的IE浏览器,但Webbrowser控件默认总是使用IE7内核兼容模式来显示网页内容,导致很多网页样式无法正常显示,例如IE7不兼容HTML5,解决方法是在注册表中为你的进程指定引用IE的版本号。

比如程序叫做app.exe

对于32位程序:
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION

对于64位程序:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION

在对应的路径下添加你程序名字,如app.exe为名的value,DWORD值。

值选项:

11001(0×2af9)    IE11  忽略html5

11000(0×2af8)    IE11

10001(0×2711)   IE10 忽略html5

10000(0×2710)   IE10
9999 (0x270F)     IE9   忽略html5
9000 (0×2328)    IE9
8888 (0x22B8)     IE8   忽略html5
8000 (0x1F40)     IE8
7000 (0x1B58)     IE7

===========================================================

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
static bool SetBrowserFeatureControlKey(wstring feature, wchar_t *appName, DWORD value) {
	HKEY key;
	bool success = true;
	wstring featuresPath(L"Software\\Microsoft\\Internet Explorer\\Main\\FeatureControl\\");
	wstring path(featuresPath + feature);
	LONG nError = RegCreateKeyExW(HKEY_CURRENT_USER, path.c_str(), 0, NULL, REG_OPTION_VOLATILE, KEY_WRITE, NULL, &key, NULL);
	if (nError != ERROR_SUCCESS) {
		success = false;
	}
	else {
		nError = RegSetValueExW(key, appName, 0, REG_DWORD, (const BYTE*)&value, sizeof(value));
		if (nError != ERROR_SUCCESS) {
			success = false;
		}
		nError = RegCloseKey(key);
		if (nError != ERROR_SUCCESS) {
			success = false;
		}
	}
	return success;
}
static DWORD GetBrowserEmulationMode();
static void SetBrowserFeatureControl() {
	// http://msdn.microsoft.com/en-us/library/ee330720(v=vs.85).aspx
	DWORD emulationMode = GetBrowserEmulationMode();
	if (emulationMode > 0) {
		// FeatureControl settings are per-process
		wchar_t fileName[MAX_PATH + 1];
		ZeroMemory(fileName, (MAX_PATH + 1) * sizeof(wchar_t));
		GetModuleFileNameW(NULL, fileName, 256);
		vector<string> splittedFileName;
		PPSHUAI::String::string_split_to_vector(splittedFileName, PPSHUAI::Convert::UnicodeToANSI(fileName), ("\\"));
		ZeroMemory(fileName, (MAX_PATH + 1) * sizeof(wchar_t));
		wstring exeName = PPSHUAI::Convert::ANSIToUnicode(splittedFileName.at(splittedFileName.size() - 1));
		memcpy(fileName, exeName.c_str(), sizeof(wchar_t) * exeName.length());
		// make the control is not running inside Visual Studio Designer
		//if (String.Compare(fileName, "devenv.exe", true) == 0 || String.Compare(fileName, "XDesProc.exe", true) == 0) {
		//  return;
		//}
		// Windows Internet Explorer 8 and later. The FEATURE_BROWSER_EMULATION feature defines the default emulation mode for Internet
		// Explorer and supports the following values.
		// Webpages containing standards-based !DOCTYPE directives are displayed in IE10 Standards mode.
		SetBrowserFeatureControlKey(L"FEATURE_BROWSER_EMULATION", fileName, emulationMode);
		// Internet Explorer 8 or later. The FEATURE_AJAX_CONNECTIONEVENTS feature enables events that occur when the value of the online
		// property of the navigator object changes, such as when the user chooses to work offline. For more information, see the ononline
		// and onoffline events.
		// Default: DISABLED
		SetBrowserFeatureControlKey(L"FEATURE_AJAX_CONNECTIONEVENTS", fileName, 1);
		// Internet Explorer 9. Internet Explorer 9 optimized the performance of window-drawing routines that involve clipping regions associated
		// with child windows. This helped improve the performance of certain window drawing operations. However, certain applications hosting the
		// WebBrowser Control rely on the previous behavior and do not function correctly when these optimizations are enabled. The
		// FEATURE_ENABLE_CLIPCHILDREN_OPTIMIZATION feature can disable these optimizations.
		// Default: ENABLED
		// SetBrowserFeatureControlKey(L"FEATURE_ENABLE_CLIPCHILDREN_OPTIMIZATION", fileName, 1);
		// Internet Explorer 8 and later. By default, Internet Explorer reduces memory leaks caused by circular references between Internet Explorer
		// and the Microsoft JScript engine, especially in scenarios where a webpage defines an expando and the page is refreshed. If a legacy
		// application no longer functions with these changes, the FEATURE_MANAGE_SCRIPT_CIRCULAR_REFS feature can disable these improvements.
		// Default: ENABLED
		// SetBrowserFeatureControlKey(L"FEATURE_MANAGE_SCRIPT_CIRCULAR_REFS", fileName, 1);
		// Windows Internet Explorer 8. When enabled, the FEATURE_DOMSTORAGE feature allows Internet Explorer and applications hosting the WebBrowser
		// Control to use the Web Storage API. For more information, see Introduction to Web Storage.
		// Default: ENABLED
		// SetBrowserFeatureControlKey(L"FEATURE_DOMSTORAGE ", fileName, 1);
		// Internet Explorer 9. The FEATURE_GPU_RENDERING feature enables Internet Explorer to use a graphics processing unit (GPU) to render content.
		// This dramatically improves performance for webpages that are rich in graphics.
		// Default: DISABLED
		SetBrowserFeatureControlKey(L"FEATURE_GPU_RENDERING ", fileName, 1);
		// Internet Explorer 9. By default, the WebBrowser Control uses Microsoft DirectX to render webpages, which might cause problems for
		// applications that use the Draw method to create bitmaps from certain webpages. In Internet Explorer 9, this method returns a bitmap
		// (in a Windows Graphics Device Interface (GDI) wrapper) instead of a GDI metafile representation of the webpage. When the
		// FEATURE_IVIEWOBJECTDRAW_DMLT9_WITH_GDI feature is enabled, the following conditions cause the Draw method to use GDI instead of DirectX
		// to create the resulting representation. The GDI representation will contain text records and vector data, but is not guaranteed to be
		// similar to the same represenation returned in earlier versions of the browser:
		//    The device context passed to the Draw method points to an enhanced metafile.
		//    The webpage is not displayed in IE9 Standards mode.
		// By default, this feature is ENABLED for applications hosting the WebBrowser Control. This feature is ignored by Internet Explorer and
		// Windows Explorer. To enable this feature by using the registry, add the name of your executable file to the following setting.
		SetBrowserFeatureControlKey(L"FEATURE_IVIEWOBJECTDRAW_DMLT9_WITH_GDI  ", fileName, 0);
		// Windows 8 introduces a new input model that is different from the Windows 7 input model. In order to provide the broadest compatibility
		// for legacy applications, the WebBrowser Control for Windows 8 emulates the Windows 7 mouse, touch, and pen input model (also known as the
		// legacy input model). When the legacy input model is in effect, the following conditions are true:
		//    Windows pointer messages are not processed by the Trident rendering engine (mshtml.dll).
		//    Document Object Model (DOM) pointer and gesture events do not fire.
		//    Mouse and touch messages are dispatched according to the Windows 7 input model.
		//    Touch selection follows the Windows 7 model ("drag to select") instead of the Windows 8 model ("tap to select").
		//    Hardware accelerated panning and zooming is disabled.
		//    The Zoom and Pan Cascading Style Sheets (CSS) properties are ignored.
		// The FEATURE_NINPUT_LEGACYMODE feature control determines whether the legacy input model is enabled
		// Default: ENABLED
		SetBrowserFeatureControlKey(L"FEATURE_NINPUT_LEGACYMODE", fileName, 0);
		// Internet Explorer 7 consolidated HTTP compression and data manipulation into a centralized component in order to improve performance and
		// to provide greater consistency between transfer encodings (such as HTTP no-cache headers). For compatibility reasons, the original
		// implementation was left in place. When the FEATURE_DISABLE_LEGACY_COMPRESSION feature is disabled, the original compression implementation
		// is used.
		// Default: ENABLED
		// SetBrowserFeatureControlKey(L"FEATURE_DISABLE_LEGACY_COMPRESSION", fileName, 1);
		// When the FEATURE_LOCALMACHINE_LOCKDOWN feature is enabled, Internet Explorer applies security restrictions on content loaded from the
		// user's local machine, which helps prevent malicious behavior involving local files:
		//    Scripts, Microsoft ActiveX controls, and binary behaviors are not allowed to run.
		//    Object safety settings cannot be overridden.
		//    Cross-domain data actions require confirmation from the user.
		// Default: DISABLED
		// SetBrowserFeatureControlKey(L"FEATURE_LOCALMACHINE_LOCKDOWN", fileName, 0);
		// Internet Explorer 7 and later. When enabled, the FEATURE_BLOCK_LMZ_??? feature allows ??? stored in the Local Machine zone to be
		// loaded only by webpages loaded from the Local Machine zone or by webpages hosted by sites in the Trusted Sites list. For more information,
		// see Security and Compatibility in Internet Explorer 7.
		// Default: DISABLED
		//    FEATURE_BLOCK_LMZ_IMG can block images that try to load from the user's local file system. To opt in, add your process name and set 
		//                          the value to 0x00000001.
		//    FEATURE_BLOCK_LMZ_OBJECT can block objects that try to load from the user's local file system. To opt in, add your process name and
		//                          set the value to 0x00000001.
		//    FEATURE_BLOCK_LMZ_SCRIPT can block script access from the user's local file system. To opt in, add your process name and set the value
		//                          to 0x00000001.
		// SetBrowserFeatureControlKey(L"FEATURE_BLOCK_LMZ_OBJECT", fileName, 0);
		// SetBrowserFeatureControlKey(L"FEATURE_BLOCK_LMZ_OBJECT", fileName, 0);
		// SetBrowserFeatureControlKey(L"FEATURE_BLOCK_LMZ_SCRIPT", fileName, 0);
		// Internet Explorer 8 and later. When enabled, the FEATURE_DISABLE_NAVIGATION_SOUNDS feature disables the sounds played when you open a
		// link in a webpage.
		// Default: DISABLED
		SetBrowserFeatureControlKey(L"FEATURE_DISABLE_NAVIGATION_SOUNDS", fileName, 1);
		// Windows Internet Explorer 7 and later. Prior to Internet Explorer 7, href attributes of a objects supported the javascript prototcol;
		// this allowed webpages to execute script when the user clicked a link. For security reasons, this support was disabled in Internet
		// Explorer 7. For more information, see Event 1034 - Cross-Domain Barrier and Script URL Mitigation.
		// When enabled, the FEATURE_SCRIPTURL_MITIGATION feature allows the href attribute of a objects to support the javascript prototcol. 
		// Default: DISABLED
		SetBrowserFeatureControlKey(L"FEATURE_SCRIPTURL_MITIGATION", fileName, 1);
		// For Windows 8 and later, the FEATURE_SPELLCHECKING feature controls this behavior for Internet Explorer and for applications hosting
		// the web browser control (WebOC). When fully enabled, this feature automatically corrects grammar issues and identifies misspelled words
		// for the conditions described earlier.
		//    (DWORD) 00000000 - Features are disabled.
		//    (DWORD) 00000001 - Features are enabled for the conditions described earlier. (This is the default value.)
		//    (DWORD) 00000002 - Features are enabled, but only for elements that specifically set the spellcheck attribute to true.
		SetBrowserFeatureControlKey(L"FEATURE_SPELLCHECKING", fileName, 0);
		// When enabled, the FEATURE_STATUS_BAR_THROTTLING feature limits the frequency of status bar updates to one update every 200 milliseconds.
		// Default: DISABLED
		SetBrowserFeatureControlKey(L"FEATURE_STATUS_BAR_THROTTLING", fileName, 1);
		// Internet Explorer 7 or later. When enabled, the FEATURE_TABBED_BROWSING feature enables tabbed browsing navigation shortcuts and
		// notifications. For more information, see Tabbed Browsing for Developers.
		// Default: DISABLED
		// SetBrowserFeatureControlKey(L"FEATURE_TABBED_BROWSING", fileName, 1);
		// When enabled, the FEATURE_VALIDATE_NAVIGATE_URL feature control prevents Windows Internet Explorer from navigating to a badly formed URL.
		// Default: DISABLED
		SetBrowserFeatureControlKey(L"FEATURE_VALIDATE_NAVIGATE_URL", fileName, 1);
		// When enabled,the FEATURE_WEBOC_DOCUMENT_ZOOM feature allows HTML dialog boxes to inherit the zoom state of the parent window.
		// Default: DISABLED
		SetBrowserFeatureControlKey(L"FEATURE_WEBOC_DOCUMENT_ZOOM", fileName, 1);
		// The FEATURE_WEBOC_POPUPMANAGEMENT feature allows applications hosting the WebBrowser Control to receive the default Internet Explorer
		// pop-up window management behavior.
		// Default: ENABLED
		SetBrowserFeatureControlKey(L"FEATURE_WEBOC_POPUPMANAGEMENT", fileName, 0);
		// Applications hosting the WebBrowser Control should ensure that window resizing and movement events are handled appropriately for the
		// needs of the application. By default, these events are ignored if the WebBrowser Control is not hosted in a proper container. When enabled,
		// the FEATURE_WEBOC_MOVESIZECHILD feature allows these events to affect the parent window of the application hosting the WebBrowser Control.
		// Because this can lead to unpredictable results, it is not considered desirable behavior.
		// Default: DISABLED
		// SetBrowserFeatureControlKey(L"FEATURE_WEBOC_MOVESIZECHILD", fileName, 0);
		// The FEATURE_ADDON_MANAGEMENT feature enables applications hosting the WebBrowser Control
		// to respect add-on management selections made using the Add-on Manager feature of Internet Explorer.
		// Add-ons disabled by the user or by administrative group policy will also be disabled in applications that enable this feature.
		SetBrowserFeatureControlKey(L"FEATURE_ADDON_MANAGEMENT", fileName, 0);
		// Internet Explorer 10. When enabled, the FEATURE_WEBSOCKET feature allows script to create and use WebSocket objects.
		// The WebSocketobject allows websites to request data across domains from your browser by using the WebSocket protocol.
		// Default: ENABLED
		SetBrowserFeatureControlKey(L"FEATURE_WEBSOCKET", fileName, 1);
		// When enabled, the FEATURE_WINDOW_RESTRICTIONS feature adds several restrictions to the size and behavior of popup windows:
		//    Popup windows must appear in the visible display area.
		//    Popup windows are forced to have status and address bars.
		//    Popup windows must have minimum sizes.
		//    Popup windows cannot cover important areas of the parent window.
		// When enabled, this feature can be configured differently for each security zone by using the URLACTION_FEATURE_WINDOW_RESTRICTIONS URL
		// action flag. 
		// Default: ENABLED
		SetBrowserFeatureControlKey(L"FEATURE_WINDOW_RESTRICTIONS", fileName, 0);
		// Internet Explorer 7 and later. The FEATURE_XMLHTTP feature enables or disables the native XMLHttpRequest object.
		// Default: ENABLED
		// SetBrowserFeatureControlKey(L"FEATURE_XMLHTTP", fileName, 1);
	}
}
static LONG GetDWORDRegKey(HKEY hKey, const std::wstring &strValueName, DWORD &nValue, DWORD nDefaultValue) {
	nValue = nDefaultValue;
	DWORD dwBufferSize(sizeof(DWORD));
	DWORD nResult(0);
	LONG nError = ::RegQueryValueExW(hKey, strValueName.c_str(), 0, NULL, reinterpret_cast<LPBYTE>(&nResult), &dwBufferSize);
	if (ERROR_SUCCESS == nError) {
		nValue = nResult;
	}
	return nError;
}
static LONG GetStringRegKey(HKEY hKey, const std::wstring &strValueName, std::wstring &strValue, const std::wstring &strDefaultValue) {
	strValue = strDefaultValue;
	WCHAR szBuffer[512];
	DWORD dwBufferSize = sizeof(szBuffer);
	ULONG nError;
	nError = RegQueryValueExW(hKey, strValueName.c_str(), 0, NULL, (LPBYTE)szBuffer, &dwBufferSize);
	if (ERROR_SUCCESS == nError) {
		strValue = szBuffer;
	}
	return nError;
}
static DWORD GetBrowserEmulationMode() {
	int browserVersion = 11;
	wstring sBrowserVersion;
	HKEY key;
	bool success = true;
	BYTE data[256];
	wstring path(L"SOFTWARE\\Microsoft\\Internet Explorer");
	LONG nError = RegOpenKeyExW(HKEY_LOCAL_MACHINE, path.c_str(), 0, KEY_QUERY_VALUE, &key);
	DWORD mode = 11000; // Internet Explorer 11. Webpages containing standards-based !DOCTYPE directives are displayed in IE11 Standards mode. Default value for Internet Explorer 11.
	if (nError != ERROR_SUCCESS) {
		success = false;
	}
	else {
		nError = GetStringRegKey(key, L"svcVersion", sBrowserVersion, L"7");
		if (nError != ERROR_SUCCESS) {
			nError = GetStringRegKey(key, L"version", sBrowserVersion, L"7");
			if (nError != ERROR_SUCCESS) {
				success = false;
			}
		}
		if (RegCloseKey(key) != ERROR_SUCCESS) {
			success = false;
		}
	}
	if (success) {
		vector<string> splittedBrowserVersion;
		PPSHUAI::String::string_split_to_vector(splittedBrowserVersion, PPSHUAI::Convert::UnicodeToANSI(sBrowserVersion), ("."));
		browserVersion = std::atoi(splittedBrowserVersion.at(0).c_str()); // convert base 16 number in s to int
		/////////////////////////////////////////////////////////
		//	11001(0×2af9)    IE11  忽略html5
		//	11000(0×2af8)    IE11
		//	10001(0×2711)    IE10  忽略html5
		//	10000(0×2710)    IE10
		//	9999 (0x270F)    IE9   忽略html5
		//	9000 (0×2328)    IE9
		//	8888 (0x22B8)    IE8   忽略html5
		//	8000 (0x1F40)    IE8
		//	7000 (0x1B58)    IE7
		switch (browserVersion) {
		case 7:
			mode = 7000; // Webpages containing standards-based !DOCTYPE directives are displayed in IE7 Standards mode. Default value for applications hosting the WebBrowser Control.
			break;
		case 8:
			mode = 8000; // Webpages containing standards-based !DOCTYPE directives are displayed in IE8 mode. Default value for Internet Explorer 8
			break;
		case 9:
			mode = 9000; // Internet Explorer 9. Webpages containing standards-based !DOCTYPE directives are displayed in IE9 mode. Default value for Internet Explorer 9.
			break;
		case 10:
			mode = 10000; // Internet Explorer 10. Webpages containing standards-based !DOCTYPE directives are displayed in IE10 mode. Default value for Internet Explorer 10.
			break;
		case 11:
			mode = 11000; // Internet Explorer 11. Webpages containing standards-based !DOCTYPE directives are displayed in IE11 mode. Default value for Internet Explorer 11.
			break;
		default:
			// use IE11 mode by default
			mode = 11000; // Internet Explorer 11. Webpages containing standards-based !DOCTYPE directives are displayed in IE11 mode. Default value for Internet Explorer 11.
			break;
		}
	}
	else {
		mode = -1;
	}
	return mode;
}


×
打赏作者
最新回复 (0)
只看楼主
全部楼主
返回