⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mtlweb.h

📁 一个使用wtl写的完整的多窗口浏览器
💻 H
📖 第 1 页 / 共 3 页
字号:
	if (lRet != ERROR_SUCCESS)
		return;

	DWORD dwSize = 0;
	DWORD dwType = REG_BINARY;
	lRet = ::RegQueryValueEx(rkOrder, _T("Order"), NULL, &dwType, NULL, &dwSize);
	if (lRet != ERROR_SUCCESS || dwSize < s_unknownOffset)
		return;

	BYTE* pByte = (BYTE*)_alloca((dwSize+10) * sizeof(BYTE));
	::memset(pByte, 0, dwSize+10);

	lRet = ::RegQueryValueEx(rkOrder, _T("Order"), NULL, &dwType, pByte, &dwSize);
	if (lRet != ERROR_SUCCESS)
		return;

	BYTE* pBegin = pByte + ((_CFavoritesOrderData*)pByte)->size + s_unknownOffset;
	BYTE* pEnd = pByte + dwSize;

	while (pBegin < pEnd) {
		_CFavoritesOrderData* pData = (_CFavoritesOrderData*)pBegin;
		CItemIDList idl(&pData->idl);
		CString strName = MtlGetFileName(idl.GetPath());
		if (!strName.IsEmpty()) {
//			ATLTRACE(_T("order(%s, %d)\n"), strName, pData->priority);
			order.Add(strName, pData->priority);
		}

		pBegin += pData->size;
	}
}
/*
void MtlGetFavoritesOrder(CFavoritesOrder& order, const CString& strDirPath)
{
	ATLASSERT(!strDirPath.IsEmpty());
	const s_unknownOffset = 12;

	CString strRoot;
	if (!MtlGetFavoritesFolder(strRoot))
		return;
	MtlMakeSureTrailingBackSlash(strRoot);
	
	CString strKeyName = _T("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\MenuOrder\\Favorites");
	CString strRelative = strDirPath.Right(strDirPath.GetLength() - strRoot.GetLength());
	if (!strRelative.IsEmpty())
		strKeyName += _T('\\') + strRelative;

	MtlRemoveTrailingBackSlash(strKeyName);
//	ATLTRACE(_T(" strKeyName(%s)\n"), strKeyName);

	CRegKey rkOrder;
	LONG lRet = rkOrder.Open(HKEY_CURRENT_USER, strKeyName);
	if (lRet != ERROR_SUCCESS)
		return;

	DWORD dwSize = 0;
	DWORD dwType = REG_BINARY;
	lRet = ::RegQueryValueEx(rkOrder, _T("Order"), NULL, &dwType, NULL, &dwSize);
	if (lRet != ERROR_SUCCESS || dwSize < s_unknownOffset)
		return;

	BYTE* pByte = (BYTE*)_alloca((dwSize+10) * sizeof(BYTE));
	::memset(pByte, 0, dwSize+10);

	lRet = ::RegQueryValueEx(rkOrder, _T("Order"), NULL, &dwType, pByte, &dwSize);
	if (lRet != ERROR_SUCCESS)
		return;

	BYTE* pBegin = pByte + ((_CFavoritesOrderData*)pByte)->size + s_unknownOffset;
	BYTE* pEnd = pByte + dwSize;

	while (pBegin < pEnd) {
		_CFavoritesOrderData* pData = (_CFavoritesOrderData*)pBegin;
		CItemIDList idl(&pData->idl);
		order.Add(idl, pData->priority);
		pBegin += pData->size;
	}
}
*/

// Based on JOBBY's code
inline DWORD MtlGetInternetZoneActionPolicy(DWORD dwAction)
{
	// create instance IInternetZoneManager
	CComPtr<IInternetZoneManager> spZoneMgr;
	HRESULT hr = CoCreateInstance(CLSID_InternetZoneManager, NULL, CLSCTX_INPROC_SERVER,
		IID_IInternetZoneManager, (void**)&spZoneMgr);

	if (FAILED(hr))
		return false;

	// get security policy
	DWORD dwPolicy = -1;
	hr = spZoneMgr->GetZoneActionPolicy(URLZONE_INTERNET, dwAction,
		(BYTE*)&dwPolicy, sizeof(dwPolicy), URLZONEREG_DEFAULT);

	if (FAILED(hr))
		return false;

	return dwPolicy;
}

inline bool MtlIsInternetZoneActionPolicyDisallow(DWORD dwAction)
{
	if (MtlGetInternetZoneActionPolicy(dwAction) == URLPOLICY_DISALLOW)
		return true;
	else
		return false;
}

