47上位机VC MFC实现文件替换功能 
 
 47上位机VC MFC实现文件替换功能 功能展示 一般的软件都有查找/替换功能,可以进行查找和替换操作CFindReplaceDialog类封装了查找替换功能,非常方便的实现我了我们想要的功能,我们当前例程就来用这个类实现查找替换的功能,效果如图 要点提示 要想知道如何使用这个类CFindReplaceDialog,我们就得翻阅MSDN看看使用说明;根据MSDN中的说法当用户点击查找下一个、替换、替换全部或者关闭对话框时,查找替换对话框会向窗体发送FINDMSGSTRING,所以我们需要捕捉这个消息,建立对应的消息响应函数,并用CFindReplaceDialog的方法判断具体进行了什么操作; 新建一个全局的常量WM_FINDREPLACEMESSAGE,并利用RegisterWindowMessage()进行关联,如人们例程static UINT  WM_FINDMESSAGE =RegisterWindowMessage(FINDMSGSTRING);这样对查找替换对话框进行操作时,消息队列中便会有了WM_FINDREPLACEMESSAGE消息了,这样我们可以建立对应的消息响应函数,对用户对查找替换对话框的操作进行响应。 下面是从MSDN复制过来常用的函数,顾名思义也行好理解 CString GetReplaceString() const;// get replacementstring CString GetFindString() const;   // get findstring BOOL SearchDown() const;        // TRUE if search down, FALSEis up BOOL FindNext() const;          // TRUE if commandis find next BOOL MatchCase() const;         // TRUE if matching case BOOL MatchWholeWord() const;     //TRUE if matching whole words only BOOL ReplaceCurrent() const;     //TRUE if replacing current string BOOL ReplaceAll() const;        // TRUE if replacing alloccurrences  
实现功能 1.新建基于对话框的应用程序 2.添加一些对话框变量:拖拽Cedit编辑框控件和Cbutton按钮控件,实现按钮消息响应,关联编辑框控件变量CEdit  m_Edit;添加查找文件对话框CFindReplaceDialog *m_pFindDlg; 以及int m_nIndex; //存储查找字符串的起始位置; 3.在对话框初始化函数OnInitDialog()中,实现一些初始化操作 - CString str="";
 
 -         str += "和文件对话框(CFileDialog)一样\r\n";
 
 -         str += "这个对话框也是VC自带的对话框\r\n";
 
 -         str += "但是让我们比较汗颜的是。\r\n";
 
 -         str += "当你点击查找或者替换按钮时。\r\n";
 
 -         str += "VC并没有给我们封装这些功能。\r\n";
 
 -         str += "需要我们自己写代码实现……囧\r\n";
 
 -         str += "根据MSDN中的说法。\r\n";
 
 -         str += "当用户点击查找下一个、替换、替换全部或者关闭对话框时。\r\n";
 
 -         str += "查找替换对话框会向窗体发送FINDMSGSTRING\r\n";
 
 -         str += "所以我们需要捕捉这个消息\r\n";
 
 -         str += "建立对应的消息响应函数\r\n";
 
 -         str += "并用CFindReplaceDialog的方法判断具体进行了什么操作";
 
 -         m_Edit.SetWindowText(str);
 
 -         m_nIndex = -1;
 
 
  复制代码4.自定义一消息static UINT WM_FINDMESSAGE = RegisterWindowMessage(FINDMSGSTRING); //新定义一个消息,完成系统CFindReplaceDialog类的查找功能实现;并实现这一消息的映射;函数定义,函数映射,函数体,如下 - afx_msg  long OnFindReplace(WPARAM wParam,LPARAM lParam); ON_REGISTERED_MESSAGE(WM_FINDMESSAGE, OnFindReplace )
 
 - long CGkbc8Dlg::OnFindReplace(WPARAM wParam, LPARAM lParam)
 
 - {if(m_pFindDlg->IsTerminating())//如果用户退出窗口时
 
 -         {
 
 -                 m_nIndex = -1;
 
 -                 return 0;
 
 -         }
 
 -     BOOL if_whole_word = m_pFindDlg->MatchWholeWord();
 
 -     BOOL if_case = m_pFindDlg->MatchCase();
 
 -     BOOL if_down = m_pFindDlg->SearchDown();
 
 -     bool last_one = false;
 
 -     do
 
 -     {
 
 -         CString find_str, text_str;
 
 -         int index;
 
 -         m_Edit.GetWindowText(text_str);
 
 -         find_str = m_pFindDlg->GetFindString();
 
 -         int len = find_str.GetLength();
 
 - if (if_down)//向下找
 
 -         {
 
 -             if (m_nIndex != text_str.GetLength()-1)
 
 -                 m_nIndex++;
 
 -             else
 
 -                 MessageBox("已经向下查找到文件尾!", "查找替换", MB_OK | MB_ICONINFORMATION);
 
 -             index = text_str.Find(find_str, m_nIndex);
 
 -         }
 
 -         else
 
 -         {
 
 -                 if (m_nIndex!=0)
 
 -                     m_nIndex--;
 
 -                 else
 
 -                     MessageBox("已经向上查找到文件头!", "查找替换", MB_OK | MB_ICONINFORMATION);
 
 -                 CString str = text_str.GetBuffer(0);
 
 -                 index = str.Find(find_str, m_nIndex);
 
 -                 text_str.ReleaseBuffer();
 
 -         }
 
 
  复制代码- if (index != -1)
 
 -         {        
 
 -             m_Edit.SetSel(index, index + len);
 
 -         }
 
 -         else
 
 -         {
 
 -             last_one = true;
 
 -             MessageBox("已经查找到最后一个!", "查找替换", MB_OK | MB_ICONINFORMATION);
 
 -         }
 
 -         if (m_pFindDlg->ReplaceCurrent() || m_pFindDlg->ReplaceAll())
 
 -         {
 
 -             CString replace_str;
 
 -             m_Edit.ReplaceSel(m_pFindDlg->GetReplaceString(), TRUE);
 
 -         }
 
 -         m_nIndex = index;
 
 -     }
 
 -     while (m_pFindDlg->ReplaceAll() && !last_one);
 
 -     m_Edit.SetFocus();//这句代码一定不能少,否则不能正常运行
 
  
-     return 0;
 
 - }
 
 
  复制代码5.最后一步就是调用查找对话框,实现查找功能 - void CGkbc8Dlg::OnButton1() 
 
 - {
 
 -         m_pFindDlg = new CFindReplaceDialog;
 
 -         m_pFindDlg->Create(FALSE,NULL);
 
 -         m_pFindDlg->ShowWindow(SW_SHOW);
 
 - }
 
 
  复制代码我们来演示整个功能实现的过程 源码及视频下载 (仅在电脑可见)  
  
 |