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

📄 editpad.cpp

📁 一个完整的编辑器的代码(很值得参考
💻 CPP
📖 第 1 页 / 共 2 页
字号:
///////////////////////////////////////////////////////////////////////////
//  File:    editpad.cpp
//  Version: 1.1.0.4
//  Updated: 19-Jul-1998
//
//  Copyright:  Ferdinand Prantl, portions by Stcherbatchenko Andrei
//  E-mail:     prantl@ff.cuni.cz
//
//  Application core
//
//  You are free to use or modify this code to the following restrictions:
//  - Acknowledge me somewhere in your about box, simple "Parts of code by.."
//  will be enough. If you can't (or don't want to), contact me personally.
//  - LEAVE THIS HEADER INTACT
////////////////////////////////////////////////////////////////////////////

// editpad.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "editpad.h"

#include "mainfrm.h"
#include "childfrm.h"
#include "editpaddoc.h"
#include "editpadview.h"
#include "childric.h"
#include "epricdoc.h"
#include "epricvw.h"
#include "childbin.h"
#include "epbindoc.h"
#include "epbinvw.h"
#include "editcmd.h"
#include "memcombo.h"
#include "registry.h"
#include "editreg.h"
#include "generpg.h"
#include "editoroptionspage.h"
#include "fontpg.h"
#include "splash.h"
#include "tipdlg.h"
#include "aboutdlg.h"
#include "edfopdlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CEditPadApp

BEGIN_MESSAGE_MAP (CEditPadApp, CWinApp)
//{{AFX_MSG_MAP(CEditPadApp)
ON_COMMAND (ID_APP_ABOUT, OnAppAbout)
ON_COMMAND(ID_HELP_TIPOFTHEDAY, OnTipOfTheDay)
ON_COMMAND (ID_FILE_SAVE_ALL, OnFileSaveAll)
ON_COMMAND(ID_VIEW_OPTIONS_GENERAL, OnViewOptionsGeneral)
ON_UPDATE_COMMAND_UI (ID_TOOLS_SPELLING_SET, OnUpdateToolsSpellingSet)
ON_COMMAND (ID_TOOLS_SPELLING_SET, OnToolsSpellingSet)
//}}AFX_MSG_MAP
// Standard file based document commands
ON_COMMAND (ID_FILE_NEW, OnFileNew)
ON_COMMAND (ID_FILE_OPEN, OnFileOpen)
// Standard print setup command
ON_COMMAND (ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
END_MESSAGE_MAP ()

/////////////////////////////////////////////////////////////////////////////
// CEditPadApp construction

CEditPadApp::CEditPadApp () : m_dwFlags (EP_OPEN_LAST_DOCS|EP_CREATE_NEW_DOC_IF_ALONE|EP_SAVE_SETS_ON_EXIT|EP_NOTIFY_CHANGES|EP_ADVANCED_UI|EP_REMEMBER_LASTPOS)
{
  // TODO: add construction code here,
  // Place all significant initialization in InitInstance
}

DWORD CEditPadApp::
GetFlags ()
{
  return m_dwFlags;
}

void CEditPadApp::
SetFlags (DWORD dwFlags)
{
  if (m_dwFlags != dwFlags)
    {
      m_dwFlags = dwFlags;
    }
}

void CEditPadApp::
LoadSettings ()
{
  CEditPadView::LoadSettings ();
  CMemComboBox::LoadSettings ();
  *CEditPadView::szWIspellPath = _T ('\0');
  CReg reg;
  if (reg.Open (HKEY_CURRENT_USER, REG_EDITPAD_MULTI, KEY_READ))
    {
      reg.LoadString (_T ("LastFiles"), m_sLastDocs);
      reg.LoadNumber (_T ("Flags"), &m_dwFlags);
    }
  reg.Close ();
  if (reg.Open (HKEY_CURRENT_USER, REG_EDITPAD, KEY_READ))
    {
      reg.LoadString (_T ("WIspellPath"), CEditPadView::szWIspellPath, _MAX_PATH);
    }
}

void CEditPadApp::
LoadLastDocs ()
{
  int pos;
  while ((pos = m_sLastDocs.Find (_T ('\n'))) != -1)
    {
      OpenDocumentFile (m_sLastDocs.Left (pos));
      m_sLastDocs = m_sLastDocs.Mid (pos + 1);
    }
  if (*(LPCTSTR) m_sLastDocs)
    {
      OpenDocumentFile (m_sLastDocs);
      m_sLastDocs.Empty ();
    }
}

void CEditPadApp::
SaveSettings ()
{
  CEditPadView::SaveSettings ();
  CMemComboBox::SaveSettings ();
  CReg reg;
  if (reg.Create (HKEY_CURRENT_USER, REG_EDITPAD_MULTI, KEY_WRITE))
    {
      VERIFY (reg.SaveString (_T ("LastFiles"), m_sLastDocs));
      VERIFY (reg.SaveNumber (_T ("Flags"), m_dwFlags));
    }
  reg.Close ();
  if (reg.Create (HKEY_CURRENT_USER, REG_EDITPAD, KEY_WRITE))
    {
      VERIFY (reg.SaveString (_T ("WIspellPath"), CEditPadView::szWIspellPath));
    }
  reg.Close ();
  struct
    {
      TCHAR path [_MAX_PATH];
      time_t stamp;
    } oOdd[REMEMBERED_TO_REMOVE];
  time_t stamp;
  time (&stamp);
  stamp += 1000;
  int i;
  for (i = 0; i < REMEMBERED_TO_REMOVE; i++)
    oOdd[i].stamp = stamp;
  CString sKey = REG_EDITPAD;
  sKey += _T ("\\Remembered");
  DWORD nEntries;
  LPCTSTR pszPath;
  RegVal vValue;
  if (reg.Open (HKEY_CURRENT_USER, sKey, KEY_READ|KEY_WRITE) && reg.HasEntries (NULL, &nEntries) && nEntries > MAX_REMEMBERED && reg.FindFirstValue (pszPath, &vValue))
    {
      DWORD dwLastPos[3];
      int nDelta, nMaxDelta, nPos;
      do
        {
          if (RegValGetBinary (&vValue, (LPBYTE) dwLastPos, sizeof (dwLastPos)))
            {
              nMaxDelta = 0;
              nPos = -1;
              for (i = 0; i < REMEMBERED_TO_REMOVE; i++)
                {
                  nDelta = oOdd[i].stamp - *dwLastPos;
                  if (nDelta > nMaxDelta)
                    {
                      nMaxDelta = nDelta;
                      nPos = i;
                    }
                }
              if (nPos >= 0)
                {
                  _tcscpy (oOdd[nPos].path, pszPath);
                  oOdd[nPos].stamp = *dwLastPos;
                }
            }
        }
      while (reg.FindNextValue (pszPath, &vValue));
      reg.FindClose ();
      for (i = 0; i < REMEMBERED_TO_REMOVE; i++)
        reg.DeleteKey (oOdd[i].path);
    }
}

void CEditPadApp::
SaveLastDocs ()
{
  POSITION pos = GetFirstDocTemplatePosition ();
  ASSERT (pos);
  CDocTemplate *pDocTempl = GetNextDocTemplate (pos);
  pos = pDocTempl->GetFirstDocPosition ();
  CString name, names;
  while (pos)
    {
      CDocument *pDoc = pDocTempl->GetNextDoc (pos);
      name = pDoc->GetPathName ();
      if (!name.IsEmpty ())
        {
          names += name + _T ('\n');
        }
    }
  m_sLastDocs = names;
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CEditPadApp object

CEditPadApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CEditPadApp initialization

BOOL CEditPadApp::
InitInstance ()
{
	CReg reg;
	if (reg.Open (HKEY_CURRENT_USER, REG_EDITPAD_MULTI, KEY_READ|KEY_WRITE))
		{
      DWORD dwVersion;
      if (!reg.LoadNumber (NULL, _T ("Version"), &dwVersion) || dwVersion < REG_EDITPAD_VERSION)
        {
          reg.DeleteSubKeys ();
          VERIFY (reg.SaveNumber (_T ("Version"), (DWORD) REG_EDITPAD_VERSION));
	      }
    }
  reg.Close ();

  // 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.

  // Initialize the common controls library
  /*INITCOMMONCONTROLSEX icex;
  icex.dwSize = sizeof( icex );
  icex.dwICC = ICC_USEREX_CLASSES;
  VERIFY( InitCommonControlsEx( &icex ) );*/

  free ((void*) m_pszHelpFilePath);
  m_pszHelpFilePath = _tcsdup (_T ("editpadm.hlp"));

  // initialized OLE 2.0 libraries
  if (!AfxOleInit ())
    {
      AfxMessageBox (IDS_OLE_INIT_FAILED);
      return FALSE;
    }

#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 ("EditPad\\Multi"));

  LoadStdProfileSettings (16);  // Load standard INI file options (including MRU)
  LoadSettings ();

  // Parse command line for standard shell commands, DDE, file open
  CCommandLineInfo cmdInfo;
  ParseCommandLine (cmdInfo);

	CSplashWnd splash;
	BOOL bSplash = cmdInfo.m_bShowSplash && (m_dwFlags & EP_NO_SPLASH) == 0;
	if (!cmdInfo.m_bRunEmbedded)
	{
		switch (m_nCmdShow)
		{
			case SW_HIDE:
			case SW_SHOWMINIMIZED:
			case SW_MINIMIZE:
			case SW_SHOWMINNOACTIVE:
				bSplash = FALSE;
		}
	}
	else
	{
		//Excel 4 will start OLE servers minimized
		m_nCmdShow = SW_SHOWNORMAL;
	}
	if (bSplash)
	{
		// only show splash if not embedded
		splash.Create(NULL);
		splash.ShowWindow(SW_SHOW);
		splash.UpdateWindow();
	}

	// Initialize RichEdit control
	if (LoadLibrary(_T("RICHED32.DLL")) == NULL)
	{
		AfxMessageBox(IDS_RICHED_LOAD_FAIL, MB_OK|MB_ICONEXCLAMATION);
		return FALSE;
	}

  // Register the application's document templates.  Document templates
  //  serve as the connection between documents, frame windows and views.

  CMultiDocTemplate *pDocTemplate;
  pDocTemplate = new CMultiDocTemplate (
                   IDR_EDITPADTYPE,
                   RUNTIME_CLASS (CEditPadDoc),
                   RUNTIME_CLASS (CChildFrame),   // custom MDI child frame
                   RUNTIME_CLASS (CEditPadView));
  AddDocTemplate (pDocTemplate);

  // create main MDI Frame window
  CMDIFrameWnd *pMainFrame;
  if (m_dwFlags & EP_ADVANCED_UI)
    pMainFrame = new CMainFrame;
  else
    pMainFrame = new CMainFrame2;
  if (!pMainFrame->LoadFrame (IDR_MAINFRAME))
    return FALSE;
  m_pMainWnd = pMainFrame;

  // Enable DDE Execute open
  //  EnableShellOpen();
  //  RegisterShellFileTypes(TRUE);
  if (m_dwFlags & EP_OPEN_LAST_DOCS)
    {
      LoadLastDocs ();
    }

  // Dispatch commands specified on the command line
  if (!ProcessShellCommand (cmdInfo))
    return FALSE;

  /*CMultiDocTemplate *pRichDocTemplate;
  pRichDocTemplate = new CMultiDocTemplate (
                   IDR_EDITPADRICHTYPE,
                   RUNTIME_CLASS (CEditPadRichDoc),
                   RUNTIME_CLASS (CRichChildFrame),
                   RUNTIME_CLASS (CEditPadRichView));
  AddDocTemplate (pRichDocTemplate);*/

  CMultiDocTemplate *pBinDocTemplate;
  pBinDocTemplate = new CMultiDocTemplate (
                   IDR_EDITPADBINTYPE,
                   RUNTIME_CLASS (CEditPadBinDoc),
                   RUNTIME_CLASS (CBinChildFrame),
                   RUNTIME_CLASS (CEditPadBinView));
  AddDocTemplate (pBinDocTemplate);

  // Enable drag/drop open
  m_pMainWnd->DragAcceptFiles ();

  if (!(m_dwFlags & EP_CREATE_NEW_DOC_ALWAYS))
    {
      POSITION pos = pDocTemplate->GetFirstDocPosition ();
      ASSERT (pos);
      CDocument *pDoc;
      CString sPath;
      bool bAlone = true;
      do
        {
          pDoc = pDocTemplate->GetNextDoc (pos);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -