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

📄 mainfrm.cpp

📁 一个完整的编辑器的代码(很值得参考
💻 CPP
📖 第 1 页 / 共 3 页
字号:
///////////////////////////////////////////////////////////////////////////
//  File:    mainfrm.cpp
//  Version: 1.1.0.4
//  Updated: 19-Jul-1998
//
//  Main frame core
//
//  Copyright:  Ferdinand Prantl, portions by Stcherbatchenko Andrei
//  E-mail:     prantl@ff.cuni.cz
//
//  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
////////////////////////////////////////////////////////////////////////////

// mainfrm.cpp : implementation of the CMainFrame class
//

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

#include "mainfrm.h"
#include "editcmd.h"
#include "registry.h"
#include "editreg.h"
#include "ccrystaltextbuffer.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNAMIC (CMainFrame, CBCGMDIFrameWnd)

#define CONTROL_BARS _T("Control Bars")

const int iMaxUserToolbars = 10;
const UINT uiFirstUserToolBarId	= AFX_IDW_CONTROLBAR_FIRST + 40;
const UINT uiLastUserToolBarId	= uiFirstUserToolBarId + iMaxUserToolbars - 1;

BEGIN_MESSAGE_MAP (CMainFrame, CBCGMDIFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
ON_WM_CREATE ()
ON_WM_CLOSE()
ON_WM_SETFOCUS()
ON_COMMAND(ID_VIEW_FULL_SCREEN, OnViewFullScreen)
ON_UPDATE_COMMAND_UI(ID_VIEW_FULL_SCREEN, OnUpdateViewFullScreen)
ON_COMMAND(ID_FILE_CLOSE_ALL, OnFileCloseAll)
ON_COMMAND(ID_WINDOW_CLOSE, OnCloseWindow)
ON_COMMAND(ID_WINDOW_NEXT, OnNextWindow)
ON_COMMAND(ID_WINDOW_PREV, OnPrevWindow)
ON_UPDATE_COMMAND_UI(ID_WINDOW_CLOSE, OnUpdateCloseWindow)
ON_UPDATE_COMMAND_UI(ID_WINDOW_NEXT, OnUpdateNextWindow)
ON_UPDATE_COMMAND_UI(ID_WINDOW_PREV, OnUpdatePrevWindow)
ON_COMMAND_EX_RANGE(ID_ENCODING_FIRST, ID_ENCODING_LAST, OnEncoding)
ON_UPDATE_COMMAND_UI(ID_ENCODING_FIRST, OnUpdateEncoding)
//}}AFX_MSG_MAP
// Global help commands
ON_COMMAND (ID_HELP_FINDER, CBCGMDIFrameWnd::OnHelpFinder)
ON_COMMAND (ID_HELP, CBCGMDIFrameWnd::OnHelp)
ON_COMMAND (ID_CONTEXT_HELP, CBCGMDIFrameWnd::OnContextHelp)
ON_COMMAND (ID_DEFAULT_HELP, CBCGMDIFrameWnd::OnHelpFinder)
ON_COMMAND (ID_VIEW_CUSTOMIZE, OnViewCustomize)
ON_REGISTERED_MESSAGE (BCGM_RESETTOOLBAR, OnToolbarReset)
ON_REGISTERED_MESSAGE (BCGM_TOOLBARMENU, OnToolbarContextMenu)
ON_COMMAND_EX_RANGE(ID_VIEW_USER_TOOLBAR1, ID_VIEW_USER_TOOLBAR1 + iMaxUserToolbars - 1, OnToolsViewUserToolbar)
ON_UPDATE_COMMAND_UI_RANGE(ID_VIEW_USER_TOOLBAR1, ID_VIEW_USER_TOOLBAR1 + iMaxUserToolbars - 1, OnUpdateToolsViewUserToolbar)
ON_REGISTERED_MESSAGE (BCGM_CUSTOMIZEHELP, OnHelpCustomizeToolbars)
END_MESSAGE_MAP ()

static UINT indicators[] =
  {
    ID_SEPARATOR,            // status line indicator
    // ID_EDIT_INDICATOR_POSITION,
    // ID_EDIT_INDICATOR_COL,
    // ID_INDICATOR_OVR,
    // ID_EDIT_INDICATOR_READ,
    // ID_EDIT_INDICATOR_CRLF,
    ID_INDICATOR_CAPS,
    ID_INDICATOR_NUM,
    ID_INDICATOR_SCRL,
    ID_INDICATOR_TIME
  };

static UINT indicators1[] =
  {
    ID_SEPARATOR,            // status line indicator
    ID_EDIT_INDICATOR_POSITION,
    ID_INDICATOR_OVR,
    ID_EDIT_INDICATOR_READ,
    ID_EDIT_INDICATOR_CRLF,
    ID_INDICATOR_CAPS,
    ID_INDICATOR_NUM,
    ID_INDICATOR_SCRL,
    ID_INDICATOR_TIME
  };

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame ()
{
	m_bFullScreen = false;
}

CMainFrame:: ~ CMainFrame ()
{
}

void CMainFrame::FullScreenOn ()
{
	// available only if there is an active doc
	CMDIChildWnd *pChild = MDIGetActive();
	if (!pChild)
    return;
	// m_bToolBarWasVisible = m_wndToolBar.IsWindowVisible () != FALSE;
  // if (m_bToolBarWasVisible)
	//   m_wndToolBar.ShowWindow (SW_HIDE);
	m_bStatusBarWasVisible = m_wndStatusBar.IsWindowVisible () != FALSE;
  if (m_bStatusBarWasVisible)
	  m_wndStatusBar.ShowWindow (SW_HIDE);
	// first create the new toolbar
	// this will contain the full-screen off button
	m_pwndFullScreenBar = new CToolBar;
	m_pwndFullScreenBar->Create (this);
	m_pwndFullScreenBar->LoadToolBar (IDR_FULLSCREEN);
	m_pwndFullScreenBar->SetBarStyle (m_pwndFullScreenBar->GetBarStyle() |
		CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
	// to look better:
	m_pwndFullScreenBar->ModifyStyle (0, TBSTYLE_FLAT);
	m_pwndFullScreenBar->EnableDocking (0);
	// place the full-screen off button somewhere:
	CPoint pt(600, 440);
	FloatControlBar (m_pwndFullScreenBar, pt);
	// remove the caption of the mainWnd:
	LONG style =:: GetWindowLong (m_hWnd, GWL_STYLE);
	m_bMax = style & WS_MAXIMIZE ? true : false;
	if (m_bMax)
    ShowWindow (SW_RESTORE);
	// now save the old positions of the main and child windows
	GetWindowRect (&m_mainRect);
	style &= ~(WS_CAPTION|WS_MAXIMIZE);
	::SetWindowLong (m_hWnd, GWL_STYLE, style);
	int screenx = GetSystemMetrics (SM_CXSCREEN);
	int screeny = GetSystemMetrics (SM_CYSCREEN);
	// resize:
	SetWindowPos (NULL, -4, -4, screenx + 8, screeny + 8, SWP_NOZORDER);
	style = ::GetWindowLong (pChild->m_hWnd, GWL_STYLE);
	m_bChildMax = style & WS_MAXIMIZE ? true : false;
	// note here: m_bMainMax is not needed since m_hWnd only
	// changed its caption...
	// and maximize the child window
	// it will remove its caption, too.
	pChild->ShowWindow (SW_SHOWMAXIMIZED);
	RecalcLayout ();
	m_bFullScreen = true;
}

void CMainFrame::FullScreenOff ()
{
	// You can use SaveBarState() in OnClose(),
	// so remove the newly added toolbar entirely
	// in order SaveBarState() not
	// to save its state. That is why I used dynamic
	// allocation
	delete m_pwndFullScreenBar;
	LONG style = ::GetWindowLong (m_hWnd, GWL_STYLE);
	style |= WS_CAPTION;
	::SetWindowLong (m_hWnd, GWL_STYLE, style);
	// if (m_bToolBarWasVisible)
	//	m_wndToolBar.ShowWindow (SW_SHOW);
	if (m_bStatusBarWasVisible)
		m_wndStatusBar.ShowWindow (SW_SHOW);
	MoveWindow (&m_mainRect);
	RecalcLayout ();
	if (m_bMax)
    ShowWindow (SW_SHOWMAXIMIZED);
	CMDIChildWnd *pChild = MDIGetActive ();
	// pchild can be NULL if the USER closed all the
	// childs during Full Screen Mode:
	if(pChild)
    {
		  if(m_bChildMax)
			  MDIMaximize (pChild);
		  else
        MDIRestore (pChild);
	  }
	m_bFullScreen = false;
}

int CMainFrame::MDIGetCount ()
{
  ASSERT (::IsWindow (m_hWnd));
  int nCount = 0;
  CWnd * pWnd = GetWindow (GW_CHILD);
  ASSERT (pWnd);
  pWnd = pWnd->GetWindow (GW_CHILD);
  while (pWnd)
    {
      nCount++;
      pWnd = pWnd->GetWindow (GW_HWNDNEXT);
    }
  return nCount;
}

BOOL CMainFrame::
PreCreateWindow (CREATESTRUCT & cs)
{
  CReg reg;
  if (reg.Open (HKEY_CURRENT_USER, REG_EDITPAD_MULTI, KEY_READ))
    {
      // reg.LoadNumber (_T ("ShowFlags"), (DWORD*) &status.flags);
      reg.LoadNumber (_T ("ShowCmd"), (DWORD*) &theApp.m_nCmdShow);

      if (reg.LoadNumber (_T ("Left"), (DWORD*) &cs.x) &&
        reg.LoadNumber (_T ("Right"), (DWORD*) &cs.cx))
        {
          cs.cx -= cs.x;
        }
      if (reg.LoadNumber (_T ("Top"), (DWORD*) &cs.y) &&
        reg.LoadNumber (_T ("Bottom"), (DWORD*) &cs.cy))
        {
          cs.cy -= cs.y;
        }
    }

  if (!CBCGMDIFrameWnd::PreCreateWindow (cs))
    return FALSE;

  return TRUE;
}

void CMainFrame::
ChooseStatusBar (BOOL bActivate)
{
  if (m_bActivated != bActivate)
    {
      if (bActivate)
        m_wndStatusBar.SetIndicators (indicators1, sizeof (indicators1) / sizeof (UINT));
      else
        m_wndStatusBar.SetIndicators (indicators, sizeof (indicators) / sizeof (UINT));
      m_bActivated = bActivate;
    }
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::
AssertValid ()
const
{
  CBCGMDIFrameWnd::AssertValid ();
}

void CMainFrame::Dump (CDumpContext & dc)
const
{
  CBCGMDIFrameWnd::Dump (dc);
}

#endif                          //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers

int CMainFrame::
OnCreate (LPCREATESTRUCT lpCreateStruct)
{
  if (CBCGMDIFrameWnd::OnCreate (lpCreateStruct) == -1)
    return -1;

  //--------------------------
  // Load toolbar user images:
  //--------------------------
  if (!m_UserImages.Load (_T(".\\usrimgs.bmp")))
  {
    TRACE(_T("Failed to load user images\n"));
    ASSERT (FALSE);
  }
  else
  {
    CBCGToolBar::SetUserImages (&m_UserImages);
  }

  CBCGToolBar::EnableQuickCustomization ();

  CList<UINT, UINT>	lstBasicCoomads;

  lstBasicCoomads.AddTail (ID_FILE_NEW);
  lstBasicCoomads.AddTail (ID_FILE_OPEN);
  lstBasicCoomads.AddTail (ID_FILE_CLOSE);
  lstBasicCoomads.AddTail (ID_FILE_SAVE);
  lstBasicCoomads.AddTail (ID_FILE_PRINT);
  lstBasicCoomads.AddTail (ID_APP_EXIT);
  lstBasicCoomads.AddTail (ID_EDIT_UNDO);
  lstBasicCoomads.AddTail (ID_EDIT_CUT);
  lstBasicCoomads.AddTail (ID_EDIT_COPY);
  lstBasicCoomads.AddTail (ID_EDIT_PASTE);
  lstBasicCoomads.AddTail (ID_EDIT_FIND);
  lstBasicCoomads.AddTail (ID_EDIT_REPLACE);
  lstBasicCoomads.AddTail (ID_VIEW_TOOLBAR);
  lstBasicCoomads.AddTail (ID_VIEW_TOGGLE_SRC_HDR);
  lstBasicCoomads.AddTail (ID_VIEW_CUSTOMIZE);
  lstBasicCoomads.AddTail (ID_VIEW_OPTIONS);
  lstBasicCoomads.AddTail (ID_VIEW_OPTIONS_GENERAL);
  lstBasicCoomads.AddTail (ID_WINDOW_NEW);
  lstBasicCoomads.AddTail (ID_WINDOW_NEXT);
  lstBasicCoomads.AddTail (ID_WINDOW_TILE_HORZ);
  lstBasicCoomads.AddTail (ID_WINDOW_CASCADE);
  lstBasicCoomads.AddTail (ID_WINDOW_CLOSE);
  lstBasicCoomads.AddTail (ID_FILE_CLOSE_ALL);
  lstBasicCoomads.AddTail (ID_APP_ABOUT);
  lstBasicCoomads.AddTail (ID_HELP_FINDER);

  CBCGToolBar::SetBasicCommands (lstBasicCoomads);
  if (!m_wndMenuBar.Create (this))
  {
    TRACE0 ("Failed to create menubar\n");
    return -1;      // fail to create
  }

  m_wndMenuBar.SetBarStyle (m_wndMenuBar.GetBarStyle() | CBRS_SIZE_DYNAMIC);
  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

    }

  m_bActivated = FALSE;

  CString strMainToolbarTitle;
  strMainToolbarTitle.LoadString (IDS_MAIN_TOOLBAR);
  m_wndToolBar.SetWindowText (strMainToolbarTitle);
  m_wndMenuBar.EnableDocking (CBRS_ALIGN_ANY);
  m_wndToolBar.EnableDocking (CBRS_ALIGN_ANY);
  EnableDocking (CBRS_ALIGN_ANY);
  m_wndToolBar.EnableCustomizeButton (TRUE, ID_VIEW_CUSTOMIZE, _T("Customize..."));
  DockControlBar (&m_wndMenuBar);
  DockControlBar (&m_wndToolBar);

  CString strControlBarRegEntry = REGISTRY_ROOT;
  strControlBarRegEntry += CONTROL_BARS;

⌨️ 快捷键说明

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