bool MtlSetInternetZoneActionPolicy(DWORD dwAction, DWORD dwPolicy)
{
	// create instance IInternetZoneManager
	CComPtr<IInternetZoneManager> spZoneMgr;
	HRESULT hr = CoCreateInstance(CLSID_InternetZoneManager, NULL, CLSCTX_INPROC_SERVER,
		IID_IInternetZoneManager, (void**)&spZoneMgr);

	if (FAILED(hr))
		return false;

	// change security policy
	hr = spZoneMgr->SetZoneActionPolicy(URLZONE_INTERNET,
		dwAction, (BYTE*)&dwPolicy, sizeof(dwPolicy), URLZONEREG_DEFAULT);

	if (FAILED(hr))
		return false;

	// set security level custom
	// now the method is applied
	CVersional<ZONEATTRIBUTES> ZoneAttrib;
	hr = spZoneMgr->GetZoneAttributes(URLZONE_INTERNET, &ZoneAttrib);

	if (FAILED(hr))
		return false;

	ZoneAttrib.dwTemplateCurrentLevel = URLTEMPLATE_CUSTOM;
	hr = spZoneMgr->SetZoneAttributes(URLZONE_INTERNET, &ZoneAttrib);
	if (FAILED(hr))
		return false;

	return true;
}

// not tested yet...
inline bool MtlDeleteAllCookies()
{
	return (::InternetSetOption(0, INTERNET_OPTION_END_BROWSER_SESSION, 0, 0) == TRUE);
}

template <class _Function>
_Function _MtlForEachHTMLDocument2(IHTMLDocument2* pDocument, _Function __f)
{
	__f(pDocument);

	CComPtr<IHTMLFramesCollection2> spFrames;
	HRESULT hr = pDocument->get_frames(&spFrames);
	// cf. Even if no frame, get_frames would succeed.
	if (FAILED(hr)) {
		return __f;
	}

	LONG nCount = 0;
	hr = spFrames->get_length(&nCount);
	if (FAILED(hr))
		return __f;

	for (LONG i = 0; i < nCount; ++i) {
		CComVariant varItem(i);
		CComVariant varResult;
		hr = spFrames->item(&varItem, &varResult);
		if (FAILED(hr))
			continue;

		CComQIPtr<IHTMLWindow2> spWindow = varResult.pdispVal;
		if (!spWindow)
			continue;

		CComPtr<IHTMLDocument2> spDocument;
		hr = spWindow->get_document(&spDocument);
		if (FAILED(hr))
			continue;

		_MtlForEachHTMLDocument2(spDocument, __f);
	}

	return __f;
}

template <class _Function>
_Function MtlForEachHTMLDocument2(IWebBrowser2* pBrowser, _Function __f)
{
	CComPtr<IDispatch> spDisp;
	HRESULT hr = pBrowser->get_Document(&spDisp);
	if (FAILED(hr))
		return __f;

	CComQIPtr<IHTMLDocument2> spDocument = spDisp;
	if (!spDocument)
		return __f;

	return _MtlForEachHTMLDocument2(spDocument, __f);
}


bool __MtlSkipChar(const CString& str, int& nIndex, TCHAR ch)
{
	ATLASSERT(nIndex < str.GetLength());

	if (str[nIndex] == ch)
		++nIndex;

	if (nIndex >= str.GetLength())
		return false;
	else
		return true;
}

void _MtlCreateHrefUrlArray(CSimpleArray<CString>& arrUrl, const CString& strHtmlText)
{
	int nLast = -1;
	int nFirst = 0;
	int nLen = strHtmlText.GetLength();

	while (nFirst != -1 && nFirst < nLen)
	{
		nFirst = __MtlFind(strHtmlText, nFirst, _T("href"));
		nFirst += 4;
		if (nFirst >= nLen)
			break;

		if (!__MtlSkipChar(strHtmlText, nFirst, _T(' ')))
			break;


		if (strHtmlText[nFirst] == _T('='))	// skip a '='
			++nFirst;
		else
			break;							// illegal

		if (nFirst >= nLen)
			break;


		if (!__MtlSkipChar(strHtmlText, nFirst, _T(' ')))
			break;

		if (strHtmlText[nFirst] == _T('\"')) {	// if "
			++nFirst;
			nLast = __MtlFindChar(strHtmlText, nFirst, _T('\"'));
			if (nLast == -1)
				break;							// illegal

			CString str = strHtmlText.Mid(nFirst, nLast - nFirst);
			arrUrl.Add(str);
		}
		else {
			nLast = __MtlFindChar(strHtmlText, nFirst, _T(' '));// first, find a space
			if (nLast != -1) {	
				CString str = strHtmlText.Mid(nFirst, nLast - nFirst);
				arrUrl.Add(str);
			}
			else {
				nLast = __MtlFindChar(strHtmlText, nFirst, _T('>'));// second, find a '>'				
				if (nLast != -1) {
					CString str = strHtmlText.Mid(nFirst, nLast - nFirst);
					arrUrl.Add(str);
				}
				else {							// illegal
					break;
				}
			}
		}

		nFirst = nLast;
	}

}

