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

📄 mtlweb.h

📁 一个使用wtl写的完整的多窗口浏览器
💻 H
📖 第 1 页 / 共 3 页
字号:

	USES_CONVERSION;
	hr = spFile->Save(T2COLE((LPCTSTR)strFile), TRUE);
	if (FAILED(hr))
		return false;

	return true;
}

inline bool MtlParseIntenetShortcutFile(CString& strFilePath)
{
	CString strExt = strFilePath.Right(4);

	if (strExt.CompareNoCase(_T(".url")) != 0)
		return false;

	TCHAR szUrl[INTERNET_MAX_PATH_LENGTH];
	::GetPrivateProfileString(_T("InternetShortcut"), _T("URL"),
		  _T(""), szUrl, INTERNET_MAX_PATH_LENGTH, strFilePath);

	CString strUrl(szUrl);
	if (strUrl.IsEmpty())
		return false;

	strFilePath = strUrl;
	return true;

}

inline CString MtlGetInternetShortcutUrl(const CString& strFile)
{
	if (!MtlIsExt(strFile, _T(".url"))) // to avoid the crash, fixed by DOGSTORE
		return CString();

	CString strResult;

	CComPtr<IUniformResourceLocator> spUrl;
	HRESULT hr = CoCreateInstance(CLSID_InternetShortcut, NULL, CLSCTX_INPROC_SERVER,
                                 IID_IUniformResourceLocator, (void**)&spUrl);
	if (FAILED(hr))
		return strResult;

	CComPtr<IPersistFile> spFile;
	hr = spUrl->QueryInterface(IID_IPersistFile, (void**)&spFile);
	if (FAILED(hr))
		return strResult;

	// Load the Internet Shortcut from persistent storage.
	USES_CONVERSION;
    hr = spFile->Load(T2COLE(strFile), STGM_READ);
	if (FAILED(hr))
		return strResult;

	LPTSTR pszUrl;
	hr = spUrl->GetURL(&pszUrl);
	if (SUCCEEDED(hr)) {
		strResult = pszUrl;
		::CoTaskMemFree(pszUrl);
	}

	return strResult;
}  

bool MtlPreOpenFile(CString& strPath, const CString& strArg = CString())
{
	ATLTRACE(_T("MtlPreOpenFile(%s)\n"), strPath.Left(100));

	if (MtlIsExt(strPath, _T(".exe"))) {
		::ShellExecute(NULL, _T("open"), strPath, strArg, NULL, SW_SHOWNORMAL);
		return true;// handled
	}

	if (!MtlIsExt(strPath, _T(".lnk")))
		return false;// not handled

	CComPtr<IShellLink> spShellLink;
	// Create IShellLink Objec
	HRESULT hr = ::CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_ALL, IID_IShellLink, (void **)&spShellLink);
	if (FAILED(hr))
		return false;

	CComQIPtr<IPersistFile> spPersistFile(spShellLink);
	if (spPersistFile) {
		// load shortcut
		USES_CONVERSION;
		hr = spPersistFile->Load(T2COLE(strPath), STGM_READ);
		if (SUCCEEDED(hr)) {
			// get link
			TCHAR szFilePath[MAX_PATH];
			WIN32_FIND_DATA wfd;
			hr = spShellLink->GetPath(szFilePath, MAX_PATH, &wfd, SLGP_UNCPRIORITY);
			if (FAILED(hr))
				return false;

			if (!MtlIsExt(szFilePath, _T(".exe"))) {
				strPath = szFilePath;
				return false;
			}

			TCHAR szCmdLine[2048]; szCmdLine[0] = _T('\0');
			hr = spShellLink->GetArguments(szCmdLine, 2048);

			if (!strArg.IsEmpty())
				::lstrcpyn(szCmdLine, strArg, 2048);

			int nShowCmd = SW_SHOWNORMAL;
			hr = spShellLink->GetShowCmd(&nShowCmd);

			TCHAR szDirPath[MAX_PATH]; szDirPath[0] = _T('\0');
			hr = spShellLink->GetWorkingDirectory(szDirPath, 2048);

			::ShellExecute(NULL, _T("open"), szFilePath, szCmdLine, szDirPath, nShowCmd);
			return true;// handled
		}
	}

	return false;
}

// Undocumented command ids
static const CLSID CGID_IWebBrowser = {0xED016940L,0xBD5B,0x11cf,0xBA, 0x4E,0x00,0xC0,0x4F,0xD7,0x08,0x16};
#define HTMLID_FIND 1
#define HTMLID_VIEWSOURCE 2
#define HTMLID_OPTIONS 3

template <class T>
class CWebBrowserCommandHandler
{
public:
// Message map and handlers
	BEGIN_MSG_MAP(CWebBrowserCommandHandler)
		COMMAND_ID_HANDLER_EX(ID_FILE_SAVE_AS, OnFileSaveAs)
		COMMAND_ID_HANDLER_EX(ID_FILE_PAGE_SETUP, OnFilePageSetup)
		COMMAND_ID_HANDLER_EX(ID_FILE_PRINT, OnFilePrint)
		COMMAND_ID_HANDLER_EX(ID_FILE_PRINT_PREVIEW, OnFilePrintPreview) // suggested by DOGSTORE, thanks
		COMMAND_ID_HANDLER_EX(ID_FILE_PROPERTIES, OnFileProperties)
		COMMAND_ID_HANDLER_EX(ID_EDIT_CUT, OnEditCut)
		COMMAND_ID_HANDLER_EX(ID_EDIT_COPY, OnEditCopy)
		COMMAND_ID_HANDLER_EX(ID_EDIT_PASTE, OnEditPaste)
		COMMAND_ID_HANDLER_EX(ID_EDIT_FIND, OnEditFind)
		COMMAND_ID_HANDLER_EX(ID_EDIT_SELECT_ALL, OnEditSelectAll)
		COMMAND_ID_HANDLER_EX(ID_VIEW_BACK, OnViewBack)
		COMMAND_ID_HANDLER_EX(ID_VIEW_FORWARD, OnViewForward)
		COMMAND_ID_HANDLER_EX(ID_VIEW_REFRESH, OnViewRefresh)
		COMMAND_ID_HANDLER_EX(ID_VIEW_STOP, OnViewStop)
		COMMAND_ID_HANDLER_EX(ID_VIEW_HOME, OnViewHome)
		COMMAND_ID_HANDLER_EX(ID_VIEW_FONT_LARGEST, OnViewFontLargest)
		COMMAND_ID_HANDLER_EX(ID_VIEW_FONT_LARGER, OnViewFontLarger)
		COMMAND_ID_HANDLER_EX(ID_VIEW_FONT_MEDIUM, OnViewFontMedium)
		COMMAND_ID_HANDLER_EX(ID_VIEW_FONT_SMALLER, OnViewFontSmaller)
		COMMAND_ID_HANDLER_EX(ID_VIEW_FONT_SMALLEST, OnViewFontSmallest)
		COMMAND_ID_HANDLER_EX(ID_VIEW_SOURCE, OnViewSource)
		COMMAND_ID_HANDLER_EX(ID_VIEW_OPTION, OnViewOption)
	END_MSG_MAP()

