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

📄 1view.cpp

📁 类似于记事本的行文本编辑器 可以存储为txt支持导入导出
💻 CPP
字号:
// 1View.cpp : implementation of the CMy1View class
//

#include "stdafx.h"
#include "1.h"

#include "1Doc.h"
#include "1View.h"


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

/////////////////////////////////////////////////////////////////////////////
// CMy1View

IMPLEMENT_DYNCREATE(CMy1View, CView)

BEGIN_MESSAGE_MAP(CMy1View, CView)
	//{{AFX_MSG_MAP(CMy1View)
	ON_WM_CREATE()
	ON_WM_CHAR()
	ON_WM_LBUTTONDOWN()
	ON_COMMAND(IDM_FONT, OnFont)
	ON_COMMAND(ID_FILE_SAVE, OnFileSave)
	ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMy1View construction/destruction

CMy1View::CMy1View()
{
	// TODO: add construction code here
	m_write="";
	m_origin=0;
	pvs=0;
	m_fontname="";
//	memset(all,0,100);
	
//	all[1]="";
}

CMy1View::~CMy1View()
{
}

BOOL CMy1View::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMy1View drawing

void CMy1View::OnDraw(CDC* pDC)
{
	CMy1Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	CString string1;
	
	string1.LoadString(IDS_LIANXI); //加载字符串资源 很多。
	pDC->TextOut(420,420,string1);
	pDC->BeginPath();//     路径层用法 
	CSize sz=pDC->GetTextExtent(string1);
	pDC->Rectangle(420,420,420+sz.cx,420+sz.cy);
	pDC->EndPath();

	//路径层必用函数吧
	pDC->SelectClipPath(RGN_DIFF); //对当前剪切区域与路径层做你要的运算 and or xor diff 等

	//测试路径层作用
	for (int j=400;j<500;j+=10)
	{
		pDC->MoveTo(400,j);
		pDC->LineTo(500,j);
		pDC->MoveTo(j,400);
		pDC->LineTo(j,500);
	}





	pDC->SelectObject(&m_font);
//	pDC->TextOut(100,100,"wewewew/newe");
	pDC->GetTextMetrics(&tm); //用当前设备描述表中的字体填充tm
	
	CreateSolidCaret(tm.tmAveCharWidth/8,tm.tmHeight);//创建之后默认初始化隐藏了 要showcaret
	ShowCaret();
	CPoint xx;
	xx=0;
	SetCaretPos(xx);
	CRect R;
	GetClientRect(&R);  
	
	temp=int((R.bottom-R.top)/tm.tmHeight);
	for (int i=0;i<temp;i++)
	{
		
		pall[i].x=0;
		pall[i].y=i*tm.tmHeight;
		pDC->TextOut(pall[i].x,pall[i].y,all[i]);  //重绘时把以前的内容用新字体重新输出
	//	all[i]="";  
	}
	temp=0;
//	CString a;
//	a.Format("%d",temp);
//	MessageBox(a);
	

	







}

/////////////////////////////////////////////////////////////////////////////
// CMy1View printing

BOOL CMy1View::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CMy1View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CMy1View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CMy1View diagnostics

#ifdef _DEBUG
void CMy1View::AssertValid() const
{
	CView::AssertValid();
}

void CMy1View::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CMy1View message handlers

int CMy1View::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
/*	// TODO: Add your specialized creation code here
	CClientDC dc(this);
	//	TEXTMETRIC tm;
	dc.GetTextMetrics(&tm); //用当前设备描述表中的字体填充tm
	
	CreateSolidCaret(tm.tmAveCharWidth/8,tm.tmHeight);//创建之后默认初始化隐藏了 要showcaret
	
	//	CBitmap bitmap;//   局部的啊 这样光标不可能显示出来 析构了。
	//	bitmap.LoadBitmap(IDB_BITMAP1);
	//	CreateCaret(&bitmap);
	ShowCaret();
	CPoint xx;
	xx=0;
	SetCaretPos(xx);
	CRect R;
	::GetClientRect(this->m_hWnd,&R);  
	
	temp=int((R.right-R.top)/tm.tmHeight);
	for (int i=0;i<temp;i++)
	{
		all[i]="";
		pall[i].x=0;
		pall[i].y=i*tm.tmHeight;
	}
	temp=0;
	*/            //    代码移动到初始化那去了
	

	
	return 0;
}

void CMy1View::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	CClientDC dc(this);
	dc.SelectObject(m_font);
//	TEXTMETRIC tm;
	dc.GetTextMetrics(&tm);
	if (0x0d==nChar)
	{

		all[temp]=m_write;
		m_write="";
	//	pall[temp].y+=tm.tmHeight;
		temp+=1;
		m_write=all[temp];
	}
	else if (0x08==nChar)
	{
		COLORREF col=dc.SetTextColor(dc.GetBkColor());
		dc.TextOut(pall[temp].x,pall[temp].y,m_write);

		m_write=m_write.Left(m_write.GetLength()-1);
		dc.SetTextColor(col);
	}
	else
	{
		pvs=temp;
		m_write+=nChar;
// 		CSize sz=dc.GetTextExtent(m_write);
// 		//SetCaretPos(m_origin.x+sz.cx,m_origin.y);   错了 因为要用第三者 直接里面不能计算 
// 		CPoint pt;
// 		pt.x=m_origin.x+sz.cx;
// 		pt.y=m_origin.y;
// 		SetCaretPos(pt);// 开始写这里 真der
	}

//	if (!all[temp].IsEmpty())
//	{

	
		CSize sz=dc.GetTextExtent(m_write);

		CPoint pt;
		pt.x=pall[temp].x+sz.cx;
		pt.y=pall[temp].y;
			SetCaretPos(pt);
//	}
		
		dc.TextOut(pall[temp].x,pall[temp].y,m_write);
		//all[temp]=m_write;
		all[temp]=m_write;

		CView::OnChar(nChar, nRepCnt, nFlags);
}

void CMy1View::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
//	SetCaretPos(point);
	//每次要把原来的字符清空
//	m_write.Empty();
//	m_origin=point;
	CClientDC dc(this);dc.SelectObject(m_font);
	int height=int(point.y/tm.tmHeight);
	int reallyheight;
	reallyheight=height*tm.tmHeight;
	point.y=reallyheight;// 表示四舍五入的位置
	
	temp=height;
//	SetCaretPos(point);
	pall[temp].x=0;pall[temp].y=point.y;
//all[pvs]=m_write;
	if (!all[temp].IsEmpty())
	{
	
	//m_write=all[temp];
	CSize sz1=dc.GetTextExtent(all[temp]);
	
	CPoint pt;
	pt.x=pall[temp].x+sz1.cx;
	pt.y=pall[temp].y;
		SetCaretPos(pt);
	//SetCaretPos(pall[temp]);
	m_write=all[temp];
	 
//	m_origin=point;
	}
	else
		temp=pvs;

	CView::OnLButtonDown(nFlags, point);
}

void CMy1View::OnInitialUpdate() 
{
	CView::OnInitialUpdate();
	
	// TODO: Add your specialized code here and/or call the base class
	CClientDC dc(this);
	//	TEXTMETRIC tm;
	dc.GetTextMetrics(&tm); //用当前设备描述表中的字体填充tm
	
	CreateSolidCaret(tm.tmAveCharWidth/8,tm.tmHeight);//创建之后默认初始化隐藏了 要showcaret
	
	//	CBitmap bitmap;//   局部的啊 这样光标不可能显示出来 析构了。
	//	bitmap.LoadBitmap(IDB_BITMAP1);
	//	CreateCaret(&bitmap);
	ShowCaret();
	CPoint xx;
	xx=0;
	SetCaretPos(xx);
	CRect R;
	GetClientRect(&R);  
	
	temp=int((R.bottom-R.top)/tm.tmHeight);
	for (int i=0;i<temp;i++)
	{
		all[i]="";                        //这的代码也还有用 因为它在OnDraw前执行
		pall[i].x=0;                      //能起到初始化功能  删除的话 程序开始第一次调用ondraw
		pall[i].y=i*tm.tmHeight;        //  就textout 就出事了
	}
	temp=0;
	
	
}


void CMy1View::OnFont() 
{
	// TODO: Add your command handler code here
	CFontDialog dlg;
	if (IDOK==dlg.DoModal())
	{
		if(m_font.m_hObject)
			m_font.DeleteObject();
		m_font.CreateFontIndirect(dlg.m_cf.lpLogFont); //用户选择字体之后点击确定就执行了 用m_font的函数获取字体
		m_fontname=dlg.m_cf.lpLogFont->lfFaceName;// 在获得字体的名字

	}
	Invalidate();
}

void CMy1View::OnFileSave() 
{
	// TODO: Add your command handler code here
	CString strname;
	int count=0;
	char hf=13;
	char hh=10;
	CFileDialog dlg(false,"txt",NULL,OFN_OVERWRITEPROMPT,"Text Files(*.txt)|*.txt|",NULL);
	if (IDOK==dlg.DoModal())
	{
		CFile file(dlg.GetPathName(),CFile::modeCreate | CFile::modeWrite);
		for (int i=0;i<=30;i++)
		{
			if (!all[i].IsEmpty())
			{
				count++;
			}
		}
		for (i=0;i<count;i++)
		{
			file.Write(all[i],strlen(all[i]));
			file.Write(&hf,1);  //换行怎么解决////..这方法估计很垃圾啊
			file.Write(&hh,1);
		}
		
		file.Close();	
	}
	
}

void CMy1View::OnFileOpen() 
{
	// TODO: Add your command handler code here
	CString strname;
	int count=0;
	CFileDialog dlg(true,"txt",NULL,OFN_OVERWRITEPROMPT,"Text Files(*.txt)|*.txt|",NULL);
	if (IDOK==dlg.DoModal())
	{
		CFile file(dlg.GetPathName(),CFile::modeCreate|CFile::modeRead);
	//  不会读入数据...
	}
}

⌨️ 快捷键说明

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