Find Replace Dialog Box example

The syntax of the FindText function is

HWND FindText(LPFINDREPLACE lpfr);

Where

LPFINDREPLACE lpfr is Pointer to a FINDREPLACE structure that contains information used to initialise the dialog box. If the function succeeds, the return value is the window handle to the dialog box. This handle can be used to communicate with or to close the dialog box. If the function fails, the return value is NULL.

//find replace dialog box
////////////////////////////////////////////////////////////////////////////
//
// resource file attributes
//
////////////////////////////////////////////////////////////////////////////
/*
IDC_MAIN MENU DISCARDABLE
BEGIN
POPUP "File"
BEGIN
MENUITEM "Exit", IDM_EXIT
END
POPUP "Edit"
BEGIN
MENUITEM "find", IDM_FIND
END
END

*/
/////////////////////////////////////////////////////////////////////////////

#include <windows.h>
#include <tchar.h>
#include "resource.h"

//create text be searched
const TCHAR worddata[] ="It was the best of times, it was the worst of times,"\
" it was the age of wisdom, it was the age of foolishness, it was the epoch of belief"\
", it was the epoch of incredulity, it was the season of Light, "\
"it was the season of Darkness, it was the spring of hope, it was the winter of despair, "\
"we had everything before us, we had nothing before us, we were all going direct to Heaven, "\
"we were all going direct the other way – in short, the period was so far like the present period, "\
"that some of its noisiest authorities insisted on its being received, for good or for evil, "\
"in the superlative degree of comparison only.";

HINSTANCE hInst = NULL;
const UINT uFindReplaceMsg = RegisterWindowMessage(FINDMSGSTRING);//Registering FINDMSGSTRING
HWND hdlg = NULL; // handle to Find dialog box

int findFrom = 0;
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
//search file
int SearchFile(const TCHAR* find, BOOL down, BOOL ignoreCase)
{
if(0 < _tcslen(find))
{
const TCHAR* start = &worddata[findFrom];
const TCHAR* pos = _tcsstr(start, find);

if(NULL != pos)
{
int foundAt = (pos - worddata);
findFrom = foundAt + _tcslen(find);
return foundAt;
}
}
findFrom = 0;
return -1;
}

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("TextFind Demo") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass = {0};

hInst = hInstance;

wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = NULL;
wndclass.lpszMenuName = MAKEINTRESOURCE(IDC_MAIN);
wndclass.lpszClassName = szAppName ;

if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("Program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}

hwnd = CreateWindow (szAppName, TEXT ("Find Text in Edit Control Demo"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;

ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;

while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HWND hEdit = NULL;
static HBRUSH hbrWhite = (HBRUSH)GetStockObject(WHITE_BRUSH);

switch (message)
{
case WM_CREATE:
hEdit = CreateWindow (_T("EDIT"), worddata,WS_CHILD | WS_VISIBLE | ES_MULTILINE | ES_READONLY | ES_NOHIDESEL,0, 0, 0, 0, hwnd, (HMENU)100, hInst, NULL) ;
return 0;

case WM_SIZE:
if(hEdit!=NULL)
MoveWindow(hEdit, 0, 0, LOWORD(lParam), HIWORD(lParam), FALSE);
return 0;

case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDM_FIND:
static FINDREPLACE fr;
static CHAR szFindWhat[80]; // buffer receiving string

// Initialize FINDREPLACE
ZeroMemory(&fr, sizeof(fr));
fr.lStructSize = sizeof(fr);
fr.hwndOwner = hwnd;
fr.lpstrFindWhat = szFindWhat;
fr.wFindWhatLen = 80;
fr.Flags = 0;
hdlg = FindText(&fr);
return 0;

case IDM_EXIT:
DestroyWindow(hwnd);
return 0;

}
break;

case WM_CTLCOLORSTATIC:
return (LRESULT)hbrWhite;

case WM_DESTROY :
PostQuitMessage (0) ;
return 0 ;

default:
LPFINDREPLACE lpfr;
//respond to FINDMSGSTRING message
if (message == uFindReplaceMsg)
{
lpfr = (LPFINDREPLACE)lParam;
if (lpfr->Flags & FR_DIALOGTERM)
{
hdlg = NULL;
return 0;
}
if (lpfr->Flags & FR_FINDNEXT)
{
int find = SearchFile(lpfr->lpstrFindWhat,
(BOOL) (lpfr->Flags & FR_DOWN),
(BOOL) (lpfr->Flags & FR_MATCHCASE));
if(-1 == find)
{
PostMessage(hEdit, EM_SETSEL, (WPARAM)-1, (LPARAM)0);
}
else
{
int to = (find + _tcslen(lpfr->lpstrFindWhat));
PostMessage(hEdit, EM_SETSEL, (WPARAM)find, (LPARAM)to);
}
}

return 0;
}
else
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
return 0;
}