	void OnFileSaveAs(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/)
	{
		T* pT = static_cast<T*>(this);
		pT->m_spBrowser->ExecWB(OLECMDID_SAVEAS, OLECMDEXECOPT_DODEFAULT, NULL, NULL);
	}
	void OnFilePageSetup(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/)
	{
		T* pT = static_cast<T*>(this);
		pT->m_spBrowser->ExecWB(OLECMDID_PAGESETUP, OLECMDEXECOPT_DODEFAULT, NULL, NULL);
	}
	void OnFilePrint(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/)
	{
		T* pT = static_cast<T*>(this);
		pT->m_spBrowser->ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DODEFAULT, NULL, NULL);
	}
	void OnFilePrintPreview(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/)
	{
		T* pT = static_cast<T*>(this);
		pT->m_spBrowser->ExecWB(OLECMDID_PRINTPREVIEW, OLECMDEXECOPT_DODEFAULT, NULL, NULL);
	}
	void OnFileProperties(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/)
	{
		T* pT = static_cast<T*>(this);
		pT->m_spBrowser->ExecWB(OLECMDID_PROPERTIES, OLECMDEXECOPT_DODEFAULT, NULL, NULL);
	}
	void OnEditCut(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/)
	{	
		T* pT = static_cast<T*>(this);
		pT->m_spBrowser->ExecWB(OLECMDID_CUT, OLECMDEXECOPT_DODEFAULT, NULL, NULL);
	}
	void OnEditCopy(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/)
	{
		T* pT = static_cast<T*>(this);
		pT->m_spBrowser->ExecWB(OLECMDID_COPY, OLECMDEXECOPT_DODEFAULT, NULL, NULL);
	}
	void OnEditPaste(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/)
	{
		T* pT = static_cast<T*>(this);
		pT->m_spBrowser->ExecWB(OLECMDID_PASTE, OLECMDEXECOPT_DODEFAULT, NULL, NULL);
	}
	void OnEditFind(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/)
	{
		T* pT = static_cast<T*>(this);
		CComQIPtr<IOleCommandTarget> spCmdTarget = pT->m_spBrowser;
		spCmdTarget->Exec(&CGID_IWebBrowser, HTMLID_FIND, 0, NULL, NULL);
		// this is just file search
		//	m_spBrowser->ExecWB(OLECMDID_FIND, OLECMDEXECOPT_DODEFAULT, NULL, NULL);
	}
	void OnEditSelectAll(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/)
	{
		T* pT = static_cast<T*>(this);
		pT->m_spBrowser->ExecWB(OLECMDID_SELECTALL, OLECMDEXECOPT_DODEFAULT, NULL, NULL);
	}

	void OnViewBack(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/)
	{
		T* pT = static_cast<T*>(this);
		pT->m_spBrowser->GoBack();
	}
	void OnViewForward(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/)
	{
		T* pT = static_cast<T*>(this);
		pT->m_spBrowser->GoForward();
	}
	void OnViewRefresh(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/)
	{
		T* pT = static_cast<T*>(this);
		pT->m_spBrowser->Refresh();
	}
	void OnViewStop(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/)
	{
		T* pT = static_cast<T*>(this);
		pT->m_spBrowser->Stop();
	}
	void OnViewHome(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/)
	{
		T* pT = static_cast<T*>(this);
		pT->m_spBrowser->GoHome();
	}
	void OnViewOption(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/)
	{
		T* pT = static_cast<T*>(this);
		CComQIPtr<IOleCommandTarget> spCmdTarget = pT->m_spBrowser;
		spCmdTarget->Exec(&CGID_IWebBrowser, HTMLID_OPTIONS, 0, NULL, NULL);
		// this is modeless
		// ::ShellExecute(0, "open", "control.exe", "inetcpl.cpl", ".", SW_SHOW);
	}
	
	// zoom font
	void OnViewFontLargest(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/)
	{
		CComVariant vaZoomFactor(4L);
		T* pT = static_cast<T*>(this);
		pT->m_spBrowser->ExecWB(OLECMDID_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER,
			&vaZoomFactor, NULL);
	}
	void OnViewFontLarger(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/)
	{
		CComVariant vaZoomFactor(3L);
		T* pT = static_cast<T*>(this);
		pT->m_spBrowser->ExecWB(OLECMDID_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER,
			&vaZoomFactor, NULL);
	}
	void OnViewFontMedium(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/)
	{
		CComVariant vaZoomFactor(2L);
		T* pT = static_cast<T*>(this);
		pT->m_spBrowser->ExecWB(OLECMDID_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER,
			&vaZoomFactor, NULL);
	}
	void OnViewFontSmaller(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/)
	{
		CComVariant vaZoomFactor(1L);
		T* pT = static_cast<T*>(this);
		pT->m_spBrowser->ExecWB(OLECMDID_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER,
			&vaZoomFactor, NULL);
	}
	void OnViewFontSmallest(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/)
	{
		CComVariant vaZoomFactor(0L);
		T* pT = static_cast<T*>(this);
		pT->m_spBrowser->ExecWB(OLECMDID_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER,
			&vaZoomFactor, NULL);
	}

	void OnViewSource(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/)
	{
		T* pT = static_cast<T*>(this);
		CComQIPtr<IOleCommandTarget> spCmdTarget = pT->m_spBrowser;
		spCmdTarget->Exec(&CGID_IWebBrowser, HTMLID_VIEWSOURCE, 0, NULL, NULL);
	}
};

