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

📄 finder.cpp

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

#include "stdafx.h"
#include "Wediter.h"
#include "Finder.h"


//#include "picturevariable.h"

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

/////////////////////////////////////////////////////////////////////////////
// CFinder dialog

//extern CWediterView* pview;

CFinder::CFinder(CWnd* pParent /*=NULL*/)
	: CDialog(CFinder::IDD, pParent)
{
	//{{AFX_DATA_INIT(CFinder)
	m_findtext = _T("");
	//}}AFX_DATA_INIT
}


void CFinder::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFinder)
	DDX_Text(pDX, IDC_FINDTEXT, m_findtext);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CFinder, CDialog)
	//{{AFX_MSG_MAP(CFinder)
	ON_WM_DESTROY()
	ON_EN_CHANGE(IDC_FINDTEXT, OnChangeFindtext)
	ON_BN_CLICKED(IDC_CHECK_UPER, OnCheckUper)
	ON_BN_CLICKED(IDC_RADIO_DOWN, OnRadioDown)
	ON_BN_CLICKED(IDC_RADIO_UP, OnRadioUp)
	ON_WM_SHOWWINDOW()
	ON_WM_KILLFOCUS()
	ON_WM_SETCURSOR()
	ON_EN_CHANGE(IDC_REPLACETEXT, OnChangeReplacetext)
	ON_BN_CLICKED(IDC_REPLACE, OnReplace)
	ON_BN_CLICKED(IDC_REPLACEALL, OnReplaceall)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFinder message handlers

void CFinder::OnOK() 
{
	// TODO: Add extra validation here
	if(FindNext())
	{
		if(!GetDlgItem(IDC_REPLACETEXT)->IsWindowVisible())
		{
			setdalg(TRUE);
		}
		else
		{
			GetDlgItem(IDC_REPLACE)->EnableWindow(pview->fd.bfind);
			GetDlgItem(IDC_REPLACEALL)->EnableWindow(pview->fd.bfind);
		}
		SetDlgItemText(IDOK,"查找下一个(&F)");
	}
	else
	{
		SetDlgItemText(IDOK,"查找(&F)");
		pview->fd.icount=0;
	}

	//CDialog::OnOK();
}


void CFinder::OnCancel() 
{
	// TODO: Add extra cleanup here

	CDialog::OnCancel();
	CDialog::DestroyWindow();
}

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

	// TODO: Add your message handler code here
	SetFindBox();

	delete this;
}

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

	// TODO: Add extra initialization here
	m_down=pview->fd.inext;
	m_uper=pview->fd.icase;
	m_ltop=0;
	m_count=0;
	CenterWindow();
	CButton* pButton;
	if (m_down==1)
	{
		pButton=(CButton*)
			this->GetDescendantWindow(IDC_RADIO_DOWN);
		pButton->SetCheck(1);
	}
	else
	{
		pButton=(CButton*)
			this->GetDescendantWindow(IDC_RADIO_UP);
		pButton->SetCheck(1);
	}
	pButton=(CButton*)
			this->GetDescendantWindow(IDC_CHECK_UPER);
	pButton->SetCheck(m_uper);
	GetDlgItem(IDOK)->EnableWindow(!m_findtext.IsEmpty());
	pButton=NULL;
	delete pButton;


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



void CFinder::OnChangeFindtext() 
{
	// 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
	GetDlgItem(IDC_FINDTEXT)->GetWindowText(m_findtext);
	GetDlgItem(IDOK)->EnableWindow(!m_findtext.IsEmpty());
	pview->fd.icount=0;
	m_count=0;
	pview->fd.bfind=!m_findtext.IsEmpty();
}

void CFinder::OnCheckUper() 
{
	// TODO: Add your control notification handler code here
	CButton* pButton=(CButton*)
		this->GetDescendantWindow(IDC_CHECK_UPER);
	m_uper=pButton->GetCheck();
	GetDlgItem(IDOK)->EnableWindow(!m_findtext.IsEmpty());
	pview->fd.icount=0;
	m_count=0;
	pview->fd.bfind=!m_findtext.IsEmpty();
	pButton=NULL;
	delete pButton;
}

void CFinder::OnRadioDown() 
{
	// TODO: Add your control notification handler code here
	if (m_down==0) 
	{
		GetDlgItem(IDOK)->EnableWindow(!m_findtext.IsEmpty());
		pview->fd.icount=0;
		m_count=0;
		pview->fd.bfind=!m_findtext.IsEmpty();
	}
	m_down=1;
}

