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

📄 insword.cpp

📁 微型文本编辑器. 多文档界面
💻 CPP
字号:
// InsWord.cpp : implementation file
//

#include "stdafx.h"
#include "Wediter.h"
#include "InsWord.h"

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

/////////////////////////////////////////////////////////////////////////////
// CInsWord dialog


CInsWord::CInsWord(CWnd* pParent /*=NULL*/)
	: CDialog(CInsWord::IDD, pParent)
{
	//{{AFX_DATA_INIT(CInsWord)
	m_words = _T("");
	m_being= _T("{}");
	m_end= _T("}{");
	//}}AFX_DATA_INIT
}


void CInsWord::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CInsWord)
	DDX_Control(pDX, IDC_TREE, m_tree);
	DDX_Text(pDX, IDC_WORDEDIT, m_words);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CInsWord, CDialog)
	//{{AFX_MSG_MAP(CInsWord)
	ON_WM_SETCURSOR()
	ON_WM_SHOWWINDOW()
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_SAVE, OnSave)
	ON_EN_CHANGE(IDC_WORDEDIT, OnChangeWordedit)
	ON_BN_CLICKED(IDC_READONLYCHECK, OnReadonlycheck)
	ON_NOTIFY(TVN_SELCHANGED, IDC_TREE, OnSelchangedTree)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CInsWord message handlers

BOOL CInsWord::OnInitDialog() 
{
	CDialog::OnInitDialog();

	//为编辑框控件IDC_WORDEDIT设置视图指针及编辑ID

	m_EditBox.SubclassDlgItem(IDC_WORDEDIT,this);

	CenterWindow();
	m_editchange=FALSE;
	CButton* pButton=(CButton*)
		GetDescendantWindow(IDC_READONLYCHECK);
	pButton->SetCheck(m_EditBox.m_readonly);
	pButton=NULL;
	delete pButton;

	
	m_EditBox.m_being=m_being;

	m_imglst.Create(16,16,ILC_MASK,2,2);
	m_imglst.Add(AfxGetApp()->LoadIcon(IDI_ICON_ROT));
	m_imglst.Add(AfxGetApp()->LoadIcon(IDI_ICON_CHID));

	m_tree.SetImageList(&m_imglst,TVSIL_NORMAL);

	CreatTree();

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}



void CInsWord::OnOK() 
{
	// TODO: Add extra validation here
	int nFrom, nTo,nidx,nlh,nrow,ncount;
	m_EditBox.GetSel(nFrom,nTo);
	if (nTo!=nFrom)
	{
		UpdateData();
		CString mstr;
		mstr=m_words.Mid(nFrom,nTo-nFrom);
		nidx=pview->GetEditCtrl().LineIndex();
		nlh=pview->GetEditCtrl().LineLength();
		nrow=pview->GetEditCtrl().LineFromChar();
		ncount=pview->GetEditCtrl().GetLineCount();
		pview->GetEditCtrl().GetSel(nFrom, nTo);
		if(nFrom==nTo && nFrom==nidx+nlh && nlh>0)
			pview->GetEditCtrl().ReplaceSel("\r\n"+mstr,TRUE);
		else if(nFrom==nTo && nFrom==nidx && nrow<=ncount-1 && nlh>0)
			pview->GetEditCtrl().ReplaceSel(mstr+"\r\n",TRUE);
		else
			pview->GetEditCtrl().ReplaceSel(mstr,TRUE);
		m_EditBox.SetFocus();
	}
	
	//CDialog::OnOK();
}

BOOL CInsWord::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{
	// TODO: Add your message handler code here and/or call default
	int nFrom, nTo;
	m_EditBox.GetSel(nFrom,nTo);
	GetDlgItem(IDOK)->EnableWindow(nFrom!=nTo);
	GetDlgItem(IDC_SAVE)->EnableWindow(m_editchange && m_EditBox.m_readonly==0);

	return CDialog::OnSetCursor(pWnd, nHitTest, message);
}

void CInsWord::OnShowWindow(BOOL bShow, UINT nStatus) 
{
	CDialog::OnShowWindow(bShow, nStatus);
	
	// TODO: Add your message handler code here
	if (m_EditBox.LineIndex(2)-2>0) m_EditBox.SetSel(m_EditBox.LineIndex(1),m_EditBox.LineIndex(2)-2);
	m_EditBox.SetFocus();
}

void CInsWord::OnCancel() 
{
	// TODO: Add extra cleanup here
	
	CDialog::OnCancel();
	CDialog::DestroyWindow();
}



void CInsWord::OnDestroy() 
{
	CDialog::OnDestroy();

	delete this;
	// TODO: Add your message handler code here
	
}