template <class T>
class CMDIFrameTitleUpsideDownMessageHandlerWeb
{
public:
	bool m_bValid;
	CString m_strWorkOffline;

	CMDIFrameTitleUpsideDownMessageHandlerWeb() : m_bValid(true), m_strWorkOffline(_T(" - [Work Offline]"))
	{
	}

	void UpdateTitleBarUpsideDown()
	{
		if (m_bValid) {
			CString strApp; strApp.LoadString(IDR_MAINFRAME);
			T* pT = static_cast<T*>(this);
			pT->SetWindowText(strApp);
		}
	}

	BEGIN_MSG_MAP(CMDIFrameTitleUpsideDownMessageHandlerWeb)
		MESSAGE_HANDLER(WM_GETTEXTLENGTH, OnGetTextLength)
		MESSAGE_HANDLER(WM_GETTEXT, OnGetText)
	END_MSG_MAP()

	LRESULT OnGetTextLength(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		T* pT = static_cast<T*>(this);
		return pT->DefWindowProc(uMsg, wParam, lParam) + m_strWorkOffline.GetLength() + 1;
	}

	LRESULT OnGetText(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		UINT cchTextMax = (UINT)wParam;   // number of characters to copy 
		LPTSTR lpszText = (LPTSTR)lParam;

		if (!m_bValid) {
			bHandled = FALSE;
			return 0;
		}

		T* pT = static_cast<T*>(this);

		CString strApp; strApp.LoadString(IDR_MAINFRAME);
		CString strTitle;
		BOOL bMaximized = FALSE;
		HWND hWndActive = pT->MDIGetActive(&bMaximized);
		if (hWndActive == NULL || bMaximized == FALSE)
			strTitle = strApp;
		else
			strTitle = MtlGetWindowText(hWndActive) + _T(" - ") + strApp;

		if (MtlIsGlobalOffline())
			strTitle += m_strWorkOffline;

		::lstrcpyn(lpszText, strTitle, cchTextMax);	

		bHandled = TRUE;
		return MtlMin(strTitle.GetLength(), (int)cchTextMax);
	}
};

CString MtlMakeFavoriteToolTipText(const CString& strName_, const CString& strUrl_, int cchTextMax)
{
	CString strName = MtlCompactString(strName_, cchTextMax);

	if (strUrl_.IsEmpty())
		return strName;

	int nCount = cchTextMax - strName.GetLength() - 1;// consider _('\n') too
	if (nCount <= 0)
		return strName;

	CString strUrl;
	bool bRet = MtlCompactPath(strUrl, strUrl_, nCount);

	if (!bRet || strName == strUrl)
		return strName;
	else
		return strName + _T('\n') + strUrl;
}


template <class TKey>
class CSimpleMapInt : public CSimpleMap<TKey, int>
{
public:
	int Lookup(TKey key) const
	{
		int nIndex = FindKey(key);
		if(nIndex == -1)
			return -1;
		return GetValueAt(nIndex);
	}
};

// I thought this is the best, but it seems that
// idls which means the same path don't surely have the same bits. 
//typedef CSimpleMapInt< CAdapt<CItemIDList> > CFavoritesOrder;

typedef CSimpleMapInt< CString > CFavoritesOrder;

// Thanks to fub and namazu
typedef struct _tagCFavoritesOrderData
{
	DWORD size;
	DWORD priority;
	ITEMIDLIST idl; // relative idlist
} _CFavoritesOrderData;

// Note. I've found the IE rarely fails to load his own favorites order, but don't care.
// Note. If the item not ordered, priority equals -5.
#define FAVORITESORDER_NOTFOUND -5

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);

⌨️ 快捷键说明

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