The Find common dialog box, shown in Figure 6-11, prompts the user to enter a string of text and then searches for that string. The Replace dialog box, which is quite similar, not only searches for a specified string but also replaces it with a specified replace string. Unlike the other common dialog boxes, these two dialog boxes are modeless, which means that the user can switch between the dialog box and the window that created it.
The Find and Replace common dialog boxes use the FINDREPLACE structure. When this structure is initialized, the application calls the FindText function to display the Find dialog box or the ReplaceText function to display the Replace dialog box. Within the FINDREPLACE structure, the application can specify items such as the owner of the dialog box, an initial string to
Figure 6-11.
search for, whether to match the case of the string, whether to match the entire string, and hook and template information.
For an application to process messages from a Find or a Replace dialog box, the application must use the RegisterWindowMessage function to register the dialog box's unique message, FINDMSGSTRING. The following code from CMNDLG32 handles the FindReplaceMsg message:
LONG APIENTRY MainWndProc (
HWND hWnd, // window handle
UINT message, // type of message
UINT wParam, // additional information
LONG lParam) // additional information
{
§
switch (message)
{
case WM_CREATE:
§
default:
// Handle the special find-replace message (FindReplaceMsg) that
// was registered at initialization time.
if (message == FindReplaceMsg)
{
if (lpFR = (LPFINDREPLACE)lParam)
{
if (lpFR->Flags & FR_DIALOGTERM) // terminating dialog
return 0;
SearchFile (lpFR);
InvalidateRect (hWnd, NULL, TRUE);
}
return (0);
}
§
}
§
}
The following example initializes the FINDREPLACE structure and then calls FindText. The flags specified for the dialog box stipulate that the Match Case, Up and Down, and Match Whole Word Only options should be disabled. The hook and template are enabled if the user has specified them.
void CallFindText (HWND hWnd)
{
frText.lStructSize = sizeof (frText);
frText.hwndOwner = hWnd;
frText.hInstance = (HANDLE)hInst;
frText.lpstrFindWhat = szFindString;
frText.lpstrReplaceWith = (LPTSTR)NULL;
frText.wFindWhatLen = sizeof (szFindString);
frText.wReplaceWithLen = 0;
frText.lCustData = 0;
lpBufPtr = FileBuf;
switch (wMode)
{
case IDM_STANDARD:
frText.Flags = FR_NOMATCHCASE | FR_NOUPDOWN | FR_NOWHOLEWORD;
frText.lpfnHook = (LPFRHOOKPROC)(FARPROC)NULL;
frText.lpTemplateName = (LPTSTR)NULL;
break;
case IDM_HOOK:
frText.Flags = FR_NOMATCHCASE | FR_NOUPDOWN | FR_NOWHOLEWORD |
FR_ENABLEHOOK;
frText.lpfnHook = (LPFRHOOKPROC)FindTextHookProc;
frText.lpTemplateName = (LPTSTR)NULL;
break;
case IDM_CUSTOM:
frText.Flags = FR_NOMATCHCASE | FR_NOUPDOWN | FR_NOWHOLEWORD |
FR_ENABLEHOOK | FR_ENABLETEMPLATE;
frText.lpfnHook = (LPFRHOOKPROC)FindTextHookProc;
frText.lpTemplateName = (LPTSTR) MAKEINTRESOURCE (FINDDLGORD);
break;
}
if ((hDlgFR = FindText (&frText)) == NULL)
ProcessCDError (CommDlgExtendedError (), hWnd);
}
The Replace common dialog box, shown in Figure 6-12, resembles the Find dialog box. It lacks Direction options, however, and it contains three additional controls that let the user specify a replacement string and choose whether to replace one or all occurrences of the string.
Figure 6-12.
The code to initialize the FINDREPLACE structure for the Replace common dialog box is nearly identical to the code that initializes this structure for the Find common dialog box. The only difference is that you must include a pointer to a string in the lpstrReplaceWith member and specify the size of that string buffer in the wReplaceWithLen member. After you initialize the FINDREPLACE structure, the application calls the ReplaceText function to display the dialog box.