CFindReplaceDialog
The CFindReplaceDialog
class supports the use of the Windows Find
and Replace dialog in MFC
applications.
Use of the
Find and Replace dialog is fundamentally different from
the user of other common dialogs. While other common
dialogs are modal, the Find and Replace dialog is modeless
and must be constructed and used accordingly.
To use the
Find and Replace dialog, first construct a CFindReplaceDialog
object. This object cannot be constructed on the stack; it
must be created using the new operator to ensure its
persistence after the function in which it is created
returns.
The Find and
Replace dialog communicates with its owner window using
special messages. To enable use of these messages, use the
::RegisterWindowMessage function. The value obtained upon
registering Find and Replace dialog messages can be used
in a window's message map using the ON_REGISTERED_MESSAGE
macro.
The actual
dialog is created by calling the Create member function.
Make sure to specify the window that should receive
messages from the dialog in the call to CFindReplaceDialog::Create.
Now you're
ready to put all this into practice. Listing 22.3 shows
how the use of the Find and Replace dialog is implemented
in the CDLG application. All the code shown here is part
of the MainFrm.cpp source file.
// MainFrm.cpp : implementation of the CMainFrame class
//
...
static UINT WM_FINDREPLACE = RegisterWindowMessage(FINDMSGSTRING);
///////////////////////////////////////////////////////////////////
// CMainFrame
IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
...
//}}AFX_MSG_MAP
ON_REGISTERED_MESSAGE(WM_FINDREPLACE, OnFindReplace)
END_MESSAGE_MAP()
...
void CMainFrame::OnViewFindreplacedialog()
{
// TODO: Add your command handler code here
CFindReplaceDialog *pDlg = new CFindReplaceDialog;
pDlg->Create(TRUE, "Findme", NULL, FR_DOWN, this);
}
LRESULT CMainFrame::OnFindReplace(WPARAM wParam, LPARAM lParam)
{
if (((LPFINDREPLACE)lParam)->Flags & FR_FINDNEXT)
{
CString temp = "Search string: ";
temp = temp + ((LPFINDREPLACE)lParam)->lpstrFindWhat;
AfxMessageBox(temp);
}
return 0;
}
The function
OnFindReplace must also be declared in MainFrm.h. Towards
the end of the class declaration, after the ClassWizard-generated
message map, but before the DECLARE_MESSAGE_MAP macro
call, insert the following line:
afx_msg LRESULT OnFindReplace(WPARAM wParam, LPARAM lParam);
Customization
of the Find and Replace dialog involves deriving a class
from CFindReplaceDialog,
providing a custom dialog template, and adding your own
message map to process notification messages from any
extra controls you added. The original dialog template can
be found in the file findtext.dlg in your \msdev\include
directory.
|