inline CString _MtlGetDirectoryPathFixed(const CString& strPath, bool bAddBackSlash = false)
{
	bool bSlash = false;
	int nIndex = strPath.ReverseFind(_T('\\'));// first

	if (nIndex == -1) {
		nIndex = strPath.ReverseFind(_T('/'));
		bSlash = true;
	}

	CString strRet = strPath.Left(nIndex);

	if (bAddBackSlash) {
		if (bSlash)
			MtlMakeSureTrailingChar(strRet, _T('/'));
		else
			MtlMakeSureTrailingChar(strRet, _T('\\'));
	}

	return strRet;
}

inline CString _MtlGetRootDirectoryPathFixed(const CString& strPath, bool bAddBackSlash = false)
{
	bool bSlash = false;

	int nIndex = strPath.Find(_T(":\\"));	// first
	if (nIndex != -1) {
		nIndex += 2;
	}
	else {									// second
		nIndex = strPath.Find(_T("://"));
		bSlash = true;
		if (nIndex != -1)
			nIndex += 3;
	}

	TCHAR chSep;
	if (bSlash)
		chSep = _T('/');
	else
		chSep = _T('\\');

	if (nIndex > 0)
		nIndex = __MtlFindChar(strPath, nIndex, chSep);

	if (nIndex == -1) {// if not found, whole string is the root path.
		nIndex = strPath.GetLength();
	}

	CString strRet = strPath.Left(nIndex);

	if (bAddBackSlash) {
		MtlMakeSureTrailingChar(strRet, chSep);
	}

	return strRet;
}

inline bool _MtlIsIllegalRootSlash(const CString& str)
{
	if (str.GetLength() < 1)
		return false;

	if (str[0] == _T('/') || str[0] == _T('\\'))
		return true;
	else
		return false;
}

inline bool _MtlIsMailTo(const CString& str)
{
	if (str.Left(7) == _T("mailto:"))
		return true;
	else
		return false;
}

inline bool _MtlIsHrefID(const CString& str)
{
	if (str.GetLength() < 1)
		return false;

	if (str[0] == _T('#'))
		return true;
	else
		return false;
}

inline CString _MtlRemoveIDFromUrl(const CString& str)
{
	int nIndex = str.ReverseFind(_T('#'));
	if (nIndex != -1)
		return str.Left(nIndex);
	else
		return str;
}

inline bool _MtlIsRelativePath(const CString& str)
{
	return (str.Find(_T("://")) == -1) && (str.Find(_T(":\\")) == -1) && !_MtlIsMailTo(str);
}

inline void MtlCreateHrefUrlArray(CSimpleArray<CString>& arrUrl,
						   const CString& strHtmlText, const CString& strLocationUrl)
{
	CSimpleArray<CString> _arrUrl;
	_MtlCreateHrefUrlArray(_arrUrl, strHtmlText);

	CString strDirectory = _MtlGetDirectoryPathFixed(strLocationUrl, true);
	CString strRoot = _MtlGetRootDirectoryPathFixed(strLocationUrl, true);
	
	for (int i = 0; i < _arrUrl.GetSize(); ++i)
	{
		CString strUrl = _arrUrl[i];

		if (_MtlIsHrefID(strUrl))
		{// href = #xxx
			strUrl = _MtlRemoveIDFromUrl(strLocationUrl) + strUrl;
		}
		else if (_MtlIsRelativePath(strUrl))
		{// maybe relative path
			if (strUrl.Left(3) == _T("../")) {			// root
				strUrl = strRoot + strUrl.Right(strUrl.GetLength() - 3);
			}
			else if (_MtlIsIllegalRootSlash(strUrl)) {	// root, but illegal.
				strUrl = strRoot + strUrl.Right(strUrl.GetLength() - 1);
			}
			else {
				strUrl = strDirectory + strUrl;
			}
		}

		strUrl.Replace(_T("&amp;") , _T("&")); // Replace &amp;
		strUrl.Replace(_T('\\'), _T('/'));
		arrUrl.Add(strUrl);
	}
}


}  //namespace MTL

#endif // __MTLWEB_H__

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -