⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 multimru.cpp

📁 C++教程的配套源码 C++教程的配套源码
💻 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 + -