?? multimru.cpp
字號:
// MultiMRU.cpp : Defines the class behaviors for the application.
//
#include "stdafx.h"
#include "MultiMRU.h"
#include "MainFrm.h"
#include "MultiMRUDoc.h"
#include "MultiMRUView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMultiMRUApp
AFX_STATIC_DATA const TCHAR *_afxFileSection[NUM_FILTER] = {_T("Recent BMP File List"),_T("Recent JPG File List")};//注冊表中"最近BMP文件"和"最近JPG文件"的主鍵名;
AFX_STATIC_DATA const TCHAR *_afxFileEntry[NUM_FILTER] = {_T("BMPFile%d"),_T("JPGFile%d")};//注冊表中每一文件項的鍵名;
AFX_STATIC_DATA const TCHAR _afxPreviewSection[] = _T("Settings");
AFX_STATIC_DATA const TCHAR _afxPreviewEntry[] = _T("PreviewPages");
BEGIN_MESSAGE_MAP(CMultiMRUApp, CWinApp)
//{{AFX_MSG_MAP(CMultiMRUApp)
ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
//添加最近文件列表菜單消息映射
ON_UPDATE_COMMAND_UI(ID_FILE_MRU_BMP_FILE1, OnUpdateRecentBMPFileMenu)
ON_COMMAND_EX_RANGE(ID_FILE_MRU_BMP_FILE1, ID_FILE_MRU_BMP_FILE16, OnOpenRecentBMPFile)
ON_UPDATE_COMMAND_UI(ID_FILE_MRU_JPG_FILE1, OnUpdateRecentJPGFileMenu)
ON_COMMAND_EX_RANGE(ID_FILE_MRU_JPG_FILE1, ID_FILE_MRU_JPG_FILE16, OnOpenRecentJPGFile)
//}}AFX_MSG_MAP
// Standard file based document commands
ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
// Standard print setup command
ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMultiMRUApp construction
CMultiMRUApp::CMultiMRUApp()
{
for(int i=0;i<NUM_FILTER;i++)
m_pRecentFileList[i]=NULL;
//初始化擴展名數組;
szExt[0]=".BMP";
szExt[1]=".JPG";
}
CMultiMRUApp::~CMultiMRUApp()
{
//釋放內存;
for(int i=0;i<NUM_FILTER;i++)
if(m_pRecentFileList[i])
delete m_pRecentFileList[i];
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CMultiMRUApp object
CMultiMRUApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CMultiMRUApp initialization
BOOL CMultiMRUApp::InitInstance()
{
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
// Change the registry key under which our settings are stored.
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization.
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
LoadStdProfileSettings(5); // 載入標準INI文件選項(包括MRU),函數參數為最大文件數 0=<nMaxMRU<=16;
// Register the application's document templates. Document templates
// serve as the connection between documents, frame windows and views.
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CMultiMRUDoc),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CMultiMRUView));
AddDocTemplate(pDocTemplate);
// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE;
// The one and only window has been initialized, so show and update it.
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
// No message handlers
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
// App command to run the dialog
void CMultiMRUApp::OnAppAbout()
{
CAboutDlg aboutDlg;
aboutDlg.DoModal();
}
/////////////////////////////////////////////////////////////////////////////
// CMultiMRUApp message handlers
CString CMultiMRUApp::GetExtension(int nIndex)
{
CString szRes;
szRes=szExt[nIndex-1];//注意索引值與下標相差一;
return szRes;
}
void CMultiMRUApp::OnUpdateRecentBMPFileMenu(CCmdUI* pCmdUI)
{
ASSERT_VALID(this);
if(pCmdUI->m_pSubMenu!=NULL)//此時更新"File"菜單的顯示;
{
BOOL bEnable=FALSE;
if (m_pRecentFileList[0] != NULL)
{
bEnable=TRUE;
}
pCmdUI->m_pMenu->EnableMenuItem(pCmdUI->m_nIndex, MF_BYPOSITION |
(bEnable ? MF_ENABLED : (MF_DISABLED | MF_GRAYED)));
return;
}
else //此時更新文件列表表項的顯示;
m_pRecentFileList[0]->UpdateMenu(pCmdUI);
}
void CMultiMRUApp::OnUpdateRecentJPGFileMenu(CCmdUI* pCmdUI)
{
ASSERT_VALID(this);
if(pCmdUI->m_pSubMenu!=NULL) //此時更新"File"菜單的顯示;
{
BOOL bEnable=FALSE;
if (m_pRecentFileList[1] != NULL)
{
bEnable=TRUE;
}
pCmdUI->m_pMenu->EnableMenuItem(pCmdUI->m_nIndex, MF_BYPOSITION |
(bEnable ? MF_ENABLED : (MF_DISABLED | MF_GRAYED)));
return;
}
else //此時更新文件列表表項的顯示;
m_pRecentFileList[1]->UpdateMenu(pCmdUI);
}
BOOL CMultiMRUApp::OnOpenRecentBMPFile(UINT nID)
{
ASSERT_VALID(this);
ASSERT(m_pRecentFileList[0] != NULL);
ASSERT(nID >= ID_FILE_MRU_BMP_FILE1);
ASSERT(nID < ID_FILE_MRU_BMP_FILE1 + (UINT)m_pRecentFileList[0]->GetSize());
int nIndex = nID - ID_FILE_MRU_BMP_FILE1;
ASSERT((*m_pRecentFileList[0])[nIndex].GetLength() != 0);
TRACE2("MRU: open file (%d) '%s'.\n", (nIndex) + 1,
(LPCTSTR)(*m_pRecentFileList[0])[nIndex]);
//打開相應的BMP文件;
if (OpenDocumentFile((*m_pRecentFileList[0])[nIndex]) == NULL)
m_pRecentFileList[0]->Remove(nIndex);
return TRUE;
}
BOOL CMultiMRUApp::OnOpenRecentJPGFile(UINT nID)
{
ASSERT_VALID(this);
ASSERT(m_pRecentFileList[1] != NULL);
ASSERT(nID >= ID_FILE_MRU_JPG_FILE1);
ASSERT(nID < ID_FILE_MRU_JPG_FILE1 + (UINT)m_pRecentFileList[1]->GetSize());
int nIndex = nID - ID_FILE_MRU_JPG_FILE1;
ASSERT((*m_pRecentFileList[1])[nIndex].GetLength() != 0);
TRACE2("MRU: open file (%d) '%s'.\n", (nIndex) + 1,
(LPCTSTR)(*m_pRecentFileList[1])[nIndex]);
//打開相應的JPG文件;
if (OpenDocumentFile((*m_pRecentFileList[1])[nIndex]) == NULL)
m_pRecentFileList[1]->Remove(nIndex);
return TRUE;
}
void CMultiMRUApp::LoadStdProfileSettings(UINT nMaxMRU)
{
ASSERT_VALID(this);
for(int i=0;i<NUM_FILTER;i++)
ASSERT(m_pRecentFileList[i]==NULL);
if (nMaxMRU != 0)
{
// 對每一種擴展名創建CRecentFileList對象,調用ReadList讀入原先的最近文件列表;
for(int i=0;i<NUM_FILTER;i++)
{
m_pRecentFileList[i] = new CRecentFileList(0, _afxFileSection[i], _afxFileEntry[i],
nMaxMRU);
m_pRecentFileList[i]->ReadList();
}
}
// 0 by default means not set
m_nNumPreviewPages = GetProfileInt(_afxPreviewSection, _afxPreviewEntry, 0);
}
//重載保存標準INI文件選項函數;
void CMultiMRUApp::SaveStdProfileSettings()
{
ASSERT_VALID(this);
for(int i=0;i<NUM_FILTER;i++)
if (m_pRecentFileList[i] != NULL)
m_pRecentFileList[i]->WriteList(); //存入注冊表中;
if (m_nNumPreviewPages != 0)
WriteProfileInt(_afxPreviewSection, _afxPreviewEntry, m_nNumPreviewPages);
}
void CMultiMRUApp::AddToRecentFileList(LPCTSTR lpszPathName)
{
ASSERT_VALID(this);
ASSERT(lpszPathName != NULL);
ASSERT(AfxIsValidString(lpszPathName));
CString strFilterExt;
for(int i=1;i<=NUM_FILTER;i++)
{
strFilterExt=GetExtension(i);
LPCTSTR lpszDot=_tcsrchr(lpszPathName,'.');
//如果參數文件的擴展名符合strFilterExt,將其加入相應的最近文件列表;
if (lpszDot != NULL && lstrcmpi(lpszDot, strFilterExt) == 0)
{
if (m_pRecentFileList[i-1] != NULL)
{
m_pRecentFileList[i-1]->Add(lpszPathName);
}
}
}
}
int CMultiMRUApp::ExitInstance()
{
//保存標準設置(主要是最近文件列表);
SaveStdProfileSettings();
return CWinApp::ExitInstance();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -