📄 mainfrm.cpp
字号:
void CMainFrame::OnSelectAll()
{
// TODO: Add your command handler code here
GetActiveIEView()->ExecWB(OLECMDID_SELECTALL, OLECMDEXECOPT_DONTPROMPTUSER,
NULL, NULL);
}
void CMainFrame::OnViewFavority()
{
// TODO: Add your command handler code here
static CDialogFav dlg;
if (dlg.GetSafeHwnd() == 0)
{
TCHAR sz[MAX_PATH];
TCHAR szPath[MAX_PATH];
HKEY hKey;
DWORD dwSize;
// find out from the registry where the favorites are located.
if(RegOpenKey(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders"), &hKey) != ERROR_SUCCESS)
{
TRACE0("Favorites folder not found\n");
return;
}
dwSize = sizeof(sz);
RegQueryValueEx(hKey, _T("Favorites"), NULL, NULL, (LPBYTE)sz, &dwSize);
ExpandEnvironmentStrings(sz, szPath, MAX_PATH);
RegCloseKey(hKey);
dlg.m_DefaultPath.Format("%s",szPath);
dlg.Create(IDD_DIALOGFAV);
}
dlg.ShowWindow(SW_SHOW);
}
void CMainFrame::OnDropDown(NMHDR* pNotifyStruct, LRESULT* pResult)
{
ButtonDropDown(pNotifyStruct,pResult);
}
int CMainFrame::BuildFavoritesMenu(LPCTSTR pszPath, int nStartPos, CMenu *pMenu)
{
CString strPath(pszPath);
CString strPath2;
CString str;
WIN32_FIND_DATA wfd;
HANDLE h;
int nPos;
int nEndPos;
int nNewEndPos;
int nLastDir;
TCHAR buf[INTERNET_MAX_PATH_LENGTH];
CStringArray astrFavorites;
CStringArray astrDirs;
CMenu* pSubMenu;
if(strPath[strPath.GetLength() - 1] != _T('\\'))
strPath += _T('\\');
strPath2 = strPath;
strPath += "*.*";
// 查找目录,首先查找.URL文件,然后查找子目录中的.URL文件
h = FindFirstFile(strPath, &wfd);
if(h != INVALID_HANDLE_VALUE)
{
nEndPos = nStartPos;
do
{
if((wfd.dwFileAttributes & (FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM))==0)
{
str = wfd.cFileName;
if(str.Right(4) == _T(".url"))
{
::GetPrivateProfileString(_T("InternetShortcut"), _T("URL"),
_T(""), buf, INTERNET_MAX_PATH_LENGTH,
strPath2 + str);
str = str.Left(str.GetLength() - 4);
for(nPos = nStartPos ; nPos < nEndPos ; ++nPos)
{
if(str.CompareNoCase(astrFavorites[nPos]) < 0)
break;
}
astrFavorites.InsertAt(nPos, str);
m_astrFavoriteURLs.InsertAt(nPos, buf);
++nEndPos;
}
}
} while(FindNextFile(h, &wfd));
FindClose(h);
// 把查到的选项增加到菜单中
for(nPos = nStartPos ; nPos < nEndPos ; ++nPos)
{
pMenu->AppendMenu(MF_STRING | MF_ENABLED, 0xe00 + nPos, astrFavorites[nPos]);
}
//已经处理完所有的.URL文件,下面查找子目录
nLastDir = 0;
h = FindFirstFile(strPath, &wfd);
ASSERT(h != INVALID_HANDLE_VALUE);
do
{
if(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
// 忽略当前目录和父目录项
if(lstrcmp(wfd.cFileName, _T(".")) == 0 || lstrcmp(wfd.cFileName, _T("..")) == 0)
continue;
for(nPos = 0 ; nPos < nLastDir ; ++nPos)
{
if(astrDirs[nPos].CompareNoCase(wfd.cFileName) > 0)
break;
}
pSubMenu = new CMenu;
pSubMenu->CreatePopupMenu();
//递归调用函数
nNewEndPos = BuildFavoritesMenu(strPath2 + wfd.cFileName, nEndPos, pSubMenu);
if(nNewEndPos != nEndPos)
{
//只插入有.URL文件的子目录到菜单中
nEndPos = nNewEndPos;
pMenu->InsertMenu(nPos, MF_BYPOSITION | MF_POPUP | MF_STRING, (UINT)pSubMenu->m_hMenu, wfd.cFileName);
pSubMenu->Detach();
astrDirs.InsertAt(nPos, wfd.cFileName);
++nLastDir;
}
delete pSubMenu;
}
} while(FindNextFile(h, &wfd));
FindClose(h);
}
return nEndPos;
}
void CMainFrame::OnNewAddressEnter()
{
CString str;
m_wndAddress.GetWindowText(str);
if (str.GetLength() == 0)
return;
GetActiveIEView()->ReadUrlDocument(str);
}
void CMainFrame::OnAddressChange()
{
}
void CMainFrame::OnNewAddress()
{
CString str;
m_wndAddress.GetLBText(m_wndAddress.GetCurSel(), str);
if (str.GetLength() == 0)
return;
GetActiveIEView()->ReadUrlDocument(str);
}
void CMainFrame::OnInetOption()
{
// TODO: Add your command handler code here
IShellDispatch* disp;
HRESULT hr;
hr = CoCreateInstance( CLSID_Shell, NULL, CLSCTX_SERVER,
IID_IDispatch, (LPVOID*)&disp ) ;
if (FAILED (hr))
{
CString str;
str.Format(_T("Failed to create Instance :-( "));
TRACE( str) ;
return;
}
hr=disp->ControlPanelItem(_bstr_t("inetcpl.cpl"));
disp->Release();
}
void CMainFrame::OnAddtoFav()
{
// TODO: Add your command handler code here
IShellUIHelper* pShellUIHelper;
HRESULT hr = CoCreateInstance(CLSID_ShellUIHelper, NULL, CLSCTX_INPROC_SERVER,
IID_IShellUIHelper, (LPVOID*)&pShellUIHelper);
if (SUCCEEDED(hr))
{
CIEView* pView = GetActiveIEView();
_variant_t vtTitle(pView->GetTitle().AllocSysString());
CString strURL = pView->GetLocationURL();
hr = pShellUIHelper->AddFavorite(strURL.AllocSysString(), &vtTitle);
pShellUIHelper->Release();
}
}
void CMainFrame::OnSettingFav()
{
// TODO: Add your command handler code here
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);
}
}
void CMainFrame::OnDocSaveAs()
{
// TODO: Add your command handler code here
COleVariant bstr;
GetActiveIEView()->ExecWB(OLECMDID_SAVEAS, OLECMDEXECOPT_DODEFAULT, &bstr, NULL);
}
void CMainFrame::OnEditFind()
{
// TODO: Add your command handler code here
MSHTML::IHTMLDocument2Ptr ptrDoc;
_variant_t varInput;
_variant_t varOutput;
IOleCommandTarget *pCMD;
HRESULT hr;
ptrDoc = GetActiveIEView()->GetHtmlDocument();
hr = ptrDoc.QueryInterface(IID_IOleCommandTarget, &pCMD);
if(SUCCEEDED(hr))
{
varInput = _T("");
hr = pCMD->Exec(&CGID_MSHTML, IDM_FIND, OLECMDEXECOPT_DODEFAULT, &varInput, &varOutput);
}
}
void CMainFrame::OnCloseWindow()
{
// TODO: Add your command handler code here
DeleteCurIEView();
}
void CMainFrame::OnCloseAll()
{
// TODO: Add your command handler code here
while(DeleteCurIEView());
}
void CMainFrame::OnShowtitle()
{
// TODO: Add your command handler code here
if(m_bShowTitle)
{
m_bShowTitle=0;
}
else
{
m_bShowTitle=1;
}
}
void CMainFrame::OnUpdateShowtitle(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_bShowTitle);
}
void CMainFrame::OnShowMainwnd()
{
// TODO: Add your command handler code here
if(!IsWindowVisible())
{
if(IsIconic())
{
ShowWindow(SW_RESTORE);
}
else
{
ShowWindow(SW_SHOW);
}
SetForegroundWindow();
}
else
{
ShowWindow(SW_HIDE);
}
}
void CMainFrame::OnSetdefaultid()
{
// TODO: Add your command handler code here
GetActiveIEView()->SetRegisterAsBrowser(TRUE);
}
void CMainFrame::OnUpdateSetdefaultid(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(GetActiveIEView()->GetRegisterAsBrowser());
}
void CMainFrame::EnableGoBack(BOOL bEnable)
{
m_bEnableGoBack=bEnable;
}
void CMainFrame::EnableGoForward(BOOL bEnable)
{
m_bEnableGoForward=bEnable;
}
void CMainFrame::OnUpdateBack(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->Enable(m_bEnableGoBack);
}
void CMainFrame::OnUpdateForward(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->Enable(m_bEnableGoForward);
}
LRESULT CMainFrame::OnCopyDataFromApp(WPARAM wParam,LPARAM lParam)
{
PCOPYDATASTRUCT pcs=(PCOPYDATASTRUCT)lParam;
char *szUrl=(char*)pcs->lpData;
LRESULT ret=SendMessage(WM_NEW_IEVIEW);
CIEView* pView=(CIEView*)ret;
if(pView)
pView->ReadUrlDocument(szUrl);
return 1;
}
void CMainFrame::ButtonDropDown(NMHDR* pNotifyStruct, LRESULT* pResult)
{
// this function handles the dropdown menus from the toolbar
NMTOOLBAR* pNMToolBar = (NMTOOLBAR*)pNotifyStruct;
CRect rect;
// translate the current toolbar item rectangle into screen coordinates
// so that we'll know where to pop up the menu
m_wndToolBar.GetToolBarCtrl().GetRect(pNMToolBar->iItem, &rect);
rect.top = rect.bottom;
::ClientToScreen(pNMToolBar->hdr.hwndFrom, &rect.TopLeft());
if(pNMToolBar->iItem == ID_VIEWFONT)
{
CMenu menu;
CMenu* pPopup;
// the font popup is stored in a resource
menu.LoadMenu(IDR_FONT_POPUP);
pPopup = menu.GetSubMenu(0);
pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON, rect.left, rect.top + 1, AfxGetMainWnd());
}
if(pNMToolBar->iItem == ID_VIEW_FAVORITY)
{
CMenu menu;
CMenu* pPopup=NULL;
// the font popup is stored in a resource
menu.LoadMenu(IDR_FONT_POPUP);
pPopup = menu.GetSubMenu(1);
// 建立收藏夹菜单
TCHAR sz[MAX_PATH];
TCHAR szPath[MAX_PATH];
HKEY hKey;
DWORD dwSize;
m_astrFavoriteURLs.RemoveAll();
while(pPopup->DeleteMenu(0, MF_BYPOSITION));
// 从注册表中查找收藏夹内容
if(RegOpenKey(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders"), &hKey) != ERROR_SUCCESS)
{
TRACE0("Favorites folder not found\n");
return;
}
dwSize = sizeof(sz);
RegQueryValueEx(hKey, _T("Favorites"), NULL, NULL, (LPBYTE)sz, &dwSize);
ExpandEnvironmentStrings(sz, szPath, MAX_PATH);
RegCloseKey(hKey);
//建立收藏夹菜单
BuildFavoritesMenu(szPath, 0, pPopup);
pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON, rect.left, rect.top + 1, AfxGetMainWnd());
}
*pResult = TBDDRET_DEFAULT;
}
void CMainFrame::OnSetActiveNewWnd()
{
// TODO: Add your command handler code here
if(m_bActvieNew)
{
m_bActvieNew=0;
}
else
{
m_bActvieNew=1;
}
}
void CMainFrame::OnUpdateSetActiveNewWnd(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(m_bActvieNew);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -