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

📄 notexpadview.cpp

📁 数控仿真与网络控制系统(雏形)
💻 CPP
字号:
// NoteXpadView.cpp : implementation of the CNoteXpadView class
//

#include "stdafx.h"
#include "NoteXpad.h"
#include "NoteXpadDoc.h"
#include "NoteXpadView.h"
#include "MyFontDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern BOOL CHANGED;
extern CString CURRENT_FILE;
LOGFONT NEAR CNoteXpadView::m_lfDefFont;
LOGFONT NEAR CNoteXpadView::m_lfOldDefFont;

//Definitions for registry key names
static TCHAR BASED_CODE szSettings[] = _T("Settings");
static TCHAR BASED_CODE szTabStops[] = _T("TabStops");
static TCHAR BASED_CODE szFont[] = _T("Font");
static TCHAR BASED_CODE szPrintFont[] = _T("PrintFont");
static TCHAR BASED_CODE szHeight[] = _T("Height");
static TCHAR BASED_CODE szWeight[] = _T("Weight");
static TCHAR BASED_CODE szItalic[] = _T("Italic");
static TCHAR BASED_CODE szUnderline[] = _T("Underline");
static TCHAR BASED_CODE szPitchAndFamily[] = _T("PitchAndFamily");
static TCHAR BASED_CODE szCharSet[] = _T("CharSet");
static TCHAR BASED_CODE szFaceName[] = _T("FaceName");
static TCHAR BASED_CODE szSystem[] = _T("System");
static TCHAR BASED_CODE szWordWrap[] = _T("WordWrap");
static TCHAR BASED_CODE szWidth[] = _T("Width");
static TCHAR BASED_CODE szEscapement[] = _T("Escapement");
static TCHAR BASED_CODE szOrientation[] = _T("Orientation");
static TCHAR BASED_CODE szStrikeOut[] = _T("StrikeOut");
static TCHAR BASED_CODE szOutPrecision[] = _T("OutPrecision");
static TCHAR BASED_CODE szClipPrecision[] = _T("ClipPrecisionp");
static TCHAR BASED_CODE szQuality[] = _T("Quality");
static TCHAR BASED_CODE szFontColor[] = _T("Colour");

/////////////////////////////////////////////////////////////////////////////
// CNoteXpadView

IMPLEMENT_DYNCREATE(CNoteXpadView, CEditView)

BEGIN_MESSAGE_MAP(CNoteXpadView, CEditView)
	//{{AFX_MSG_MAP(CNoteXpadView)
	ON_CONTROL_REFLECT(EN_CHANGE, OnChange)
	ON_WM_DESTROY()
	ON_CONTROL_REFLECT(EN_MAXTEXT, OnMaxtext)
	ON_WM_CTLCOLOR_REFLECT()
	ON_COMMAND(ID_CHANGECOLOR, OnChangecolor)
	ON_WM_KEYDOWN()
	ON_WM_LBUTTONDOWN()
	ON_WM_CREATE()
	ON_WM_LBUTTONUP()
	
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CEditView::OnFilePrintPreview)
	ON_COMMAND(ID_FILE_PRINT, CEditView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CEditView::OnFilePrint)
	
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNoteXpadView construction/destruction

CNoteXpadView::CNoteXpadView()
{
	/*Set the text and background colors to default values incase
	the registry does not contain font data*/

	m_TextColor = RGB(0,0,255);
	m_BackColor = RGB(255,255,255);
	m_BkBrush.CreateSolidBrush(m_BackColor);
}

CNoteXpadView::~CNoteXpadView()
{
}

void CNoteXpadView::Initialize()
{
	/*This function sets the font of the view.*/

		CFrameWnd* pMainWnd =(CFrameWnd*)AfxGetMainWnd();
		CEdit* theView = (CEdit*)pMainWnd->GetActiveView();

		CClientDC dc(pMainWnd);

		CFont font;

		VERIFY(font.CreateFontIndirect(&m_lfDefFont));
		CFont* def_font = dc.SelectObject(&font);

		theView->SetFont(&font);			
}


/////////////////////////////////////////////////////////////////////////////
// CNoteXpadView printing


void CNoteXpadView::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
{
	// Default CEditView begin printing.
	CEditView::OnBeginPrinting(pDC, pInfo);
}

void CNoteXpadView::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo)
{

	CEditView::OnEndPrinting(pDC, pInfo);
}

/////////////////////////////////////////////////////////////////////////////
// CNoteXpadView diagnostics

#ifdef _DEBUG
void CNoteXpadView::AssertValid() const
{
	CEditView::AssertValid();
}

void CNoteXpadView::Dump(CDumpContext& dc) const
{
	CEditView::Dump(dc);
}

CNoteXpadDoc* CNoteXpadView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CNoteXpadDoc)));
	return (CNoteXpadDoc*)m_pDocument;
}
#endif //_DEBUG

void CNoteXpadView::OnChange() 
{
	/*The text in the view has changed, set global CHANGED boolean to TRUE
	  Also, call SetModifiedFlag so that the app will prompt to save if closed*/

	CHANGED = TRUE;	
	CDocument* pDoc = (CDocument*)GetDocument();
	pDoc->SetModifiedFlag(TRUE);

}


void CNoteXpadView::WriteProfileFont(LPCTSTR szSec, const LOGFONT* plf, LOGFONT* plfOld)
{
	/*This function writes the current font of the view to the registry.*/

		CWinApp* pApp = AfxGetApp();

		long clr = m_TextColor;
		pApp->WriteProfileInt(szSec, szHeight, plf->lfHeight);
		pApp->WriteProfileInt(szSec,szFontColor,clr);
		pApp->WriteProfileInt(szSec,szWidth,plf->lfWidth);
		pApp->WriteProfileInt(szSec,szEscapement,plf->lfEscapement );
		pApp->WriteProfileInt(szSec,szOrientation,plf->lfOrientation );
		pApp->WriteProfileInt(szSec,szStrikeOut,plf->lfStrikeOut );
		pApp->WriteProfileInt(szSec,szOutPrecision,plf->lfOutPrecision);
		pApp->WriteProfileInt(szSec,szClipPrecision,plf->lfClipPrecision);
		pApp->WriteProfileInt(szSec,szQuality,plf->lfQuality);
		pApp->WriteProfileInt(szSec, szWeight, plf->lfWeight);
		pApp->WriteProfileInt(szSec, szItalic, plf->lfItalic);
		pApp->WriteProfileInt(szSec, szUnderline, plf->lfUnderline);
		pApp->WriteProfileInt(szSec, szPitchAndFamily, plf->lfPitchAndFamily);
		pApp->WriteProfileInt(szSec, szCharSet, plf->lfCharSet);
		pApp->WriteProfileString(szSec, szFaceName, (LPCTSTR)plf->lfFaceName);

		*plfOld = *plf;
}



void CNoteXpadView::GetProfileFont(LPCTSTR szSec, LOGFONT* plf)
{
	/*This function reads values from the registry, and initializes a
	LOGFONT struct to the font saved in the registry.*/

		CWinApp* pApp = AfxGetApp();
		
		plf->lfWidth = pApp->GetProfileInt(szSec,szWidth,0);
		plf->lfEscapement = pApp->GetProfileInt(szSec,szEscapement,0);
		plf->lfOrientation = pApp->GetProfileInt(szSec,szOrientation,0);
		plf->lfStrikeOut = pApp->GetProfileInt(szSec,szStrikeOut,0);
		plf->lfOutPrecision = pApp->GetProfileInt(szSec,szOutPrecision,OUT_DEFAULT_PRECIS);
		plf->lfClipPrecision = pApp->GetProfileInt(szSec,szClipPrecision,CLIP_CHARACTER_PRECIS);
		plf->lfQuality = pApp->GetProfileInt(szSec,szQuality,DEFAULT_QUALITY);
		plf->lfHeight = pApp->GetProfileInt(szSec,szHeight,0);
		plf->lfWeight = pApp->GetProfileInt(szSec, szWeight, 0);
		plf->lfItalic = (BYTE)pApp->GetProfileInt(szSec, szItalic, 0);
		plf->lfUnderline = (BYTE)pApp->GetProfileInt(szSec, szUnderline, 0);
		plf->lfPitchAndFamily = (BYTE)pApp->GetProfileInt(szSec, szPitchAndFamily, 0);
		plf->lfCharSet = (BYTE)pApp->GetProfileInt(szSec, szCharSet, DEFAULT_CHARSET);

		CString strFont = pApp->GetProfileString(szSec, szFaceName, szSystem);
		lstrcpyn((TCHAR*)plf->lfFaceName, strFont, sizeof plf->lfFaceName);
		plf->lfFaceName[sizeof plf->lfFaceName-1] = 0;

		m_TextColor = pApp->GetProfileInt(szSec,szFontColor,RGB(0,0,0));
		
}

