#include <ExDisp.h> // For ShellUIHelper
#include <comdef.h> // For Compiler COM Support Classes
void CMainFrame::OnAddToFavorites()
{
IShellUIHelper* pShellUIHelper;
HRESULT hr = CoCreateInstance(CLSID_ShellUIHelper, NULL,
CLSCTX_INPROC_SERVER, IID_IShellUIHelper,
(LPVOID*)&pShellUIHelper);
if (SUCCEEDED(hr))
{
CIEFavMnuView* pView = (CIEFavMnuView*)GetActiveView();
_variant_t vtTitle(pView->GetTitle().AllocSysString());
CString strURL = pView->m_webBrowser.GetLocationURL();
pShellUIHelper->AddFavorite(strURL.AllocSysString(), &vtTitle);
pShellUIHelper->Release();
}
}
Figure 3 AddToFavDlg
typedef BOOL (CALLBACK* LPFNADDFAV)(HWND, TCHAR*, UINT, TCHAR*, UINT,
LPITEMIDLIST);
void CMainFrame::OnAddToFavorites()
{
HMODULE hMod = (HMODULE)LoadLibrary("shdocvw.dll");
if (hMod)
{
LPFNADDFAV lpfnDoAddToFavDlg = (LPFNADDFAV)GetProcAddress(hMod,
"DoAddToFavDlg");
if (lpfnDoAddToFavDlg)
{
TCHAR szPath[MAX_PATH];
LPITEMIDLIST pidlFavorites;
if (SHGetSpecialFolderPath(NULL, szPath, CSIDL_FAVORITES, TRUE) &&
(SUCCEEDED(SHGetSpecialFolderLocation(NULL, CSIDL_FAVORITES,
&pidlFavorites))))
{
CIEFavMnuView* pView = (CIEFavMnuView*)GetActiveView();
TCHAR szTitle[MAX_PATH];
strcpy(szTitle, pView->GetTitle());
TCHAR szURL[MAX_PATH];
strcpy(szURL, pView->m_webBrowser.GetLocationURL());
BOOL bOK = lpfnDoAddToFavDlg(m_hWnd, szPath,
sizeof(szPath)/sizeof(szPath[0]),
szTitle,
sizeof(szTitle)/sizeof(szTitle[0]),
pidlFavorites);
m_pMalloc->Free(pidlFavorites);
if (bOK)
CreateInternetShortcut(); // Some method you create.
}
}
FreeLibrary(hMod);
}
}
Figure 5 OrganizeFavDlg
typedef UINT (CALLBACK* LPFNORGFAV)(HWND, LPTSTR);
void CMainFrame::OnOrganizeFavorites()
{
HMODULE hMod = (HMODULE)LoadLibrary("shdocvw.dll");
if (hMod)
{
LPFNORGFAV lpfnDoOrganizeFavDlg = (LPFNORGFAV)GetProcAddress(hMod,
"DoOrganizeFavDlg");
if (lpfnDoOrganizeFavDlg)
{
TCHAR szPath[MAX_PATH];
HRESULT hr = SHGetSpecialFolderPath(m_hWnd, szPath,
CSIDL_FAVORITES, TRUE);
if (SUCCEEDED(hr))
lpfnDoOrganizeFavDlg(m_hWnd, szPath);
}
FreeLibrary(hMod);
}
}
Figure 8 AddFavToMnu
void CMainFrame::AddFavToMnu(IShellFolder* pFolder, UINT* pMenuID,
CMenu* menu)
{
IEnumIDList* pItems = NULL;
LPITEMIDLIST pidlNext = NULL;
STRRET StrRetName;
STRRET StrRetFile;
LPTSTR lpszName = NULL;
LPTSTR lpszFileName = NULL;
LPTSTR lpszURL = NULL;
// Enumerate all object in the given folder
HRESULT hr = pFolder->EnumObjects(NULL, SHCONTF_FOLDERS|SHCONTF_NONFOLDERS,
&pItems);
while (NOERROR == hr)
{
hr = pItems->Next(1, &pidlNext, NULL);
if (NOERROR == hr)
{
pFolder->GetDisplayNameOf(pidlNext, SHGDN_NORMAL, &StrRetName);
pFolder->GetDisplayNameOf(pidlNext, SHGDN_FORPARSING, &StrRetFile);
StrRetToStr(StrRetName, &lpszName, pidlNext);
StrRetToStr(StrRetFile, &lpszFileName, pidlNext);
SHFILEINFO fileInfo;
SHGetFileInfo(lpszFileName, NULL, &fileInfo, sizeof(fileInfo),
SHGFI_ATTRIBUTES|SHGFI_TYPENAME);
// Create a submenu if this item is a folder
// and it is not a channel shortcut.
if ((fileInfo.dwAttributes & SFGAO_FOLDER)
&& strcmp(fileInfo.szTypeName, _T("Channel Shortcut")) != 0)
{
CMenu mnuSub;
mnuSub.CreatePopupMenu();
if (::IsMenu(mnuSub))
{
menu->AppendMenu(MF_POPUP, (UINT)mnuSub.m_hMenu, lpszName);
// Bind to the subfolder and then call this function again.
IShellFolder* pSubFolder;
HRESULT hrSub = pFolder->BindToObject(pidlNext, NULL,
IID_IShellFolder,
(LPVOID*)&pSubFolder);
if (FAILED(hrSub))
continue;
AddFavToMnu(pSubFolder, pMenuID, &mnuSub);
mnuSub.Detach();
pSubFolder->Release();
}
}
else
{
menu->AppendMenu(MF_STRING, (*pMenuID)++, lpszName);
//
// Resolve the favorite differently depending on if the
// favorite is a channel shortcut, and Internet shortcut,
// or a normal shell link. Note that we are checking szTypeName
// to determine the type of favorite. If you are worried about
// localization, check the file extension of lpszFileName
// instead.
//
if (!strcmp(fileInfo.szTypeName, _T("Channel Shortcut")))
{
ResolveChannel(pFolder, pidlNext, &lpszURL);
}
else if (!strcmp(fileInfo.szTypeName, _T("Internet Shortcut")))
{
ResolveInternetShortcut(lpszFileName, &lpszURL);
}
else
{
ResolveLink(lpszFileName, &lpszURL);
}
if (lpszURL)
{
// Add the URL to the array and free lpszURL
// since it was created with IMalloc::Alloc
m_arrayURLs.Add(lpszURL);
m_pMalloc->Free(lpszURL);
}
}
if (lpszName)
m_pMalloc->Free(lpszName);
if (lpszFileName)
m_pMalloc->Free(lpszFileName);
if (pidlNext)
m_pMalloc->Free(pidlNext);
}
}
if (pItems)
pItems->Release();
}