📄 notexpadreplacedlg.cpp
字号:
// NoteXpadReplaceDlg.cpp : implementation file
//
#include "stdafx.h"
#include "notexpad.h"
#include "NoteXpadReplaceDlg.h"
#include "notexpadview.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
UINT ID_REPLACE_EDIT = 1699;
UINT ID_REPLACEWITH_EDIT = 1700;
/////////////////////////////////////////////////////////////////////////////
// CNoteXpadReplaceDlg dialog
CNoteXpadReplaceDlg::CNoteXpadReplaceDlg(CWnd* pParent /*=NULL*/)
: CDialog(CNoteXpadReplaceDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CNoteXpadReplaceDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void CNoteXpadReplaceDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CNoteXpadReplaceDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CNoteXpadReplaceDlg, CDialog)
//{{AFX_MSG_MAP(CNoteXpadReplaceDlg)
ON_BN_CLICKED(IDC_BUTTON1, OnReplaceAll)
ON_WM_CREATE()
ON_BN_CLICKED(IDREPLACE, OnReplace)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CNoteXpadReplaceDlg message handlers
int CNoteXpadReplaceDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
CRect rect1(12,30,285,50);
CRect rect2(12,75,285,95);
m_ReplaceString.Create(WS_CHILD|WS_VISIBLE|WS_TABSTOP|ES_AUTOHSCROLL,rect1,this,ID_REPLACE_EDIT);
m_ReplaceWithString.Create(WS_CHILD|WS_VISIBLE|WS_TABSTOP|ES_AUTOHSCROLL,rect2,this,ID_REPLACEWITH_EDIT);
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;
return 0;
}
BOOL CNoteXpadReplaceDlg::OnInitDialog()
{
CDialog::OnInitDialog();
m_ReplaceString.SubclassDlgItem(ID_REPLACE_EDIT,this);
m_ReplaceWithString.SubclassDlgItem(ID_REPLACEWITH_EDIT,this);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CNoteXpadReplaceDlg::OnInitializeFields()
{
CDopeEdit* theEdit = (CDopeEdit*)GetDlgItem(ID_REPLACE_EDIT);
CFrameWnd* pMainWnd =(CFrameWnd*)AfxGetMainWnd();
CNoteXpadView* theView = (CNoteXpadView*)pMainWnd->GetActiveView();
CString searchQuery;
searchQuery = theView->GetSelectedTxt();
if (searchQuery.GetLength() !=0)
{
theEdit->SetFocus();
theEdit->SetWindowText(searchQuery);
theEdit->SetSel(0,searchQuery.GetLength());
}
}
BOOL CNoteXpadReplaceDlg::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
{
if (nID == ID_REPLACE_EDIT && nCode == EN_CHANGE)
OnField1Change();
return CDialog::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}
void CNoteXpadReplaceDlg::OnField1Change()
{
CEdit* field1 = (CEdit*)GetDlgItem(ID_REPLACE_EDIT);
CButton* replaceBtn = (CButton*)GetDlgItem(IDREPLACE);
CButton* replaceAllBtn = (CButton*)GetDlgItem(IDC_REPLACEALL);
switch (field1->GetWindowTextLength())
{
case 0:
replaceBtn->EnableWindow(FALSE);
replaceAllBtn->EnableWindow(FALSE);
break;
default:
replaceBtn->EnableWindow(TRUE);
replaceAllBtn->EnableWindow(TRUE);
break;
}
}
void CNoteXpadReplaceDlg::OnReplace()
{
CString windowText,bufferText,oldStr,newStr;
CFrameWnd* pMainWnd =(CFrameWnd*)AfxGetMainWnd();
CEdit* theView = (CEdit*)pMainWnd->GetActiveView();
int startofsel,endofsel;
theView->GetWindowText(windowText);
CDopeEdit* QueryBox = (CDopeEdit*)GetDlgItem(ID_REPLACE_EDIT);
CDopeEdit* ReplaceWithBox = (CDopeEdit*)GetDlgItem(ID_REPLACEWITH_EDIT);
QueryBox->GetWindowText(oldStr);
ReplaceWithBox->GetWindowText(newStr);
int start = windowText.Find(oldStr);
theView->GetSel(startofsel,endofsel);
if (startofsel == start && endofsel == (start+oldStr.GetLength()))
{ //First instance of replacement is already selected
theView->ReplaceSel(newStr,TRUE);
int locationOfSel = windowText.GetLength() - (start+oldStr.GetLength());
CString temp = windowText.Right(locationOfSel);
start = temp.Find(oldStr);
if (start !=-1)
{
start +=windowText.GetLength() - temp.GetLength()+newStr.GetLength()-oldStr.GetLength();
theView->SetSel(start,start+oldStr.GetLength());
}
else
{
AfxMessageBox("Finished searching through the view.",MB_OK|MB_ICONINFORMATION);
theView->SetSel(-1,-1);
}
return;
}
if (start == -1) //Text not found.
{
AfxMessageBox("String not found in view!",MB_OK|MB_ICONINFORMATION);
QueryBox->SetFocus();
QueryBox->OnSelAll();
}
else //Text was found
{
theView->SetSel(start, start+oldStr.GetLength() );
}
}
void CNoteXpadReplaceDlg::OnReplaceAll()
{
CString windowText,oldStr,newStr;
CFrameWnd* pMainWnd =(CFrameWnd*)AfxGetMainWnd();
CEdit* theView = (CEdit*)pMainWnd->GetActiveView();
CDopeEdit* QueryBox = (CDopeEdit*)GetDlgItem(ID_REPLACE_EDIT);
CDopeEdit* ReplaceWithBox = (CDopeEdit*)GetDlgItem(ID_REPLACEWITH_EDIT);
theView->GetWindowText(windowText);
QueryBox->GetWindowText(oldStr);
ReplaceWithBox->GetWindowText(newStr);
if (windowText.Find(oldStr) !=-1)
{
int rMade = windowText.Replace(oldStr,newStr);
theView->SetWindowText(windowText);
CString msg;
msg.Format("%d Successful replacements in file.",rMade);
AfxMessageBox(msg,MB_OK|MB_ICONINFORMATION);
}
else
{
AfxMessageBox("String not found in view!",MB_OK|MB_ICONINFORMATION);
QueryBox->SetFocus();
QueryBox->OnSelAll();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -