Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Курсова_робота_СП.doc
Скачиваний:
0
Добавлен:
27.12.2019
Размер:
2.3 Mб
Скачать

Додаток 2. Код MainFrm.Cpp

// MainFrm.cpp : implementation of the CMainFrame class

//

#include "stdafx.h"

#include "TextEdit.h"

#include "MainFrm.h"

#ifdef _DEBUG

#define new DEBUG_NEW

#endif

IMPLEMENT_DYNAMIC(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)

ON_WM_CREATE()

ON_WM_SETFOCUS()

ON_WM_DROPFILES()

ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)

ON_COMMAND(ID_EDIT_CUT, OnEditCut)

ON_COMMAND(ID_EDIT_COPY, OnEditCopy)

ON_COMMAND(ID_FILE_PRINT, OnFilePrint)

ON_COMMAND(ID_FILE_OPEN, OnFileOpen)

ON_COMMAND(ID_FILE_SAVE, OnFileSave)

ON_COMMAND(ID_FILE_NEW, OnFileNew)

ON_COMMAND(ID_FILE_SAVEAS, OnFileSaveas)

ON_COMMAND(ID_EDIT_UNDO, OnEditUndo)

END_MESSAGE_MAP()

static UINT indicators[] =

{

ID_SEPARATOR, // status line indicator

ID_INDICATOR_CAPS,

ID_INDICATOR_NUM,

ID_INDICATOR_SCRL,

};

CMainFrame::CMainFrame()

{

m_strPathname = "";

}

CMainFrame::~CMainFrame()

{

}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)

{

if (CFrameWnd::OnCreate(lpCreateStruct) == -1)

return -1;

if (!m_RichEdit.Create(WS_CHILD | WS_VISIBLE | ES_AUTOVSCROLL | ES_AUTOHSCROLL |

ES_AUTOHSCROLL | ES_MULTILINE | WS_HSCROLL | WS_VSCROLL,

CRect(0,0,0,0), this, AFX_IDW_PANE_FIRST))

{

TRACE0("Failed to create RichEdit window\n");

return -1;

}

::SetWindowLong(m_RichEdit.GetSafeHwnd(),GWL_EXSTYLE,WS_EX_CLIENTEDGE);

if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE |

CBRS_TOP | 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

}

Bar

UINT nID, nStyle;

int cxWidth;

m_wndStatusBar.GetPaneInfo( 0, nID, nStyle, cxWidth);

m_wndStatusBar.SetPaneInfo( 0, nID, SBPS_NORMAL | SBPS_STRETCH, cxWidth);

CHARFORMAT cfDefault;

cfDefault.cbSize = sizeof(cfDefault);

cfDefault.dwEffects = CFE_PROTECTED;

cfDefault.dwMask = CFM_BOLD | CFM_FACE | CFM_SIZE | CFM_CHARSET | CFM_PROTECTED;

cfDefault.yHeight = 200;

cfDefault.bCharSet = 0x00;

strcpy(cfDefault.szFaceName, _T("Courier"));

m_RichEdit.SetDefaultCharFormat(cfDefault);

m_RichEdit.SetEventMask(ENM_CHANGE | ENM_SELCHANGE | ENM_PROTECTED);

DragAcceptFiles();

SetWindowTitle();

return 0;

}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)

{

if( !CFrameWnd::PreCreateWindow(cs)

return FALSE;

cs.dwExStyle &= ~WS_EX_CLIENTEDGE; // Визначає, що вікно має рамку з

поглибленим краєм

cs.lpszClass = AfxRegisterWndClass(0,0,0,LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(IDR_MAINFRAME)));

return TRUE;

}

#ifdef _DEBUG

void CMainFrame::AssertValid() const

{

CFrameWnd::AssertValid();

}

void CMainFrame::Dump(CDumpContext& dc) const

{

CFrameWnd::Dump(dc);

}

#endif //_DEBUG

void CMainFrame::OnSetFocus(CWnd* pWnd)

{

m_RichEdit.SetFocus();

}

BOOL CMainFrame::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)

{

if (m_RichEdit.OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))

return TRUE;

CFrameWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);

}

void CMainFrame::OnEditPaste()

{

m_RichEdit.PasteSpecial(CF_TEXT);

}

void CMainFrame::OnEditCut()

{

m_RichEdit.Cut();

}

void CMainFrame::OnEditCopy()

{

m_RichEdit.Copy();

}

void CMainFrame::OnEditUndo()

{

m_RichEdit.Undo();

}

void CMainFrame::OnFilePrint()

{

Print(true);

}

void CMainFrame::Print(bool bShowPrintDialog)

{

CPrintDialog printDialog(false);

if (bShowPrintDialog)

{

if(printDialog.DoModal() == IDCANCEL)

return; // User pressed cancel, don't print.

}

else

{

printDialog.GetDefaults();

}

HDC hPrinterDC = printDialog.GetPrinterDC();

FORMATRANGE fr;

int nHorizRes = GetDeviceCaps(hPrinterDC, HORZRES);

int nVertRes = GetDeviceCaps(hPrinterDC, VERTRES);

int nLogPixelsX = GetDeviceCaps(hPrinterDC, LOGPIXELSX);

int nLogPixelsY = GetDeviceCaps(hPrinterDC, LOGPIXELSY);

LONG lTextLength; // Length of document.

LONG lTextPrinted; // Amount of document printed.

SetMapMode ( hPrinterDC, MM_TEXT );

ZeroMemory(&fr, sizeof(fr));

fr.hdc = fr.hdcTarget = hPrinterDC;

fr.rcPage.left = fr.rcPage.top = 0;

fr.rcPage.right = (nHorizRes/nLogPixelsX) * 1440;

fr.rcPage.bottom = (nVertRes/nLogPixelsY) * 1440;

// Set up 0" margins all around.

fr.rc.left = fr.rcPage.left ;//+ 1440; // 1440 TWIPS = 1 inch.

fr.rc.top = fr.rcPage.top ;//+ 1440;

fr.rc.right = fr.rcPage.right ;//- 1440;

fr.rc.bottom = fr.rcPage.bottom ;//- 1440;

// Default the range of text to

print as the entire document.

fr.chrg.cpMin = 0;

fr.chrg.cpMax = -1;

m_RichEdit.FormatRange(&fr,true);

DOCINFO di;

ZeroMemory(&di, sizeof(di));

di.cbSize = sizeof(DOCINFO);

di.lpszDocName = m_strPathname;

di.lpszOutput = NULL;

StartDoc(hPrinterDC, &di);

lTextLength = m_RichEdit.GetTextLength();

do

{

StartPage(hPrinterDC);

lTextPrinted =m_RichEdit.FormatRange(&fr,true);

m_RichEdit.DisplayBand(&fr.rc );

EndPage(hPrinterDC);

if (lTextPrinted < lTextLength)

{

fr.chrg.cpMin = lTextPrinted;

fr.chrg.cpMax = -1;

}

}

while (lTextPrinted < lTextLength);

m_RichEdit.FormatRange(NULL,false);

EndDoc (hPrinterDC);

DeleteDC(hPrinterDC);

}

void CMainFrame::OnDropFiles(HDROP hDropInfo)

{

CString FileName;

::DragQueryFile(hDropInfo, 0, FileName.GetBufferSetLength(_MAX_PATH),_MAX_PATH);

FileName.ReleaseBuffer();

::DragFinish(hDropInfo);

m_strPathname = FileName;

SetWindowTitle();

ReadFile();

}

void CMainFrame::ReadFile()

{

CString FileName = m_strPathname;

FileName.Replace(_T("\\"), _T("\\\\"));

CFile cFile(FileName, CFile::modeRead);

EDITSTREAM es;

es.dwCookie = (DWORD) &cFile;

es.pfnCallback = (EDITSTREAMCALLBACK) MyStreamInCallback;

m_RichEdit.StreamIn(SF_TEXT,es); // Perform the streaming

}

void CMainFrame::WriteFile()

{

CString Pathname = m_strPathname;

Pathname.Replace(_T("\\"), _T("\\\\"));

CFile cFile(Pathname, CFile::modeCreate|CFile::modeWrite);

EDITSTREAM es;

es.dwCookie = (DWORD) &cFile;

es.pfnCallback = (EDITSTREAMCALLBACK) MyStreamOutCallback;

m_RichEdit.StreamOut(SF_TEXT,es); // Perform the streaming

}

void CMainFrame::OnFileOpen()

{

char szFilters[]= "Text Files (*.txt)|*.txt|All Files (*.*)|*.*||";

CFileDialog fileDlg (TRUE, "txt", "*.txt", OFN_FILEMUSTEXIST| OFN_HIDEREADONLY, szFilters, this);

if( fileDlg.DoModal ()==IDOK )

{

m_strPathname = fileDlg.GetPathName();

SetWindowTitle();

ReadFile();

}

}

DWORD CALLBACK MyStreamInCallback(CFile* dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)

{

CFile* pFile = (CFile*) dwCookie;

*pcb = pFile->Read(pbBuff, cb);

return 0;

}

DWORD CALLBACK MyStreamOutCallback(CFile* dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)

{

CFile* pFile = (CFile*) dwCookie;

pFile->Write(pbBuff, cb);

*pcb = cb;

return 0;

}

void CMainFrame::OnFileSave()

{

if (m_strPathname == "") OnFileSaveas();

else WriteFile();

}

void CMainFrame::OnFileNew()

{

m_RichEdit.SetWindowText("");

m_strPathname = "";

SetWindowTitle();

}

void CMainFrame::OnFileSaveas()

{

char szFilters[]= "Text Files (*.txt)|*.txt|All Files (*.*)|*.*||";

CFileDialog fileDlg (FALSE, "txt", "*.txt", OFN_OVERWRITEPROMPT| OFN_HIDEREADONLY, szFilters, this);

if( fileDlg.DoModal ()==IDOK )

{

m_strPathname = fileDlg.GetPathName();

SetWindowTitle();

WriteFile();

}

}

void CMainFrame::SetWindowTitle()

{

CString Title;

if (m_strPathname == "") Title = "TextEdit - Untitled";

else Title = "TextEdit - " + m_strPathname;

SetWindowText(Title);

}

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]