void CFinder::OnRadioUp() 
{
	// TODO: Add your control notification handler code here
	if (m_down==1) 
	{
		GetDlgItem(IDOK)->EnableWindow(!m_findtext.IsEmpty());
		pview->fd.icount=0;
		m_count=0;
		pview->fd.bfind=!m_findtext.IsEmpty();
	}
	m_down=0;
}

BOOL CFinder::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{
 	// TODO: Add your message handler code here and/or call default
 
	GetDlgItem(IDOK)->EnableWindow(!m_findtext.IsEmpty() && pview->fd.bfind);

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

void CFinder::OnShowWindow(BOOL bShow, UINT nStatus) 
{
	CDialog::OnShowWindow(bShow, nStatus);

	// TODO: Add your message handler code here

	if(m_findtext.IsEmpty())
		GetDlgItem(IDC_FINDTEXT)->SetFocus();
	pview->fd.bfind=!m_findtext.IsEmpty();

}

void CFinder::OnKillFocus(CWnd* pNewWnd) 
{
	CDialog::OnKillFocus(pNewWnd);
	
	// TODO: Add your message handler code here
	SetFindBox();
}



void CFinder::OnChangeReplacetext() 
{
	// 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

	GetDlgItem(IDC_REPLACETEXT)->GetWindowText(m_replacetext);
}

void CFinder::setdalg(bool breplace)
{
	CRect rect;
	if(breplace)
	{
		if(m_ltop>0)
		{
			GetDlgItem(IDC_REPLACE)->ShowWindow(SW_SHOW);
			GetDlgItem(IDC_REPLACE)->ShowWindow(SW_SHOW);
			GetDlgItem(IDC_REPLACESTATIC)->ShowWindow(SW_SHOW);
			GetDlgItem(IDC_REPLACETEXT)->ShowWindow(SW_SHOW);
			GetDlgItem(IDC_REPLACEALL)->ShowWindow(SW_SHOW);

			GetDlgItem(IDCANCEL)->GetWindowRect(rect);
			ScreenToClient(rect);
			GetDlgItem(IDCANCEL)->MoveWindow(rect.TopLeft().x,rect.TopLeft().y+m_ltop,rect.Width(),rect.Height());
			GetDlgItem(IDC_UPDOWNSTATIC)->GetWindowRect(rect);
			ScreenToClient(rect);
			GetDlgItem(IDC_UPDOWNSTATIC)->MoveWindow(rect.TopLeft().x,rect.TopLeft().y+m_ltop,rect.Width(),rect.Height());
			GetDlgItem(IDC_CHECK_UPER)->GetWindowRect(rect);
			ScreenToClient(rect);
			GetDlgItem(IDC_CHECK_UPER)->MoveWindow(rect.TopLeft().x,rect.TopLeft().y+m_ltop,rect.Width(),rect.Height());
			GetDlgItem(IDC_RADIO_UP)->GetWindowRect(rect);
			ScreenToClient(rect);
			GetDlgItem(IDC_RADIO_UP)->MoveWindow(rect.TopLeft().x,rect.TopLeft().y+m_ltop,rect.Width(),rect.Height());
			GetDlgItem(IDC_RADIO_DOWN)->GetWindowRect(rect);
			ScreenToClient(rect);
			GetDlgItem(IDC_RADIO_DOWN)->MoveWindow(rect.TopLeft().x,rect.TopLeft().y+m_ltop,rect.Width(),rect.Height());
			GetWindowRect(rect);
			ScreenToClient(rect);
			rect.bottom=rect.bottom+m_ltop;
			ClientToScreen(rect);
			MoveWindow(rect);
			m_ltop=0;
		}
		else
			SetWindowText("替换");
		GetDlgItem(IDC_REPLACE)->EnableWindow(pview->fd.bfind);
		GetDlgItem(IDC_REPLACEALL)->EnableWindow(pview->fd.bfind);
	}
	else
	{
		GetDlgItem(IDC_REPLACE)->ShowWindow(SW_HIDE);
		GetDlgItem(IDC_REPLACESTATIC)->ShowWindow(SW_HIDE);
		GetDlgItem(IDC_REPLACETEXT)->ShowWindow(SW_HIDE);
		GetDlgItem(IDC_REPLACEALL)->ShowWindow(SW_HIDE);
		
		GetDlgItem(IDC_REPLACETEXT)->GetWindowRect(rect);
		ScreenToClient(rect);
		m_ltop=rect.top;
		GetDlgItem(IDC_UPDOWNSTATIC)->GetWindowRect(rect);
		ScreenToClient(rect);
		m_ltop=rect.top-m_ltop;
		GetDlgItem(IDC_UPDOWNSTATIC)->MoveWindow(rect.TopLeft().x,rect.TopLeft().y-m_ltop,rect.Width(),rect.Height());
		GetDlgItem(IDCANCEL)->GetWindowRect(rect);
		ScreenToClient(rect);
		GetDlgItem(IDCANCEL)->MoveWindow(rect.TopLeft().x,rect.TopLeft().y-m_ltop,rect.Width(),rect.Height());
		GetDlgItem(IDC_CHECK_UPER)->GetWindowRect(rect);
		ScreenToClient(rect);
		GetDlgItem(IDC_CHECK_UPER)->MoveWindow(rect.TopLeft().x,rect.TopLeft().y-m_ltop,rect.Width(),rect.Height());
		GetDlgItem(IDC_RADIO_UP)->GetWindowRect(rect);
		ScreenToClient(rect);
		GetDlgItem(IDC_RADIO_UP)->MoveWindow(rect.TopLeft().x,rect.TopLeft().y-m_ltop,rect.Width(),rect.Height());
		GetDlgItem(IDC_RADIO_DOWN)->GetWindowRect(rect);
		ScreenToClient(rect);
		GetDlgItem(IDC_RADIO_DOWN)->MoveWindow(rect.TopLeft().x,rect.TopLeft().y-m_ltop,rect.Width(),rect.Height());
		GetWindowRect(rect);
		ScreenToClient(rect);
		rect.bottom=rect.bottom-m_ltop;
		ClientToScreen(rect);
		MoveWindow(rect);
		if(pview->fd.bfind)
			SetDlgItemText(IDOK,"查找下一个(&F)");
		else
			SetDlgItemText(IDOK,"查找(&F)");
	}
}

bool CFinder::FindNext()
{
	if(pview)
	{
		if(pview->FindText(m_findtext,m_down==1,m_uper==1))
		{
			pview->SetFinder(m_findtext,m_down==1,m_uper==1,pview->fd.icount+1,TRUE);
			ShowmessgeBar("找到 \""+m_findtext+"\"");
		}
		else
		{
			GetDlgItem(IDOK)->EnableWindow(FALSE);
			if(pview->fd.icount==0)
				ShowmessgeBar("没有找到!");
			else
			{
				CString mstr;
				mstr.Format("%d",pview->fd.icount);
				ShowmessgeBar("找到\""+mstr+"\"处");
			}
			pview->SetFinder(m_findtext,m_down==1,m_uper==1,pview->fd.icount,FALSE);

		}
		return pview->fd.bfind;
	}
	else
		return FALSE;
}

void CFinder::OnReplace() 
{
	// TODO: Add your control notification handler code here
	if(pview->fd.bfind)
	{
		pview->GetEditCtrl().ReplaceSel(m_replacetext,TRUE);
		m_count=m_count+1;
	}
	if(!FindNext())
	{
		GetDlgItem(IDC_REPLACE)->EnableWindow(FALSE);
		GetDlgItem(IDC_REPLACEALL)->EnableWindow(FALSE);
		if(m_count!=0)
		{
			CString mstr1,mstr2;
			mstr1.Format("%d",m_count);
			mstr2.Format("%d",pview->fd.icount);
			m_count=0;
			pview->fd.icount=0;
			ShowmessgeBar("找到\""+mstr2+"\"处,"+"已替换\""+mstr1+"\"处");
		}
	}
	else
		ShowmessgeBar("\""+m_findtext+"\""+"替换成\""+m_replacetext+"\"");
}

void CFinder::OnReplaceall() 
{
	// TODO: Add your control notification handler code here
LableReplaceall:
	if(pview->fd.bfind)
	{
		OnReplace();
		goto LableReplaceall;
	}
}

void CFinder::SetFindBox()
{
	if(!m_findtext.IsEmpty())
	{
		CString mstr;
		CFindComboBx* P_Cbox=(CFindComboBx*)
			AfxGetApp()->m_pMainWnd->GetDescendantWindow(IDR_FIND_COMBOBOX);
		if (P_Cbox)
		{
			if(P_Cbox->SelectString(0,m_findtext)==CB_ERR)
			{
				P_Cbox->InsertString(0,m_findtext);
				P_Cbox->SetCurSel(0);
			}
		}
		P_Cbox=NULL;
		delete P_Cbox;
	}
}

⌨️ 快捷键说明

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