void CInsWord::OnSave() 
{
	// TODO: Add your control notification handler code here
	CString mstr,fm;
	CStdioFile file;
	int ncount,n,nFrom, nTo,nsta,ncot;
	fm=gapp.wordname;
	if (file.Open(fm,CFile::modeCreate|CFile::modeWrite|CFile::typeText))
	{
		UpdateData();
		m_EditBox.GetSel(nFrom,nTo);
		ncount=m_EditBox.GetLineCount();
		for(n=0;n<ncount;n++)
		{
			nsta=m_EditBox.LineIndex(n);
			m_EditBox.SetSel(nsta,nsta);
			ncot=m_EditBox.LineLength();
			mstr=m_words.Mid(nsta,ncot);

			file.WriteString(mstr+"\n");
		}
		file.Close();
		m_EditBox.SetSel(nFrom,nTo);
		m_editchange=FALSE;
		m_tree.DeleteAllItems();
		CreatTree();
	}
	else
		MessageBox("文件<"+fm+">不存在,或已坏!",NULL,MB_ICONERROR);
	m_EditBox.SetFocus();
}

void CInsWord::OnChangeWordedit() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	m_editchange=TRUE;
}

void CInsWord::OnReadonlycheck() 
{
	// TODO: Add your control notification handler code here
	CButton* pButton=(CButton*)
		this->GetDescendantWindow(IDC_READONLYCHECK);
	m_EditBox.m_readonly=pButton->GetCheck();

	pButton=NULL;
	delete pButton;
}

void CInsWord::OnSelchangedTree(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
	// TODO: Add your control notification handler code here
	CString seltxxt;
	HTREEITEM Item;
	Item=m_tree.GetSelectedItem();
	if(m_tree.GetParentItem(Item))
	{
		seltxxt=m_tree.GetItemText(Item);
		if(!seltxxt.IsEmpty())
		{
			seltxxt=m_being+seltxxt;
			int nFrom, nTo;
			UpdateData();
			nFrom=m_words.Find(seltxxt,0);
			nTo=seltxxt.GetLength();
			if(nFrom!=-1)
			{
				int nrow,nfrow;
				m_EditBox.SetSel(nFrom,nFrom+nTo);
				nrow=m_EditBox.LineFromChar();
				nfrow=m_EditBox.GetFirstVisibleLine();
				m_EditBox.LineScroll(nrow-nfrow);
			}
		}
	}
	*pResult = 0;

}



void CInsWord::CreatTree()
{
	int nidx,ncount,n,nFrom, nTo,nsta,ncot;
	CString mstr;
	mstr=gapp.wordname;
	if(mstr.Find("\\")!=-1)
	{
		mstr.MakeReverse();
		mstr=mstr.Left(mstr.Find("\\"));
		mstr.MakeReverse();
	}
	if(mstr.Find(".")!=-1)
		mstr=mstr.Left(mstr.Find("."));

	
	//初始化树控件
	TV_INSERTSTRUCT treeItem;
	HTREEITEM Item;
	treeItem.hInsertAfter=TVI_LAST;
	treeItem.item.mask=TVIF_TEXT|TVIF_PARAM;

	//根目录
	if(nidx=m_tree.GetCount()-1<0)
		nidx=0;
	treeItem.hParent=TVI_ROOT;
	treeItem.item.pszText=mstr.GetBuffer(mstr.GetLength());
	treeItem.item.lParam=nidx;
	Item=m_tree.InsertItem(&treeItem);
	m_tree.SetItemImage(Item,0,0);

	//枝目录
	m_EditBox.GetSel(nFrom,nTo);
	ncount=m_EditBox.GetLineCount();
	HTREEITEM cItem;
	treeItem.hParent=Item;
	for(n=0;n<ncount;n++)
	{
		UpdateData();
		nsta=m_EditBox.LineIndex(n);
		m_EditBox.SetSel(nsta,nsta);
		ncot=m_EditBox.LineLength();
		mstr=m_words.Mid(nsta,ncot);
		if(mstr.Find(m_being,0)==0)
		{
			mstr.Replace(m_being,"");
			if(!mstr.IsEmpty())
			{
				nidx=nidx+1;
				treeItem.item.lParam=nidx;
				//strcpy(treeItem.item.pszText,mstr);
				treeItem.item.pszText=mstr.GetBuffer(mstr.GetLength());
				cItem=m_tree.InsertItem(&treeItem);
				m_tree.SetItemImage(cItem,1,1);
			}
		}
	}
}

⌨️ 快捷键说明

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