📄 mainfrm.cpp
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//
#include "stdafx.h"
#include "FavsMV.h"
#include "MainFrm.h"
//#include "HTMLViewer.h"
#include "MyHTMLView.h"
#include "FullScreenMode.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMainFrame
IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
ON_WM_CREATE()
ON_COMMAND(ID_RETRIEVE, OnRetrieve)
ON_UPDATE_COMMAND_UI(ID_RETRIEVE, OnUpdateRetrieve)
ON_UPDATE_COMMAND_UI(IDS_SITENAME, OnClickView)
ON_UPDATE_COMMAND_UI(ID_STOP_ALL, OnUpdateStopAll)
ON_COMMAND(ID_STOP_ALL, OnStopAll)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
static UINT indicators[] =
{
// ID_SEPARATOR, // status line indicator
IDS_SITENAME,
// ID_INDICATOR_CAPS,
// ID_INDICATOR_NUM,
// ID_INDICATOR_SCRL,
};
/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction
CMainFrame::CMainFrame()
{
// TODO: add member initialization code here
bURLRetreived=FALSE;
bSplitterCreated = FALSE;
m_StatusText = "";
bStopAll = FALSE;
}
CMainFrame::~CMainFrame()
{
}
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
// if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
// | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
// !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
// {
// TRACE0("Failed to create toolbar\n");
// return -1; // fail to create
// }
if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("Failed to create status bar\n");
return -1; // fail to create
}
// TODO: Delete these three lines if you don't want the toolbar to
// be dockable
// m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
// EnableDocking(CBRS_ALIGN_ANY);
// DockControlBar(&m_wndToolBar);
return 0;
}
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics
#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
CFrameWnd::AssertValid();
}
void CMainFrame::Dump(CDumpContext& dc) const
{
CFrameWnd::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
TCHAR szPath[MAX_PATH];
int nCols;
int nRows;
if(!getFavouriteskey(szPath))
return FALSE;
int nCount = GetFavorites(szPath, 0 , &caFavourites);
if(nCount <= 1)
{
AfxMessageBox("Favourites less than or equal to 1");
return FALSE;
}
else if((nCount >= 2) && (nCount <= 5))
{
nCols = nCount;
nRows = 1;
}
else
{
nCols = GetBestCount(GetEven(nCount));
nRows = nCount/nCols;
}
CreateViews(nRows , nCols , pContext);
SetSizes(nRows, nCols);
return TRUE;
}
int CMainFrame::GetFavorites(LPCTSTR pszPath, int nStartPos , CStringArray* caFavourites)
{
#define INTERNET_MAX_PATH_LENGTH 256
int nCount = 0;
CString strPath(pszPath);
CString strPath2;
CString str;
WIN32_FIND_DATA wfd;
HANDLE h;
int nPos;
int nEndPos;
TCHAR buf[INTERNET_MAX_PATH_LENGTH];
CStringArray astrFavorites;
CStringArray astrDirs;
// make sure there's a trailing backslash
if(strPath[strPath.GetLength() - 1] != _T('\\'))
strPath += _T('\\');
strPath2 = strPath;
strPath += "*.*";
// now scan the directory, first for .URL files and then for subdirectories
// that may also contain .URL files
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"))
{
// an .URL file is formatted just like an .INI file, so we can
// use GetPrivateProfileString() to get the information we want
::GetPrivateProfileString(_T("InternetShortcut"), _T("URL"),
_T(""), buf, INTERNET_MAX_PATH_LENGTH,
strPath2 + str);
caFavourites->Add(buf);
str = str.Mid(0, str.GetLength()-4);
str = str.Left(str.GetLength() - 4);
// scan through the array and perform an insertion sort
// to make sure the menu ends up in alphabetic order
for(nPos = nStartPos ; nPos < nEndPos ; ++nPos)
{
if(str.CompareNoCase(astrFavorites[nPos]) < 0)
break;
}
astrFavorites.InsertAt(nPos, str);
++nEndPos;
nCount++;
}
}
} while(FindNextFile(h, &wfd));
FindClose(h);
}
return nCount;
}
int CMainFrame::GetEven(int nCount)
{
return ((nCount % 2) == 0 ? nCount : nCount - 1);
}
void CMainFrame::OnRetrieve()
{
int i,j;
int nCols = m_Splitter.GetColumnCount();
int nRows = m_Splitter.GetRowCount();
int iIndex = 0;
for( i = 0 ; i <= nRows-1;i++)
{
for( j = 0; j <= nCols-1 ; j++)
{
CString url = caFavourites.GetAt(iIndex);
CString siteName = url.Mid(url.Find("//", 0)+2);
siteName = siteName.Left(siteName.Find("/",0));
m_wndStatusBar.SetPaneText(0, siteName, TRUE);
CMyHTMLView* cv = (CMyHTMLView*)m_Splitter.GetPane(i,j);
cv->NavigateTheURL(url);
cv->bURLRetreived = TRUE;
iIndex++;
if(bStopAll)
break;
}
if(bStopAll)
break;
}
CRect rect;
GetClientRect(&rect);
rect.right += 10;
rect.bottom += 10;
MoveWindow(rect, TRUE);
m_wndStatusBar.SetPaneText(0, "", TRUE);
bURLRetreived=TRUE;
}
int CMainFrame::GetBestCount(int iCount)
{
int retVal = 0;
for(int i = 3 ; i <= 9 ; i ++)
{
if( (iCount % i) == 0)
{
if( i != iCount)
{
retVal = i;
break;
}
}
}
if(retVal == 0)
GetBestCount(GetEven(iCount));
return retVal;
}
void CMainFrame::OnUpdateRetrieve(CCmdUI* pCmdUI)
{
pCmdUI->Enable(!bURLRetreived);
}
void CMainFrame::OnClickView(CCmdUI *pCmdUI)
{
pCmdUI->SetText(m_StatusText);
}
void CMainFrame::OnUpdateStopAll(CCmdUI* pCmdUI)
{
pCmdUI->Enable((bURLRetreived) && (!bURLRetreived));
}
void CMainFrame::OnStopAll()
{
bStopAll = TRUE;
}
BOOL CMainFrame::getFavouriteskey(TCHAR szPath[MAX_PATH])
{
TCHAR sz[MAX_PATH];
HKEY hKey;
DWORD dwSize;
if(RegOpenKey(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders"), &hKey) != ERROR_SUCCESS)
{
TRACE0("Favorites folder not found\n");
return FALSE;
}
dwSize = sizeof(sz);
RegQueryValueEx(hKey, _T("Favorites"), NULL, NULL, (LPBYTE)sz, &dwSize);
ExpandEnvironmentStrings(sz, szPath, MAX_PATH);
RegCloseKey(hKey);
return TRUE;
}
void CMainFrame::SetSizes(int nRows, int nCols)
{
int i;
CRect rect;
GetClientRect(&rect);
int iColWidth = rect.right / nCols;
for(i = 0; i <= nCols-1;i++)
m_Splitter.SetColumnInfo(i, iColWidth,50);
int iRowHeight = rect.bottom / nRows;
for(i = 0 ; i <= nRows-1;i++)
m_Splitter.SetRowInfo(i, iRowHeight , 50);
}
void CMainFrame::CreateViews(int nRows, int nCols, CCreateContext* pContext)
{
int i ,j;
int iIndex = 0;
m_Splitter.CreateStatic(this, nRows,nCols);
for( i = 0 ; i <= nRows-1;i++)
for( j = 0; j <= nCols-1 ; j++)
m_Splitter.CreateView(i,j, RUNTIME_CLASS(CMyHTMLView), CSize(0,0), pContext);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -