www.pudn.com > 数控仿真与网络控制系统.rar > FindDialog.cpp


// FindDialog.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "NoteXpad.h" 
#include "FindDialog.h" 
#include "DopeEdit.h" 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
UINT ID_DOPE_EDIT = 1773; 
///////////////////////////////////////////////////////////////////////////// 
// CNoteXpadFindDialog dialog 
 
 
CNoteXpadFindDialog::CNoteXpadFindDialog(CWnd* pParent /*=NULL*/) 
	: CDialog(CNoteXpadFindDialog::IDD, pParent) 
{ 
	 
	//{{AFX_DATA_INIT(CNoteXpadFindDialog) 
	m_PreserveCase = FALSE; 
	//}}AFX_DATA_INIT 
} 
 
 
void CNoteXpadFindDialog::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CNoteXpadFindDialog) 
	DDX_Control(pDX, IDC_PRESERVECASE, m_chkPreserveCase); 
	DDX_Check(pDX, IDC_PRESERVECASE, m_PreserveCase); 
	//}}AFX_DATA_MAP 
} 
 
 
BEGIN_MESSAGE_MAP(CNoteXpadFindDialog, CDialog) 
	//{{AFX_MSG_MAP(CNoteXpadFindDialog) 
	ON_BN_CLICKED(IDC_BTNFIND, OnBtnfind) 
	ON_BN_CLICKED(IDC_FINDNEXT, OnFindnext) 
	ON_WM_CREATE() 
	ON_BN_CLICKED(IDC_PRESERVECASE, OnPreserveCase) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CNoteXpadFindDialog message handlers 
 
 
void CNoteXpadFindDialog::OnBtnfind()  
{ 
	 
		 
	CFrameWnd* pMainWnd =(CFrameWnd*)AfxGetMainWnd(); 
 
	CEdit* theView = (CEdit*)pMainWnd->GetActiveView(); 
 
	CString avishJohn; 
 
	theView->GetWindowText(avishJohn); 
	CString SearchQuery; 
	CDopeEdit* QueryBox = (CDopeEdit*)GetDlgItem(ID_DOPE_EDIT); 
 
	if (avishJohn == "") 
	{ 
		QueryBox->SetFocus(); 
		QueryBox->OnSelAll(); 
		return; 
	} 
 
	 
 
	QueryBox->GetWindowText(SearchQuery); 
 
	if (m_chkPreserveCase.GetCheck() ==0) 
	{ 
		avishJohn.MakeLower(); 
		SearchQuery.MakeLower(); 
	} 
 
	int start = avishJohn.Find(SearchQuery); 
 
	if (start == -1) 
	{ 
		AfxMessageBox("Search Query was not found!",MB_OK|MB_ICONEXCLAMATION); 
		QueryBox->SetFocus(); 
		QueryBox->OnSelAll(); 
		 
	} 
	theView->SetSel(start,(start + SearchQuery.GetLength())); 
	 
	CButton* findNext = (CButton*)GetDlgItem(IDC_FINDNEXT); 
	findNext->EnableWindow(TRUE); 
 
} 
 
void CNoteXpadFindDialog::OnCancel()  
{	 
	CDialog::OnCancel(); 
} 
 
void CNoteXpadFindDialog::OnFindnext()  
{ 
	CString RightOfCurrent; 
	CString SearchQuery; 
	CString ExistingText; 
 
	int StartOfSel; 
	int EndOfSel; 
	int NewStart; 
	int EndNewSel; 
	CString Left; 
	CEdit* QueryBox = (CEdit*)GetDlgItem(ID_DOPE_EDIT); 
	CFrameWnd* pMainWnd =(CFrameWnd*)AfxGetMainWnd(); 
	CEdit* theView = (CEdit*)pMainWnd->GetActiveView(); 
 
	QueryBox->GetWindowText(SearchQuery); 
	theView->GetWindowText(ExistingText); 
 
	if (m_chkPreserveCase.GetCheck() ==0) 
	{ 
		ExistingText.MakeLower(); 
		SearchQuery.MakeLower(); 
	} 
 
	theView->GetSel(StartOfSel,EndOfSel); 
 
 
	RightOfCurrent = ExistingText.Mid(EndOfSel); 
	Left = ExistingText.Left(EndOfSel); 
 
	NewStart = Left.GetLength() +  RightOfCurrent.Find(SearchQuery); 
 
	if (RightOfCurrent.Find(SearchQuery) == -1) 
	{ 
		AfxMessageBox("Finished searching through the file.",MB_OK); 
		return; 
	} 
		 
	EndNewSel = NewStart + SearchQuery.GetLength(); 
	theView->SetSel(NewStart,EndNewSel); 
 
 
} 
void CNoteXpadFindDialog::OnDopeEditChange() 
{ 
	CString txt; 
	CEdit* cEdit = (CEdit*)GetDlgItem(ID_DOPE_EDIT); 
	cEdit->GetWindowText(txt); 
	CButton* cBtn = (CButton*)GetDlgItem(IDC_BTNFIND); 
	CButton* bBtn = (CButton*)GetDlgItem(IDC_FINDNEXT); 
 
	if (txt == "") 
	{ 
		cBtn->EnableWindow(FALSE); 
		bBtn->EnableWindow(FALSE); 
	} 
	else 
	{ 
		cBtn->EnableWindow(TRUE); 
	} 
 
} 
int CNoteXpadFindDialog::OnCreate(LPCREATESTRUCT lpCreateStruct)  
{	 
	CRect rect(12,30,378,48); 
	m_SearchBox.Create(WS_CHILD|WS_VISIBLE|ES_AUTOHSCROLL|WS_TABSTOP,rect,this,ID_DOPE_EDIT); 
	if (CDialog::OnCreate(lpCreateStruct) == -1) 
		return -1; 
	 
	 
	return 0; 
} 
 
BOOL CNoteXpadFindDialog::OnInitDialog()  
{ 
	CDialog::OnInitDialog(); 
	 
	m_SearchBox.SubclassDlgItem(ID_DOPE_EDIT,this); 
	int pCase = AfxGetApp()->GetProfileInt("Settings","PreserveFindCase",1); 
	m_chkPreserveCase.SetCheck(pCase); 
	return TRUE;  // return TRUE unless you set the focus to a control 
	              // EXCEPTION: OCX Property Pages should return FALSE 
} 
 
 
BOOL CNoteXpadFindDialog::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)  
{ 
	if (nID ==ID_DOPE_EDIT) 
	{ 
		if (nCode == EN_CHANGE); 
			OnDopeEditChange(); 
	} 
	 
	return CDialog::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo); 
} 
 
void CNoteXpadFindDialog::OnPreserveCase()  
{ 
	CButton* theCheck = (CButton*)GetDlgItem(IDC_PRESERVECASE); 
	 
	AfxGetApp()->WriteProfileInt("Settings","PreserveFindCase",theCheck->GetCheck()); 
 
} 
 
void CNoteXpadFindDialog::OnInitializeFields() 
{ 
	CFrameWnd* pMainWnd =(CFrameWnd*)AfxGetMainWnd(); 
	CEditView* theView = (CEditView*)pMainWnd->GetActiveView(); 
	CDopeEdit* findBox = (CDopeEdit*)GetDlgItem(ID_DOPE_EDIT); 
	CString selectedText; 
	theView->GetSelectedText(selectedText); 
 
	if (selectedText.GetLength() != 0) 
	{ 
		findBox->SetWindowText(selectedText); 
		findBox->OnSelAll(); 
	} 
 
}