以下代码全部经过亲手实践。真实可靠。测试环境VC2008,WinXP和Win7。
CWebTestView派生于CHtmlView
第一种:
HRESULT CWebTestView::CreateInternetShortcut(LPTSTR pszURL, LPTSTR pszURLfilename, LPTSTR szDescription,LPTSTR szIconFile,int nIndex) { HRESULT hres; CoInitialize(NULL); IUniformResourceLocator *pURL; hres = CoCreateInstance (CLSID_InternetShortcut, NULL, CLSCTX_INPROC_SERVER, IID_IUniformResourceLocator, (void **)&pURL); if (SUCCEEDED (hres)) { IPersistFile *ppf; IShellLink *psl; // Query IShellLink for the IPersistFile interface for hres = pURL->QueryInterface (IID_IPersistFile, (void **)&ppf); hres = pURL->QueryInterface (IID_IShellLink, (void **)&psl); if (SUCCEEDED (hres)) { // Set the path to the shortcut target. pURL->SetURL(pszURL,0); //hres = psl->SetIconLocation(szIconFile,nIndex); if (SUCCEEDED (hres)) { // Set the description of the shortcut. hres = psl->SetDescription (szDescription); if (SUCCEEDED (hres)) { // Ensure that the string consists of ANSI characters. // Save the shortcut via the IPersistfile::Save member function. hres = ppf->Save (pszURLfilename, TRUE); } } // Release the pointer to IPersistFile. if(ppf) ppf->Release (); // Release the pointer to IShellLink. if(psl) psl->Release (); } pURL->Release (); } return hres; } // CWebTestView 消息处理程序 void CWebTestView::On32771() { // TODO: 在此添加命令处理程序代码 typedef BOOL (CALLBACK* LPFNADDFAV)(HWND, CHAR*, UINT, CHAR*, UINT,LPITEMIDLIST); HMODULE hMod = (HMODULE)LoadLibrary(_T("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)))) { TCHAR szTitle[MAX_PATH]; _tcscpy(szTitle, GetLocationName()); TCHAR szURL[MAX_PATH]; _tcscpy(szURL, GetLocationURL()); CHAR szAnsiPath[128]; SHUnicodeToAnsi(szPath,szAnsiPath,sizeof(szPath)); CHAR szAnsiTitile[128]; SHUnicodeToAnsi(szTitle,szAnsiTitile,sizeof(szTitle)); BOOL bOK = lpfnDoAddToFavDlg(m_hWnd, szAnsiPath, sizeof(szAnsiPath)/sizeof(CHAR), szAnsiTitile, sizeof(szAnsiTitile)/sizeof(CHAR), pidlFavorites); SHAnsiToUnicode(szAnsiPath,szPath,sizeof(szAnsiPath)*2); CoTaskMemFree(pidlFavorites); if (bOK) { HRESULT hres= CreateInternetShortcut( szURL, szPath, _T("TEST")); //创建Internet快捷方式 if(SUCCEEDED (hres)) MessageBox(_T("收藏夹添加成功")); } } } FreeLibrary(hMod); } return; }
第二种:
void CWebTestView::On32772() { // TODO: 在此添加命令处理程序代码 IShellUIHelper* pShellUIHelper; HRESULT hr = CoCreateInstance(CLSID_ShellUIHelper, NULL, CLSCTX_INPROC_SERVER, IID_IShellUIHelper,(LPVOID*)&pShellUIHelper); if (SUCCEEDED(hr)) { _variant_t vtTitle(GetLocationName().AllocSysString()); CString strURL = GetLocationURL(); hr=pShellUIHelper->AddFavorite(strURL.AllocSysString(), &vtTitle); if (SUCCEEDED(hr)) { MessageBox(_T("收藏夹添加成功")); } pShellUIHelper->Release(); } }
第三种:
void CWebTestView::On32773() { // TODO: 在此添加命令处理程序代码 #define ID_IE_ID_ADDFAV 2261 HWND oleHwnd = ::FindWindowEx(m_wndBrowser.m_hWnd, NULL, _T("Shell DocObject View"), NULL); HWND ieHwnd=::FindWindowEx(oleHwnd, NULL, _T("Internet Explorer_Server"), NULL); ::SendMessage(ieHwnd , WM_COMMAND, MAKEWPARAM(LOWORD(ID_IE_ID_ADDFAV), 0x0), 0 ); }