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

📄 edit.cpp

📁 一个很好的富文本编辑程序,类似于windows中的写字板但并不同
💻 CPP
字号:
// Edit.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "Edit.h"
#include "KSFileDialog.h"
#include "MainFrm.h"
#include "ChildFrm.h"
#include "EditDoc.h"
#include "EditView.h"
#include "StatLink.h"

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

/////////////////////////////////////////////////////////////////////////////
// CEditApp

BEGIN_MESSAGE_MAP(CEditApp, CWinApp)
	//{{AFX_MSG_MAP(CEditApp)
	ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
		ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
	ON_UPDATE_COMMAND_UI(ID_FILE_MRU_FILE1, OnUpdateFileMruFile1)
	ON_COMMAND(ID_FILE_SAVE_ALL, OnFileSaveAll)
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CEditApp construction

CEditApp::CEditApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CEditApp object

CEditApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CEditApp initialization

BOOL CEditApp::InitInstance()
{
	// Initialize OLE libraries
	if (!AfxOleInit())
	{
		AfxMessageBox(IDP_OLE_INIT_FAILED);
		return FALSE;
	}

	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("Sameer Edit"));

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

	// Register the application's document templates.  Document templates
	//  serve as the connection between documents, frame windows and views.
	CMainFrame* pMainFrame = new CMainFrame;
	if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
		return FALSE;
	m_pMainWnd = pMainFrame;
	
	CMultiDocTemplate* pDocTemplate;
	pDocTemplate = new CMultiDocTemplate(
		IDR_EDITTYPE,
		RUNTIME_CLASS(CEditDoc),
		RUNTIME_CLASS(CChildFrame), // custom MDI child frame
		RUNTIME_CLASS(CMyEditView));
	pDocTemplate->SetContainerInfo(IDR_EDITTYPE_CNTR_IP);
	AddDocTemplate(pDocTemplate);


	// create main MDI Frame window

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

	// Enable DDE Execute open
	EnableShellOpen();
	RegisterShellFileTypes(TRUE);

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

		if (!cmdInfo.m_bRunEmbedded)
	{
		m_pMainWnd->UpdateWindow();

		if (!m_pMainWnd->IsIconic()  &&
			m_splash.Create(m_pMainWnd))
		{
			m_splash.ShowWindow(SW_SHOW);
			m_splash.UpdateWindow();
			m_splash.SetTimer(1, 500, NULL);
			Sleep(500);
			m_splash.DestroyWindow();
		}
	}

	// The main window has been initialized, so show and update it.
	ReadConfig(); 
		
	m_pMainWnd->SetWindowPlacement(&m_WP);

	pMainFrame->ShowWindow(m_nCmdShow);
	pMainFrame->UpdateWindow();

	return TRUE;
}


/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
protected:
	CStaticLink	m_wndLink[3];			 // static controls with hyperlinks
public:
	CAboutDlg() : CDialog(IDD_ABOUTBOX) { }
	virtual BOOL OnInitDialog();
};


BOOL CAboutDlg::OnInitDialog() 
{

	// TODO: Add extra initialization here
	m_wndLink[1].SubclassDlgItem(IDC_EMAIL, this,
		_T("mailto:sabbasi00@hotmail.com"));
	
	m_wndLink[2].SubclassDlgItem(IDC_STATIC3, this,
		_T("http://www.CodeGuru.com"));


	return CDialog::OnInitDialog();

}
// App command to run the dialog
void CEditApp::OnAppAbout()
{
	CAboutDlg aboutDlg;
	aboutDlg.DoModal();
}

/////////////////////////////////////////////////////////////////////////////
// CSplashWnd dialog

BEGIN_MESSAGE_MAP(CSplashWnd, CDialog)
	//{{AFX_MSG_MAP(CSplashWnd)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

void CSplashWnd::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSplashWnd)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BOOL CSplashWnd::Create(CWnd* pParent)
{
	//{{AFX_DATA_INIT(CSplashWnd)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT

	if (!CDialog::Create(CSplashWnd::IDD, pParent))
	{
		TRACE0("Warning: creation of CSplashWnd dialog failed\n");
		return FALSE;
	}

	return TRUE;
}

BOOL CSplashWnd::OnInitDialog()
{
	CDialog::OnInitDialog();
	CenterWindow();

	// initialize the big icon control
	m_icon.SubclassDlgItem(IDC_BIGICON, this);
	m_icon.SizeToContent();

	return TRUE;  // return TRUE  unless you set the focus to a control
}

/////////////////////////////////////////////////////////////////////////////
// CBigIcon

BEGIN_MESSAGE_MAP(CBigIcon, CButton)
	//{{AFX_MSG_MAP(CBigIcon)
	ON_WM_DRAWITEM()
	ON_WM_ERASEBKGND()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBigIcon message handlers

