www.pudn.com > NoteBookVC++.rar > NEWFINDREPLACE.cpp


// NEWFINDREPLACE.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "记事本.h" 
#include "NEWFINDREPLACE.h" 
 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// NEWFINDREPLACE dialog 
 
 
NEWFINDREPLACE::NEWFINDREPLACE(CWnd* pParent /*=NULL*/) 
	: CDialog(NEWFINDREPLACE::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(NEWFINDREPLACE) 
		// NOTE: the ClassWizard will add member initialization here 
	//}}AFX_DATA_INIT 
	Text.Format(""); 
    FromDown=TRUE; 
	 
	NEWFINDREPLACE::FindStr.Format(""); 
	NEWFINDREPLACE::ReplaceStr.Format(""); 
	result=-1; 
} 
 
 
void NEWFINDREPLACE::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(NEWFINDREPLACE) 
		// NOTE: the ClassWizard will add DDX and DDV calls here 
	//}}AFX_DATA_MAP 
	 
} 
 
 
BEGIN_MESSAGE_MAP(NEWFINDREPLACE, CDialog) 
	//{{AFX_MSG_MAP(NEWFINDREPLACE) 
	ON_BN_CLICKED(IDREPLACE, OnReplace) 
	ON_BN_CLICKED(IDALLREPLACE, OnAllreplace) 
	ON_BN_CLICKED(IDC_RADIODOWN, OnRadiodown) 
	ON_BN_CLICKED(IDC_RADIOUP, OnRadioup) 
	ON_EN_CHANGE(IDC_EDIT1, OnChangeEdit1) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// NEWFINDREPLACE message handlers 
 
BOOL NEWFINDREPLACE::OnInitDialog()  
{ 
	RECT lrect; 
    //得到主窗口相对桌面的位置 
	AfxGetMainWnd()->GetWindowRect(&lrect); 
	//设置对话框相对桌面的位置 
	CDialog::SetWindowPos(NULL,lrect.left+150,lrect.top+100,250,155,NULL); 
	CDialog::OnInitDialog(); 
	 
	button1.SubclassDlgItem(IDOK,this); 
	button1.EnableWindow(FALSE); 
	button2.SubclassDlgItem(IDREPLACE,this); 
	button2.EnableWindow(FALSE); 
	button3.SubclassDlgItem(IDALLREPLACE,this); 
	button3.EnableWindow(FALSE); 
 
	// TODO: Add extra initialization here 
	Find.SubclassDlgItem(IDC_EDIT1,this); 
	Replace.SubclassDlgItem(IDC_EDIT2,this); 
	if (FromDown==TRUE) 
		NEWFINDREPLACE::CheckRadioButton(IDC_RADIOUP,IDC_RADIODOWN,IDC_RADIODOWN); 
	else 
        NEWFINDREPLACE::CheckRadioButton(IDC_RADIOUP,IDC_RADIODOWN,IDC_RADIOUP); 
	return TRUE;  // return TRUE unless you set the focus to a control 
	              // EXCEPTION: OCX Property Pages should return FALSE 
} 
 
 
 
void NEWFINDREPLACE::OnOK()  
{ 
	 
	// TODO: Add extra validation here 
    CMyApp *App=(CMyApp *)AfxGetApp(); 
    
	Find.GetWindowText(FindStr); 
    App->Afx_text->FindStr.Format(FindStr); 
	Replace.GetWindowText(ReplaceStr); 
    App->Afx_text->ReplacdStr.Format(ReplaceStr); 
	App->Afx_text->edit.GetWindowText(Text); 
    DWORD position=App->Afx_text->edit.GetSel(); 
     
	if (NEWFINDREPLACE::FromDown==TRUE) 
	{ 
	result=Text.Find(FindStr,HIWORD(position)+FindStr.GetLength()); 
	App->Afx_text->FindResult=result; 
	} 
	else 
	{ 
    Text.MakeReverse(); 
	FindStr.MakeReverse(); 
	result=Text.Find(FindStr,Text.GetLength()-1-LOWORD(position)); 
    if (result!=-1) 
       	result=Text.GetLength()-result-FindStr.GetLength(); 
    App->Afx_text->FindResult=result; 
	} 
    
    if (result!=-1) 
	{ 
	 
		//需修改 
	   
		App->Afx_text->edit.SetSel(result,result+FindStr.GetLength(),FALSE); 
	    
		// 
	} 
	else  
		NEWFINDREPLACE::MessageBox("找不到指定字符串 !","查找结果",MB_OK); 
 
} 
 
void NEWFINDREPLACE::OnReplace()  
{ 
	// TODO: Add your control notification handler code here 
	CMyApp *App=(CMyApp *)AfxGetApp(); 
   
	Find.GetWindowText(FindStr); 
    App->Afx_text->FindStr.Format(FindStr); 
	Replace.GetWindowText(ReplaceStr); 
    App->Afx_text->ReplacdStr.Format(ReplaceStr); 
 
	if(result!=-1) 
	{ 
	result=-1; 
	App->Afx_text->FindResult=result; 
    App->Afx_text->edit.ReplaceSel(ReplaceStr,TRUE); 
    App->Afx_text->edit.GetWindowText(Text); 
	} 
	else 
        NEWFINDREPLACE::MessageBox("找不到指定字符串 !","查找结果",MB_OK); 
} 
 
void NEWFINDREPLACE::OnAllreplace()  
{ 
	// TODO: Add your control notification handler code here 
    CMyApp *App=(CMyApp *)AfxGetApp(); 
	Find.GetWindowText(FindStr); 
    App->Afx_text->FindStr.Format(FindStr); 
	Replace.GetWindowText(ReplaceStr); 
    App->Afx_text->ReplacdStr.Format(ReplaceStr); 
 	App->Afx_text->edit.GetWindowText(Text); 
	int i=0; 
	result=Text.Find(FindStr,i); 
    while(result!=-1) 
	{ 
    App->Afx_text->edit.SetSel(result,result+FindStr.GetLength(),FALSE); 
    App->Afx_text->edit.ReplaceSel(ReplaceStr,FALSE); 
    App->Afx_text->edit.GetWindowText(Text); 
	i=result+ReplaceStr.GetLength(); 
	result=Text.Find(FindStr,i); 
	} 
	if (i==0) 
	NEWFINDREPLACE::MessageBox("找不到指定字符串 !","查找结果",MB_OK); 
    else 
	NEWFINDREPLACE::MessageBox("所有替换成功 !","替换结果",MB_OK); 
	 
} 
 
 
 
void NEWFINDREPLACE::OnCancel()  
{ 
	// TODO: Add extra cleanup here 
    Text.Format(""); 
	 
	CDialog::OnCancel(); 
} 
 
 
 
void NEWFINDREPLACE::OnRadiodown()  
{ 
	// TODO: Add your control notification handler code here 
	FromDown=TRUE; 
} 
 
void NEWFINDREPLACE::OnRadioup()  
{ 
	// TODO: Add your control notification handler code here 
	FromDown=FALSE; 
} 
 
 
void NEWFINDREPLACE::OnChangeEdit1()  
{ 
	// 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. 
	Find.GetWindowText(FindStr); 
	if (FindStr.Compare("")==0) 
	{ 
	button1.EnableWindow(FALSE); 
	button2.EnableWindow(FALSE); 
	button3.EnableWindow(FALSE); 
	} 
	else 
	{ 
	button1.EnableWindow(TRUE); 
	button2.EnableWindow(TRUE); 
	button3.EnableWindow(TRUE); 
	} 
 
	// TODO: Add your control notification handler code here 
	 
}