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

📄 explorertoolbarctrl.h

📁 一个使用wtl写的完整的多窗口浏览器
💻 H
📖 第 1 页 / 共 2 页
字号:
		m_mapID.RemoveAll();
		m_arrFolderCmdID.RemoveAll();

		m_nCurrentID = m_nMinID;

		BOOL bRet;
		int nCount = GetButtonCount();
		for(int i = 0; i < nCount; i++)
		{
			bRet = DeleteButton(0);
			ATLASSERT(bRet);
		}
	}

	void _UpdateButtons()
	{
		CLockRedraw lock(m_hWnd);
		
		// cleanup
		_CleanUpButtons();

		if (m_idlRootFolder.IsNull())
			return;

		// Set up buttons
		CSimpleArray<TBBUTTON> btns;

		MtlForEachObject(m_idlRootFolder, _Function_SetUpTBBtn<thisClass>(this, btns, m_nCurrentID));

		// do anything you want
		T* pT = static_cast<T*>(this);
		pT->OnInitialUpdateTBButtons(btns, m_idlRootFolder);

		// insert items
		int i;
		for (i = 0; i < btns.GetSize(); ++i) {
			MTLVERIFY(InsertButton(-1, &btns[i]));
		}

		// Set up button text
		for (i = 0; i < btns.GetSize(); ++i) {
			TBBUTTON& btn = btns[i];
			CItemIDList idl = _GetIDListFromCmdID(btn);
			CString strBtn = MtlGetDisplayName(idl - m_idlRootFolder);
			strBtn = _GetButtonText(strBtn);
			CVersional<TBBUTTONINFO> bi;
			bi.dwMask = TBIF_TEXT;
			bi.pszText = (LPTSTR)(LPCTSTR)strBtn;
			MTLVERIFY(SetButtonInfo(btn.idCommand, &bi));
		}

		if (GetButtonCount() == 0)
			_AddNoneButton();
	}

	template <class _This>
	struct _Function_SetUpTBBtn
	{
		_This* _pThis;
		CSimpleArray<TBBUTTON>& _btns;
		int& _nCmdID;

		_Function_SetUpTBBtn(_This* pThis, CSimpleArray<TBBUTTON>& btns, int& nCmdID)
			: _pThis(pThis), _btns(btns), _nCmdID(nCmdID)
		{ }

		void operator()(IShellFolder* pFolder, const CItemIDList& idlFolder, const CItemIDList& idlFile, bool bFolder)
		{
			CItemIDList idlFull = idlFolder + idlFile;

			TBBUTTON btn;
			btn.iBitmap = MtlGetNormalIconIndex(idlFull, _pThis->m_idlHtml);
			btn.idCommand = _nCmdID;
			btn.fsState = TBSTATE_ENABLED;
			btn.fsStyle = TBSTYLE_BUTTON | TBSTYLE_AUTOSIZE;// | TBSTYLE_DROPDOWN is not proper effect...
			btn.dwData = 0;
			btn.iString = 0;

			_btns.Add(btn);
			_pThis->m_mapID.Add(_nCmdID, idlFull);// id map set up

			if (bFolder)
				_pThis->m_arrFolderCmdID.Add(_nCmdID);

			++_nCmdID;
		}
	};

	void _AddNoneButton()
	{
		// NOTE: Command Bar currently supports only menu items
		//       that are enabled and have a drop-down menu
		TBBUTTON btn;
		btn.iBitmap = NULL;
		btn.idCommand = 0;
		btn.fsState = 0;
		btn.fsStyle = TBSTYLE_BUTTON | TBSTYLE_AUTOSIZE;// | TBSTYLE_DROPDOWN;
		btn.dwData = 0;
		btn.iString = 0;

		BOOL bRet = InsertButton(-1, &btn);
		ATLASSERT(bRet);

		TBBUTTONINFO bi;
		memset(&bi, 0, sizeof(bi));
		bi.cbSize = sizeof(TBBUTTONINFO);
		bi.dwMask = TBIF_TEXT;
		bi.pszText = _T("(empty)");
	
		bRet = SetButtonInfo(0, &bi);
		ATLASSERT(bRet);
	}

	static CString _GetButtonText(const CString& strBtn)
	{
		return MtlCompactString(strBtn, CFavoritesMenuOption::GetMaxMenuItemTextLength());
	}

	void DoPopupMenu(int nCmdID, bool bAnimate)
	{
		// get popup menu and it's position
		RECT rect;
		GetItemRect(CommandToIndex(nCmdID), &rect);
		ClientToScreen(&rect);
		TPMPARAMS TPMParams;
		TPMParams.cbSize = sizeof(TPMPARAMS);
		TPMParams.rcExclude = rect;
//		HMENU hMenuPopup = ::GetSubMenu(m_menu.m_hMenu, nIndex);

		// press button and display popup menu
//		PressButton(nCmdID, TRUE);
//		SetHotItem(nCmdID);

		CString strPath = _GetPathFromCmdID(nCmdID);
		if (strPath.IsEmpty())
			return;

		if (MtlIsDirectoryPath(strPath)) {
			m_ExpMenu.SetRootDirectoryPath(strPath);
			DoTrackPopupMenu(m_menu.m_hMenu, TPM_LEFTBUTTON | TPM_RIGHTBUTTON | TPM_VERTICAL | TPM_LEFTALIGN | TPM_TOPALIGN |
				(!AtlIsOldWindows() ? (bAnimate ? TPM_VERPOSANIMATION : TPM_NOANIMATION) : 0), rect.left, rect.bottom, &TPMParams);
		}
		else {
			T* pT = static_cast<T*>(this);
			pT->OnExecute(strPath);
		}
		
//		PressButton(nCmdID, FALSE);
//		if(::GetFocus() != m_hWnd)
//			SetHotItem(-1);
	}

	BOOL DoTrackPopupMenu(HMENU hMenu, UINT uFlags, int x, int y, LPTPMPARAMS lpParams = NULL)
	{
		CMenuHandle menuPopup = hMenu;

		BOOL bTrackRet = menuPopup.TrackPopupMenuEx(uFlags, x, y, m_hWnd, lpParams);// owner is me
		return bTrackRet;
	}

	void _SetSystemImageList()
	{
		ATLASSERT(::IsWindow(m_hWnd));

		SHFILEINFO sfi;
		HIMAGELIST hImgs = (HIMAGELIST)::SHGetFileInfo(_T("C:\\"), 0, &sfi, sizeof(sfi),
			SHGFI_SYSICONINDEX | SHGFI_SMALLICON);

		SetImageList(hImgs);
	}

	template <class _This>
	struct _DefaultTBBtnCompare : public std::binary_function<const TV_INSERTSTRUCT&, const TV_INSERTSTRUCT&, bool>
	{
		_This* _pThis;

		_DefaultTBBtnCompare(_This* pThis) : _pThis(pThis) { }

		bool operator()(const TBBUTTON& x, const TBBUTTON& y)
		{
			CItemIDList idlFolder = _pThis->m_idlRootFolder;
			CItemIDList idlA = _pThis->_GetIDListFromCmdID(x);
			idlA -= idlFolder;
			CItemIDList idlB = _pThis->_GetIDListFromCmdID(y);
			idlB -= idlFolder;

			CString strA = MtlGetDisplayName(idlA);
			CString strB = MtlGetDisplayName(idlB);
			ATLASSERT(!strA.IsEmpty() && !strB.IsEmpty());


			bool bDirA = _pThis->_IsFolderCmdID(x.idCommand);
			bool bDirB = _pThis->_IsFolderCmdID(y.idCommand);

			if (bDirA == bDirB)
				return strA < strB;
			else {
				if (bDirA)
					return true;
				else
					return false;
			}
		}
	};

	CItemIDList _GetIDListFromCmdID(const TBBUTTON& btn)
	{
		return m_mapID.Lookup(btn.idCommand);
	}

	CItemIDList _GetIDListFromCmdID(int nCmdID)
	{
		return m_mapID.Lookup(nCmdID);
	}

	CString _GetPathFromCmdID(int nCmdID)
	{
		CItemIDList idl = m_mapID.Lookup(nCmdID);
		if (idl.IsNull())
			return CString();

		CString strPath = idl.GetPath();
		if (_IsFolderCmdID(nCmdID))
			MtlMakeSureTrailingBackSlash(strPath);

		return strPath;
	}

	CString _GetRootFolderPath()
	{
		CString strPath = m_idlRootFolder.GetPath();
		MtlMakeSureTrailingBackSlash(strPath);

		return strPath;
	}

	bool _PopupContextMenu(int nCmdID, CPoint pt)
	{
		CItemIDList idl = _GetIDListFromCmdID(nCmdID);
		CItemIDList idlFolder = m_idlRootFolder;
		idl -= idlFolder;	// get file name

		if (idl.IsNull())
			return false;

		// get Desktop folder
		CComPtr<IShellFolder> spDesktopFolder;
		HRESULT hr = ::SHGetDesktopFolder(&spDesktopFolder);
		if (FAILED(hr))
			return false;

		// get IShellFolder
		CComPtr<IShellFolder> spFolder;
		 hr = spDesktopFolder->BindToObject(idlFolder, NULL, IID_IShellFolder, (void**)&spFolder);
		if (FAILED(hr))
			return false;

		LPITEMIDLIST pidls[] = { idl.m_pidl };

		CComPtr<IContextMenu> spContextMenu;
		hr = spFolder->GetUIObjectOf(m_hWnd, 1, (const struct _ITEMIDLIST **)pidls, 
			IID_IContextMenu, 0, (void**)&spContextMenu);
		if (FAILED(hr))
			return false;

		m_spContextMenu2.Release();

		hr = spContextMenu->QueryInterface(IID_IContextMenu2, (void**)&m_spContextMenu2);
		if (FAILED(hr))
			return false;

		// setup menu
		CMenu menu; menu.CreatePopupMenu();
		int nPos = 0;

		UINT uFlags = CMF_EXPLORE | CMF_CANRENAME;
		hr = m_spContextMenu2->QueryContextMenu(menu.m_hMenu, nPos, s_nIDLast, 0x7fff, uFlags);
		if (FAILED(hr)) {
			return false;
		}
	
		int nRenameID = MtlGetCmdIDFromAccessKey(menu.m_hMenu, _T("&M"));

		ClientToScreen(&pt);
		UINT uMenuFlags = TPM_LEFTALIGN | TPM_TOPALIGN | TPM_LEFTBUTTON | TPM_RIGHTBUTTON;
		int idCmd = menu.TrackPopupMenu(uMenuFlags|TPM_RETURNCMD, pt.x, pt.y, m_hWnd, NULL);

		CMINVOKECOMMANDINFO cmi;
		cmi.cbSize = sizeof(CMINVOKECOMMANDINFO);
		cmi.fMask = 0;
		cmi.hwnd = m_hWnd;
		// Fixed by NISHIZAWA, T2A(MAKEINTRESOURCE(idCmd - s_nIDLast)); is wrong. See MAKEINTRESOURCE macro defination.
		cmi.lpVerb =  (LPCSTR)MAKEINTRESOURCE(idCmd - s_nIDLast);
		cmi.lpParameters = NULL;
		cmi.lpDirectory = NULL;
		cmi.nShow = SW_SHOWNORMAL;
		cmi.dwHotKey = 0;
		cmi.hIcon  = NULL;

		if (idCmd == 0) {
			m_spContextMenu2.Release();
			return false;
		}
		else if (idCmd == nRenameID) {
			_RenameFile(nCmdID);
			m_spContextMenu2.Release();
			return true;
		}

		// Execute command now
		hr = m_spContextMenu2->InvokeCommand(&cmi);

		m_spContextMenu2.Release();

		if (FAILED(hr))
			return false;

		return true;
	}

	void _RenameFile(int nCmdID)
	{
		CItemIDList idlFolder = m_idlRootFolder;
		CItemIDList idl = _GetIDListFromCmdID(nCmdID);
		idl -= idlFolder;

		CComPtr<IShellFolder> spFolder;
		if (!MtlGetShellFolder(idlFolder, &spFolder))
			return;

		CString strName;
		if (!MtlGetDisplayName(spFolder, idl, strName))
			return;

		T* pT = static_cast<T*>(this);
		if (pT->OnRenameExplorerToolBarCtrl(strName))
			MtlSetDisplayName(spFolder, idl, strName, m_hWnd);
	}

	bool _IsFolderCmdID(int nCmdID)
	{
		if (m_arrFolderCmdID.Find(nCmdID) == -1)
			return false;
		else
			return true;
	}
};

⌨️ 快捷键说明

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