#define CY_SHADOW   4
#define CX_SHADOW   4

void CBigIcon::SizeToContent()
{
	int cxIcon = ::GetSystemMetrics(SM_CXICON);
	int cyIcon = ::GetSystemMetrics(SM_CYICON);

	SetWindowPos(NULL, 0, 0, cxIcon*2 + CX_SHADOW + 4, cyIcon*2 + CY_SHADOW + 4,
		SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOZORDER);
}

void CBigIcon::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
	CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
	ASSERT(pDC != NULL);

	CRect rect;
	GetClientRect(rect);
	int cxClient = rect.Width();
	int cyClient = rect.Height();

	HICON hicon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	if (hicon == NULL)
		return;

	int cxIcon = ::GetSystemMetrics(SM_CXICON);
	int cyIcon = ::GetSystemMetrics(SM_CYICON);

	CBitmap bitmap;
	if (!bitmap.CreateCompatibleBitmap(pDC, cxIcon, cyIcon))
		return;
	CDC dcMem;
	if (!dcMem.CreateCompatibleDC(pDC))
		return;
	CBitmap* pBitmapOld = dcMem.SelectObject(&bitmap);
	if (pBitmapOld == NULL)
		return;

	dcMem.StretchBlt(0, 0, cxIcon, cyIcon, pDC,
		2, 2, cxClient-CX_SHADOW-4, cyClient-CY_SHADOW-4, SRCCOPY);

	dcMem.DrawIcon(0, 0, hicon);

	CPen pen;
	pen.CreateStockObject(BLACK_PEN);
	CPen* pPenOld = pDC->SelectObject(&pen);
	pDC->Rectangle(0, 0, cxClient-CX_SHADOW, cyClient-CY_SHADOW);
	if (pPenOld)
		pDC->SelectObject(pPenOld);

	CBrush br;
	br.CreateStockObject(DKGRAY_BRUSH);
	rect.SetRect(cxClient-CX_SHADOW, CY_SHADOW, cxClient, cyClient);
	pDC->FillRect(rect, &br);
	rect.SetRect(CX_SHADOW, cyClient-CY_SHADOW, cxClient, cyClient);
	pDC->FillRect(rect, &br);

	pDC->StretchBlt(2, 2, cxClient-CX_SHADOW-4, cyClient-CY_SHADOW-4,
		&dcMem, 0, 0, cxIcon, cyIcon, SRCCOPY);
}

BOOL CBigIcon::OnEraseBkgnd(CDC*)
{
	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////


BOOL CEditApp::DoPromptFileName(CString& fileName, UINT nIDSTitle, DWORD lFlags,
			      BOOL bOpenFileDialog, CDocTemplate* pTemplate)
{
	CKSFileDialog dlgFile(bOpenFileDialog);
	dlgFile.SetAppPointer(this);
	dlgFile.SetStringFilter("Edit Files(*.edt)|*.edt|Rich Text Format(*.rtf)|*.rtf|Text File(*.txt)|*.txt|All Files(*.*)|*.*");

	dlgFile.SetMyParent(AfxGetMainWnd());
	return dlgFile.DoPromptFileName(fileName,nIDSTitle,lFlags,bOpenFileDialog, pTemplate);
}
/////////////////////////////////////////////////////////////////////////////
// CEditApp message handlers

void CEditApp::OnFileOpen() 
{
	// TODO: Add your command handler code here
		CString newName;
	if (!DoPromptFileName(newName, AFX_IDS_OPENFILE,
	   OFN_FILEMUSTEXIST|OFN_HIDEREADONLY, TRUE, NULL))
		return; // open cancelled

	OpenDocumentFile(newName);	// TODO: Add your command handler code here
}

void CEditApp::OnUpdateFileMruFile1(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	if (pCmdUI->m_pSubMenu!=NULL)
	{	
		return;
	}
	
	CWinApp::OnUpdateRecentFileMenu(pCmdUI);
	return;
}

void CEditApp::OnFileSaveAll() 
{
	// TODO: Add your command handler code here
  POSITION pos = GetFirstDocTemplatePosition ();
  ASSERT (pos);
  CDocTemplate *pDocTempl = GetNextDocTemplate (pos);
  pos = pDocTempl->GetFirstDocPosition ();
  while (pos)
    {
      CEditDoc *pDoc = (CEditDoc*) pDocTempl->GetNextDoc (pos);
      ASSERT (pDoc);
      CString sDocPath = pDoc->GetPathName ();
      if (pDoc->IsModified ())
        pDoc->DoSave (sDocPath);
    }
}

int CEditApp::ExitInstance() 
{
	WriteConfig();
		
	// any other code you need before exit
	
	return CWinApp::ExitInstance();
}

const WINDOWPLACEMENT& CEditApp::GetWP(void) const
{
	return m_WP;
}

void CEditApp::PutWP(const WINDOWPLACEMENT& newval)
{	
	m_WP=newval;
	m_WP.length=sizeof(m_WP);
}

void CEditApp::ReadConfig(void)
{
	ReadWindowConfig();
}

void CEditApp::WriteConfig(void)
{
	WriteWindowConfig();
}

void CEditApp::ReadWindowConfig(void)
{
	WINDOWPLACEMENT wp;
	
	wp.length=sizeof(wp);
	m_pMainWnd->GetWindowPlacement(&wp);

	m_WP.length=sizeof(m_WP);
	m_WP.showCmd=GetProfileInt("Window","show",wp.showCmd);
	m_WP.flags=GetProfileInt("Window","flags",wp.flags);
	m_WP.ptMinPosition.x=GetProfileInt("Window","minposx",
		wp.ptMinPosition.x);
	m_WP.ptMinPosition.y=GetProfileInt("Window","minposy",
		wp.ptMinPosition.y);
	m_WP.ptMaxPosition.x=GetProfileInt("Window","maxposx",
		wp.ptMaxPosition.x);
	m_WP.ptMaxPosition.y=GetProfileInt("Window","maxposy",
		wp.ptMaxPosition.y);
	m_WP.rcNormalPosition.left=GetProfileInt("Window","left",
		wp.rcNormalPosition.left);
	m_WP.rcNormalPosition.top=GetProfileInt("Window","top",
		wp.rcNormalPosition.top);
	m_WP.rcNormalPosition.right=GetProfileInt("Window","right",
		wp.rcNormalPosition.right);
	m_WP.rcNormalPosition.bottom=GetProfileInt("Window","bottom",
		wp.rcNormalPosition.bottom);

	CRect rcTemp;
	CRect rcScreen(0,0,GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN));

	::SystemParametersInfo(SPI_GETWORKAREA,0,&rcScreen,0);

	rcTemp=m_WP.rcNormalPosition;
	rcTemp.NormalizeRect();
	m_WP.rcNormalPosition=rcTemp;

	if(::IntersectRect(&m_WP.rcNormalPosition, &rcTemp, &rcScreen))
	{
		if(rcTemp!=m_WP.rcNormalPosition)
		{	// some part of the window is off the screen
			if(rcTemp.left<rcScreen.left) 		// off the left edge
				rcTemp.OffsetRect(rcScreen.left-rcTemp.left,0);
			if(rcTemp.right>rcScreen.right) 	// off the right edge
				rcTemp.OffsetRect(rcScreen.right-rcTemp.right,0);
			if(rcTemp.left<rcScreen.left) 		// won't fit, shrink
				rcTemp.left=rcScreen.left;
			if(rcTemp.top<rcScreen.top) 		// off the top edge
				rcTemp.OffsetRect(0,rcScreen.top-rcTemp.top);
			if(rcTemp.bottom>rcScreen.bottom) 	// off the bottom edge
				rcTemp.OffsetRect(0,rcScreen.bottom-rcTemp.bottom);
			if(rcTemp.top<rcScreen.top) 		// won't fit, shrink
				rcTemp.top=rcScreen.top;
			m_WP.rcNormalPosition=rcTemp;
		}
	}
	else
	{
		m_WP=wp;
	}

	if(m_WP.showCmd==SW_MINIMIZE && wp.showCmd!=SW_MINIMIZE)
	{
		m_WP.showCmd=SW_RESTORE;
	}

	if(m_WP.showCmd!=SW_MAXIMIZE && wp.showCmd==SW_MAXIMIZE)
	{
		m_WP.showCmd=SW_MAXIMIZE;
	}
}

void CEditApp::WriteWindowConfig(void)
{
	WriteProfileInt("Window","show",m_WP.showCmd);
	WriteProfileInt("Window","flags",m_WP.flags);
	WriteProfileInt("Window","minposx",m_WP.ptMinPosition.x);
	WriteProfileInt("Window","minposy",m_WP.ptMinPosition.y);
	WriteProfileInt("Window","maxposx",m_WP.ptMaxPosition.x);
	WriteProfileInt("Window","maxposy",m_WP.ptMaxPosition.y);
	WriteProfileInt("Window","left",m_WP.rcNormalPosition.left);
	WriteProfileInt("Window","top",m_WP.rcNormalPosition.top);
	WriteProfileInt("Window","right",m_WP.rcNormalPosition.right);
	WriteProfileInt("Window","bottom",m_WP.rcNormalPosition.bottom);
}

⌨️ 快捷键说明

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