void CNoteXpadView::SaveViewFont()
{
	m_lfOldDefFont = m_lfDefFont;
	WriteProfileFont("Font",&m_lfDefFont,&m_lfOldDefFont);
	
}

void CNoteXpadView::OnDestroy() 
{
	CEditView::OnDestroy();
	
	SaveViewFont(); //Call the SaveViewFont to save font to registry
	
}

void CNoteXpadView::OnChangeFont()
{
	CFrameWnd* pMainWnd =(CFrameWnd*)AfxGetMainWnd();
	CEdit* theView = (CEdit*)pMainWnd->GetActiveView();
	CFont* pFont = theView->GetFont();
	LOGFONT lf;

	if (pFont != NULL)
	   {
		   pFont->GetObject(sizeof(LOGFONT), &lf);
	   }
	else
	   {
		   ::GetObject(GetStockObject(SYSTEM_FONT), sizeof(LOGFONT), &lf);
	   }

	MyFontDlg fDialog(&lf);
	
	if (fDialog.DoModal() == IDOK)
		{
			CClientDC dc(pMainWnd);

			CFont font;
			
			fDialog.GetCurrentFont(&m_lfDefFont);

			VERIFY(font.CreateFontIndirect(&m_lfDefFont));
	
			CFont* def_font = dc.SelectObject(&font);

			theView->SetFont(&font);
		
		}

}

void CNoteXpadView::LoadViewFont()
{

	GetProfileFont("Font",&m_lfDefFont);
	Initialize();
	
}	



void CNoteXpadView::OnMaxtext() 
{
	MessageBox("You have reached the maximum capacity of this View.","Error: Max Length Reached",MB_OK|MB_ICONSTOP);	
}

HBRUSH CNoteXpadView::CtlColor(CDC* pDC, UINT nCtlColor) 
{	

	pDC->SetTextColor(m_TextColor);
	pDC->SetBkColor(m_BackColor);

	// TODO: Return a non-NULL brush if the parent's handler should not be called
	return m_BkBrush;
}

void CNoteXpadView::OnChangecolor() 
{
	CColorDialog cDialog(m_TextColor);

	//cDialog.m_cc.rgbResult = m_TextColor;

	cDialog.DoModal();

	m_TextColor = cDialog.m_cc.rgbResult;

	CNoteXpadView::Invalidate();
	
}

void CNoteXpadView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{	
	CEditView::OnKeyDown(nChar, nRepCnt, nFlags);
	CFrameWnd* theFrame = (CFrameWnd*)AfxGetMainWnd();
	theFrame->SendMessage(WM_TIMER,2,0);	
	
}
void CNoteXpadView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	CFrameWnd* theFrame = (CFrameWnd*)AfxGetMainWnd();
	theFrame->SendMessage(WM_TIMER,2,0);	
	CEditView::OnLButtonDown(nFlags, point);
}

void CNoteXpadView::OnInitialUpdate() 
{	
	
	CEditView::OnInitialUpdate();
	
}

BOOL CNoteXpadView::PreCreateWindow(CREATESTRUCT& cs) 
{
	BOOL preCreate = CEditView::PreCreateWindow(cs);
	cs.style &= ~(ES_AUTOHSCROLL|WS_HSCROLL);
	
	return preCreate;
}


CString CNoteXpadView::GetSelectedTxt()
{
	int start, end;
	CString buffer,left,right; //Temporary strings
	char newLine = 0x0a; //Define newline character
	CFrameWnd* mainWnd = (CFrameWnd*)AfxGetMainWnd();
	CEdit* theView = (CEdit*)mainWnd->GetActiveView(); //CEdit pointer to view

	theView->GetSel(start,end); //Set start & end to beginning and end of selected text
	theView->GetWindowText(buffer); //Set buffer to entire window text
	
	buffer = buffer.Mid(start,(end-start)); //Set buffer to the selected text

	int firstNewline = buffer.Find(newLine); //Find first newline character
	int lastNewline = buffer.ReverseFind(newLine); //Find last one too

	if (firstNewline != 0) //There are newline characters in selected text
	{
		left = buffer.Left(firstNewline-1); //Grab left of first newline
		right = buffer.Right(buffer.GetLength() -lastNewline-1); //Grab right of last one
		buffer = left+right; //Set buffer to new string w/o newlines
	
		return buffer;
	}

	return buffer; //No newline chars found, just return selected text

}

⌨️ 快捷键说明

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