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

📄 minieditview.cpp

📁 c++从入门到精通
💻 CPP
字号:
// miniEditView.cpp : implementation of the CMiniEditView class
//

#include "stdafx.h"
#include "miniEdit.h"

#include "miniEditDoc.h"
#include "miniEditView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMiniEditView

IMPLEMENT_DYNCREATE(CMiniEditView, CView)

BEGIN_MESSAGE_MAP(CMiniEditView, CView)
	//{{AFX_MSG_MAP(CMiniEditView)
	ON_WM_LBUTTONDOWN()
	ON_WM_SETCURSOR()
	ON_WM_SETFOCUS()
	ON_WM_CHAR()
	ON_COMMAND(ID_FILE_NEW, OnFileNew)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMiniEditView construction/destruction

CMiniEditView::CMiniEditView()
{
	// TODO: add construction code here
	p.x=0;p.y=0;
	m_nTextPos=0;
	nPosy=0;
	m_strText.Empty();
}

CMiniEditView::~CMiniEditView()
{
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMiniEditView drawing

void CMiniEditView::OnDraw(CDC* pDC)
{
	CMiniEditDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	pDC->SelectStockObject(ANSI_FIXED_FONT);
	iLCount=pDoc->strList.GetCount();//Protected型成员变量iLcount记录文本的行数
	m_nCellHeight=pDC->GetTextExtent("j").cy+5;	//字符的高和宽
	m_nCellWidth=pDC->GetTextExtent("W").cx;

	if(!pDoc->strList.IsEmpty())
	{
		POSITION  postemp = pDoc->strList.GetHeadPosition();
		for(int j=0;j<iLCount;j++)
			if(!pDoc->strList.IsEmpty())
				pDC->TextOut(1,j*m_nCellHeight,pDoc->strList.GetNext(postemp));
	}
}

/////////////////////////////////////////////////////////////////////////////
// CMiniEditView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CMiniEditView message handlers

void CMiniEditView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CMiniEditDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	CClientDC dc(this);
    CRect rect;
	GetClientRect(&rect);
	m_nTextPos=0;
    p.x=(point.x/m_nCellWidth)*m_nCellWidth;
	p.y=(point.y/m_nCellHeight)*m_nCellHeight;
	//nPosy=p.y/m_nCellHeight;//nPosy记录当前行是第几行
	//找到第nPosy行在链表strList中的位置
	pos=pDoc->strList.FindIndex(nPosy);
	//把当前行的字符串元素赋值给CString类型变量m_strText
	if(!pDoc->strList.IsEmpty())
	{
		//nPosy记录当前行是第几行
		nPosy=p.y/m_nCellHeight;
		//找到第nPosy行在链表strList中的位置
		pos=pDoc->strList.FindIndex(nPosy);
		//把当前行的字符串元素赋值给CString类型变量m_strText
		m_strText=pDoc->strList.GetAt(pos);
		if(p.x>=m_strText.GetLength()*m_nCellWidth)
			p.x=m_strText.GetLength()*m_nCellWidth;	
		//定位光标,最后的空白区
		m_nTextPos=p.x/m_nCellWidth;
	}
	else
	{
		p.x=0;
		p.y=0;
	}
	SetCaretPos(p);
    ShowCaret();
	//CView::OnLButtonDown(nFlags, point);
}

BOOL CMiniEditView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{
	// TODO: Add your message handler code here and/or call default
	if(nHitTest==HTCLIENT)
	{
		::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_IBEAM));
		return TRUE;
	}
	return CView::OnSetCursor(pWnd, nHitTest, message);
}

void CMiniEditView::OnSetFocus(CWnd* pOldWnd) 
{
	CView::OnSetFocus(pOldWnd);
	
	// TODO: Add your message handler code here
	CPoint pp(p.x,p.y);
	SetCursor(AfxGetApp()->LoadStandardCursor(IDC_IBEAM));
	CreateSolidCaret(2,15);
	SetCaretPos(pp);
	ShowCaret();
}

void CMiniEditView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	CMiniEditDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	CClientDC dc(this);
	POSITION posCursor=pos;//temp;
	CRect rect;
	//键盘处理语句
	switch(nChar)
	{

	case VK_ESCAPE:
	case VK_RETURN:
		//设置当前行的元素
		pDoc->strList.SetAt(pos,m_strText.Left(m_nTextPos));
		//取当前光标右边的元素并插入到strList当前位置的后面
		m_strText=m_strText.Right(m_strText.GetLength()-m_nTextPos);
		pos=pDoc->strList.InsertAfter(pos,m_strText);
		//文本行总数加1,光标所指在行加1,
		m_nTextPos=0;
		iLCount++;
	  	nPosy++;
		HideCaret();
		PositionCaret(&dc);
		ShowCaret();
		GetClientRect(&rect);
		InvalidateRect(rect);
		break;
	case VK_BACK:
		if((nPosy>=0)&&(m_nTextPos!=0))
		{
			if(m_nTextPos>=0)
			{
				m_strText=m_strText.Left(m_nTextPos-1)+
						  m_strText.Right(m_strText.GetLength()-m_nTextPos);
				m_nTextPos--;
				pDoc->strList.SetAt(posCursor,m_strText);
				CRect rect(m_nTextPos*m_nCellWidth,p.y,(m_strText.GetLength()+2)
					   *m_nCellWidth,p.y+m_nCellHeight);
				InvalidateRect(rect);
			}	
			
		}
		else if(nPosy>0)  
		{	
			nPosy--;
			pos=pDoc->strList.FindIndex(nPosy);
			m_strText=pDoc->strList.GetAt(pos);
			m_nTextPos=m_strText.GetLength();
			PositionCaret(&dc);
		}
		break;
	default:
		if((nChar>=0)&&(nChar<=31))
			return;
		if(!pDoc->strList.IsEmpty())
		{
			if(m_nTextPos==m_strText.GetLength())
			{
				m_strText+=nChar;
				pDoc->strList.SetAt(posCursor,m_strText);
				m_nTextPos++;
			}
			else
			{
				m_strText.Insert(m_nTextPos++,nChar);
				pDoc->strList.SetAt(pos,m_strText);
			}
		}
		else
		{
			m_strText+=nChar;
			pDoc->strList.AddTail(m_strText);
			pos=pDoc->strList.GetHeadPosition();
			m_nTextPos++;
		}
		CRect rect((m_nTextPos-1)*m_nCellWidth,p.y,(m_strText.GetLength()+2)
		    	   *m_nCellWidth,p.y+m_nCellHeight);
		InvalidateRect(rect);
		break;
	}	
	HideCaret();
	PositionCaret(&dc);
	ShowCaret();
	CView::OnChar(nChar, nRepCnt, nFlags);
}

void CMiniEditView::PositionCaret(CDC *pDC)
{
	BOOL bRelease=FALSE;
	if(pDC==NULL)
	{	
		pDC=GetDC();
		bRelease=TRUE;
	}
	 p.y=nPosy*m_nCellHeight;
	 CPoint point=p;
	 point.x=m_nTextPos*m_nCellWidth;
     SetCaretPos(point);
	 if(bRelease)
		ReleaseDC(pDC);
}

void CMiniEditView::OnFileNew() 
{
	// TODO: Add your command handler code here
	CMiniEditDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	pDoc->FileNew();
	//刷新客户区
	CRect rectClient;
	GetClientRect(&rectClient);
	InvalidateRect(rectClient);
	//光标定位,置初值
	p.x=0;p.y=0;
	m_nTextPos=0;
	nPosy=0;
	m_strText.Empty();
	//定位光标在左上角
	SetCaretPos(p);
 	ShowCaret();
}

⌨️ 快捷键说明

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