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

📄 visualkb.cpp

📁 是一个用C++编写的图形编辑器!
💻 CPP
📖 第 1 页 / 共 2 页
字号:
			m_nCurrentRow++;
			point.y=(m_nCurrentRow - 1) * m_cyChar + m_cyChar / 9;
			GetPositionFromPoint(point);//从点计算出当前行、列
			point=GetPointFromPosition();//再根据行、列修正点
			SetCaretPos (point);
		}
		break;
	case VK_UP:
		if(m_nCurrentRow > 1){
			CPoint point=GetPointFromPosition();
			m_nCurrentRow--;
			point.y=(m_nCurrentRow - 1) * m_cyChar + m_cyChar / 9;
			GetPositionFromPoint(point);
			point=GetPointFromPosition();
			SetCaretPos (point);
		}
		break;
    case VK_HOME:
   		m_nCurrentCol = 0;
		m_nCurrentRow = 1;
		SetCaretPos (GetPointFromPosition());
        break;

    case VK_END:
		if(m_nLineCount == 1){
			m_nCurrentCol = m_strInputText.Length ();
			m_nCurrentRow = m_nLineCount;
		}
		else{
			unsigned nBeginPos=0;
			nBeginPos = m_strInputText.Find(m_nLineCount-1,'\n');
			cstring sub=m_strInputText.SubString(nBeginPos,m_strInputText.Length()-nBeginPos);
			m_nCurrentRow = m_nLineCount;
			m_nCurrentCol=sub.Length();
		}
		SetCaretPos (GetPointFromPosition());
        break;
    }

}

void CMainWindow::OnKeyUp( UINT nChar, UINT nRepCnt, UINT nFlags )
{

}



void CMainWindow::OnChar (UINT nChar, UINT nRepCnt, UINT nFlags)
{
    CClientDC dc (this);
	dc.SelectObject(&font);

	//
	// Determine which character was just input from the keyboard.
	//
    switch (nChar) {

    case VK_ESCAPE:
		m_strInputText.Empty ();

		m_nCurrentCol = 0;
		m_nCurrentRow = 1;
		m_nLineCount =1;
        break;
    case VK_RETURN:
      	m_strInputText=m_strInputText+'\n';//可以改成加'\r'回车键,需修改
		m_nLineCount++;
		m_nCurrentRow++;
		m_nCurrentCol = 0;
	    break;
    case VK_BACK:
		if(m_nLineCount == 1){
			if (m_nCurrentCol > 0) {
				m_strInputText.Delete(m_nCurrentCol - 1);
				m_nCurrentCol--;
			}
		}
		else{//不止一行
			int nBeginPos = m_strInputText.Find(m_nCurrentRow - 1,'\n');  
			if(m_nCurrentCol == 1 ){
				m_strInputText.Delete(nBeginPos+m_nCurrentCol - 1);
				m_nCurrentRow--;
				m_nLineCount--;
				m_strInputText.Delete(nBeginPos - 1);
				m_nCurrentCol = nBeginPos - 1;
			}
			else if(m_nCurrentCol == 0){
			    m_nCurrentRow--;
				m_nLineCount--;
				m_strInputText.Delete(nBeginPos - 1);
				m_strInputText.Delete(nBeginPos - 2);
				m_nCurrentCol = nBeginPos - 2;
			}
			else{
				m_strInputText.Delete(nBeginPos+m_nCurrentCol - 1);
				m_nCurrentCol--;
			}
		}
		Adjust(m_strInputText);
        break;

    default:
        if ((nChar >= 0) && (nChar <= 31))
            return;
				
		unsigned nBeginPos=0;
	
		if(m_nLineCount >= 2)
			nBeginPos = m_strInputText.Find(m_nLineCount-1,'\n');
		cstring sub=m_strInputText.SubString(nBeginPos,m_strInputText.Length()-nBeginPos);
		CSize size = dc.GetTextExtent (LPCTSTR(sub),m_strInputText.Length()-nBeginPos);//调用该成员函数计算使用当前字体的文本的高度与宽度

        if ((m_ptTextOrigin.x + size.cx) > m_nLineLimit) {
			m_strInputText+='\n';
			m_strInputText+=nChar;
			m_nCurrentCol = 1;
			m_nLineCount++;
			m_nCurrentRow++;
		}
        else{
			m_nCurrentCol++;
			m_strInputText+=nChar;
		}
    }

	HideCaret ();
    dc.ExtTextOut( m_ptTextOrigin.x, m_ptTextOrigin.y, ETO_OPAQUE , m_rcTextBox, "",NULL );
	dc.SelectObject(&font);

	dc.DrawText( (char*)m_strInputText, &m_rcTextBox, DT_LEFT | DT_WORDBREAK );
    SetCaretPos (GetPointFromPosition());
    ShowCaret ();
}

void CMainWindow::OnFileExit()
{
	SendMessage( WM_CLOSE, 0, 0);
}


void CMainWindow::OnEditCut()
{
    if(m_nCurrentRow == 1)//在第一行
	{
		m_strDeleteText	=  m_strInputText.SubString(m_nPrevCol,m_nCurrentCol - m_nPrevCol);
		m_strInputText.Delete(m_nPrevCol,m_nCurrentCol - m_nPrevCol);//删除第m_nCurrentRow行的选中字符串
	}
	else
	{
		int nBeginPos = m_strInputText.Find(m_nCurrentRow - 1,'\n');
		m_strDeleteText	=  m_strInputText.SubString(nBeginPos + m_nPrevCol,m_nCurrentCol - m_nPrevCol);
		m_strInputText.Delete(nBeginPos + m_nPrevCol,m_nCurrentCol - m_nPrevCol);
	}
	m_nCurrentCol = m_nPrevCol;
	CClientDC dc (this);
	dc.SelectObject(&font);
    dc.ExtTextOut( m_ptTextOrigin.x, m_ptTextOrigin.y, ETO_OPAQUE , m_rcTextBox, "",NULL );
	Adjust(m_strInputText);
	dc.DrawText( (char*)m_strInputText, &m_rcTextBox, DT_LEFT | DT_WORDBREAK );
	SetCaretPos(m_ptCaretPos);
	ShowCaret();

}


void CMainWindow::OnEditPaste()
{
    if(m_nCurrentRow == 1)//在第一行
		m_strInputText.Insert(m_nCurrentCol,m_strDeleteText);//在m_nCurrentCol处加入字符串
	else
	{
		int nBeginPos = m_strInputText.Find(m_nCurrentRow - 1,'\n');
		m_strInputText.Insert(nBeginPos + m_nCurrentCol,m_strDeleteText);
	}
	m_nCurrentCol += m_strDeleteText.Length();
	CClientDC dc (this);
	dc.SelectObject(&font);
    HideCaret ();
    dc.ExtTextOut( m_ptTextOrigin.x, m_ptTextOrigin.y, ETO_OPAQUE , m_rcTextBox, "",NULL );
	Adjust(m_strInputText);
	dc.DrawText( (char*)m_strInputText, &m_rcTextBox, DT_LEFT | DT_WORDBREAK );
	SetCaretPos (GetPointFromPosition());
    ShowCaret ();
}

void CMainWindow::OnEditCopy()//把选中字符串拷贝到m_strDeleteText
{
    if(m_nCurrentRow == 1)//在第一行
		m_strDeleteText = m_strInputText.SubString(m_nPrevCol,m_nCurrentCol - m_nPrevCol);
	else
	{
		int nBeginPos = m_strInputText.Find(m_nCurrentRow - 1,'\n');
		m_strDeleteText = m_strInputText.SubString(nBeginPos + m_nPrevCol,m_nCurrentCol - m_nPrevCol);
	}
}

void CMainWindow::OnEditDelete()
{
     if(m_nCurrentRow == 1)//在第一行
		m_strInputText.Delete(m_nPrevCol,m_nCurrentCol - m_nPrevCol);//删除第m_nCurrentRow行的选中字符串
	else
	{
		int nBeginPos = m_strInputText.Find(m_nCurrentRow - 1,'\n');
		m_strInputText.Delete(nBeginPos + m_nPrevCol,m_nCurrentCol - m_nPrevCol);
	}
	m_nCurrentCol = m_nPrevCol;
	CClientDC dc (this);
	dc.SelectObject(&font);
    dc.ExtTextOut( m_ptTextOrigin.x, m_ptTextOrigin.y, ETO_OPAQUE , m_rcTextBox, "",NULL );
	Adjust(m_strInputText);
	dc.DrawText( (char*)m_strInputText, &m_rcTextBox, DT_LEFT | DT_WORDBREAK );
	SetCaretPos(m_ptCaretPos);
	ShowCaret();
}


void CMainWindow::OnUpdateEditPaste(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck (m_strDeleteText.Length() == 0);	//选中或取消菜单项
	pCmdUI->Enable(m_strDeleteText.Length() != 0);		//使菜单项有效或无效
}

void CMainWindow::OnUpdateEditDelete(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck (m_strDeleteText.Length() == 0);	//选中或取消菜单项
	pCmdUI->Enable(m_strDeleteText.Length() != 0);		//使菜单项有效或无效
}

void CMainWindow::OnUpdateEditCopy(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck (m_strDeleteText.Length() == 0);	//选中或取消菜单项
	pCmdUI->Enable(m_strDeleteText.Length() != 0);		//使菜单项有效或无效
}

void CMainWindow::OnUpdateEditCut(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck (m_strDeleteText.Length() == 0);	//选中或取消菜单项
	pCmdUI->Enable(m_strDeleteText.Length() != 0);		//使菜单项有效或无效
}




CPoint CMainWindow::GetPointFromPosition()
{
	CClientDC dc(this);
	dc.SelectObject(&font);

	CPoint point;
	point.x = m_rcTextBox.left;
	point.y=(m_nCurrentRow - 1) * m_cyChar + m_cyChar / 9;
	if(m_nCurrentCol > 0){
		int nBeginPos = 0;
		if(m_nCurrentRow >= 2)
				nBeginPos = m_strInputText.Find(m_nCurrentRow - 1,'\n');
		cstring string = m_strInputText.SubString(nBeginPos,m_nCurrentCol);
		point.x += (dc.GetTextExtent ((char*)string, string.Length ())).cx;
	}
	return point;
}

void CMainWindow::GetPositionFromPoint(CPoint point)
{	 
	static int i=0;
	i++;
	if(i==4)
		int j=0;
	CClientDC dc(this);
	dc.SelectObject(&font);

	if(point.x <= m_ptTextOrigin.x && point.y <= m_ptTextOrigin.y){
		m_nCurrentRow = 1;
		m_nCurrentCol = 0;  
		return;
	}
	else if(point.y > m_nLineCount * m_cyLine)
		m_nCurrentRow = m_nLineCount;
	else 
		m_nCurrentRow = point.y / m_cyLine + 1;
	
	int nBeginPos = 0 ,nBeginPos1 ,nLenPos;
	cstring string;
	if(m_nCurrentRow >=2 )
		nBeginPos=m_strInputText.Find(m_nCurrentRow - 1,'\n');
	nBeginPos1 =  m_strInputText.Find(m_nCurrentRow,'\n');
	if(m_nCurrentRow != m_nLineCount)
		string = m_strInputText.SubString(nBeginPos,nBeginPos1- nBeginPos - 1);
    else
		string = m_strInputText.SubString(nBeginPos,nBeginPos1- nBeginPos);
	
	nLenPos = dc.GetTextExtent((char*)string, string.Length()).cx;
	if (point.x > nLenPos)
		m_nCurrentCol = string.Length();
	else{
		int i = 0;
		int nPrevChar = m_ptTextOrigin.x;
		int nNextChar = m_ptTextOrigin.x;

		while (nNextChar < point.x) {
			i++;
			nPrevChar = nNextChar;  
			cstring str=m_strInputText.SubString(nBeginPos,i);
			nNextChar = m_ptTextOrigin.x + dc.GetTextExtent((char*)str, i).cx;
		}
		int n = (m_strInputText.SubString(nBeginPos,m_strInputText.Length()-nBeginPos)).Length();
		if (i > n)
			m_nCurrentCol = n;
		else
			m_nCurrentCol = i;
	}
}

void CMainWindow::Adjust(cstring &m_str)
{
	UINT write_index = 0,index = 0;
	cstring string,substring;
	CClientDC dc(this);
	dc.SelectObject(&font);

    while(m_str[index]){
		while(dc.GetTextExtent(substring,substring.Length()).cx < m_nLineLimit && m_str[index]){
			if(m_str[index]!='\n'){
				substring += m_str[index];
				string += m_str[index];
				index++;
			    write_index++;
			}
			else
				index++;
			
		}
		if (m_str[index])
			string += '\n';

		substring.Empty();
	}
	m_str = string;
}

⌨️ 快捷键说明

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