MFC:RichEdit and CFindReplaceDialog

CFindReplaceDialog 类:

所谓的CFindReplaceDialog 指的就是下面的东西

对于这个类,你是不用去添加就有的,即直接可用:

afxdlgs.h 中

class CFindReplaceDialog : public CCommonDialog
{
    DECLARE_DYNAMIC(CFindReplaceDialog)

public:
// Attributes
    FINDREPLACE m_fr;

// Constructors
    CFindReplaceDialog();
    // Note: you must allocate these on the heap.
    //  If you do not, you must derive and override PostNcDestroy()

    virtual BOOL Create(BOOL bFindDialogOnly, // TRUE for Find, FALSE for FindReplace
            LPCTSTR lpszFindWhat,
            LPCTSTR lpszReplaceWith = NULL,
            DWORD dwFlags = FR_DOWN,
            CWnd* pParentWnd = NULL);

    // find/replace parameter block
    static CFindReplaceDialog* PASCAL GetNotifier(LPARAM lParam);

// Operations
    // Helpers for parsing information after successful return
    CString GetReplaceString() const;// get replacement string
    CString GetFindString() const;   // get find string
    BOOL SearchDown() const;         // TRUE if search down, FALSE is up
    BOOL FindNext() const;           // TRUE if command is 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 all occurrences
    BOOL IsTerminating() const;      // TRUE if terminating dialog

// Implementation
protected:
    virtual void PostNcDestroy();

#ifdef _DEBUG
public:
    virtual void Dump(CDumpContext& dc) const;
#endif

protected:
    TCHAR m_szFindWhat[128];
    TCHAR m_szReplaceWith[128];
};

对于这个东西的用法相信大家都知道,在使用的时候还是可以对主窗口时行操作的,即是非模态类型:

CFindReplaceDialog *pFindDlg;
int index = 0;

void CMFCApplication5Dlg::OnBnClickedButton1() // 这个函数仅仅是用于触发这个窗口,最好去限制只出现一个窗口
{
    index = 0;
    pFindDlg = new CFindReplaceDialog;   //由于是非模态对话框这里用new命令分配内存
    if (!pFindDlg->Create(TRUE, _T(""), NULL, FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD, this))//这里可以去获取鼠标所在的字符串
    {
        return;
    }
    pFindDlg->ShowWindow(SW_SHOW);   //窗口创建完毕要显示出来
    pFindDlg->SetActiveWindow();    //设置为活动窗口
    // TODO: Add your control notification handler code here
}

当然CFindReplaceDialog 怎么和主窗口关联起来呢?

对于这个窗口的操作都会有一个FINDMSGSTRING 消息发到方窗口,所以主窗口只要对这个消息进行处理就可以了:

static UINT WM_FINDREPLACE = ::RegisterWindowMessage(FINDMSGSTRING);


BEGIN_MESSAGE_MAP(CMFCApplication5Dlg, CDialogEx)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_BN_CLICKED(IDOK, &CMFCApplication5Dlg::OnBnClickedOk)
    ON_BN_CLICKED(IDC_BUTTON1, &CMFCApplication5Dlg::OnBnClickedButton1)
    ON_REGISTERED_MESSAGE(WM_FINDREPLACE, OnFindReplace)
END_MESSAGE_MAP()

之后在主窗口类中定义对应的函数:
LONG CMFCApplication5Dlg::OnFindReplace(WPARAM wparam, LPARAM lparam)
{
    BOOL if_whole_word = pFindDlg->MatchWholeWord();
    BOOL if_case = pFindDlg->MatchCase();
    BOOL if_down = pFindDlg->SearchDown();

    bool last_one = false;
    int nindex = 0;
    do
    {
        CString find_str, text_str;

        m_RichEdit.GetWindowText(text_str);
        find_str = pFindDlg->GetFindString();
        int len = find_str.GetLength();
        if (if_down)//向下找
        {
            if (nindex != text_str.GetLength() - 1)
                nindex++;
            else
                MessageBox(L"已经向下查找到文件尾!", L"查找替换", MB_OK | MB_ICONINFORMATION);
            index = text_str.Find(find_str, index);
        }
        else
        {
            /*
            if (nindex != 0)
                nindex--;
            else
                MessageBox(L"已经向上查找到文件头!", L"查找替换", MB_OK | MB_ICONINFORMATION);
            CString str = text_str.GetBuffer(0);
            index = str.find_last_of(find_str, nindex);
            index = str.
            text_str.ReleaseBuffer();*/
        }

        if (index != -1)
        {
            m_RichEdit.SetSel(index, index + len);
            index++;
        }
        else
        {
            last_one = true;
            index = 0;
            MessageBox(L"已经查找到最后一个!", L"查找替换", MB_OK | MB_ICONINFORMATION);

        }

        if (pFindDlg->ReplaceCurrent() || pFindDlg->ReplaceAll())
        {
            CString replace_str;
            m_RichEdit.ReplaceSel(pFindDlg->GetReplaceString(), TRUE);
        }
        nindex = index;
    } while (pFindDlg->ReplaceAll() && !last_one);

    m_RichEdit.SetFocus();//这句代码一定不能少,否则不能正常运行

    return 0;
}

对于RichEdit 的换行要注意 mulitiline属性为TRUE,wantreturn属性,也要设置为true。Auto HScroll也要设置为FALSE

还要注意在BOOL CMFCApplication5App::InitInstance() 中加    AfxInitRichEdit2();// 我也不知道为什么要加,知道怎么用就好(没有时间)

由于工作于mfc不相关,而且很忙,所以这些代码很烂。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值