Find DialogBox events handling

Hi i created a find dilog box.Where should i hnadle the FR_FINDNEXT message.i registered the FINDMSGSTRING in WinMain Function after Registering the main window.is it correct??
You have studied this??

Using Common Dialog Boxes / Finding Text
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646829%28v=vs.85%29.aspx#finding_text

Andy
Last edited on
Ya i studied this document,I created Find Dilog box with same code.But some times iam getting access violation.This is the code i have written
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
switch (message)
    {
case WM_COMMAND:
		
switch(LOWORD(wParam))
{
case ID_FIND40012:
UINT uFindReplaceMsg;  // message identifier for FINDMSGSTRING 

uFindReplaceMsg = RegisterWindowMessage(FINDMSGSTRING);

FINDREPLACE fr;       // common dialog box structure
HWND hwnd;            // owner window
WCHAR szFindWhat[80];  // buffer receiving string
HWND hdlg = NULL;     // handle to Find dialog box

// 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);
break;

}
}

Where should i write the below part code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
LPFINDREPLACE lpfr;

if (message == uFindReplaceMsg)
{ 
    // Get pointer to FINDREPLACE structure from lParam.
    lpfr = (LPFINDREPLACE)lParam;

    // If the FR_DIALOGTERM flag is set, 
    // invalidate the handle that identifies the dialog box. 
    if (lpfr->Flags & FR_DIALOGTERM)
    { 
        hdlg = NULL; 
        return 0; 
    } 

    // If the FR_FINDNEXT flag is set, 
    // call the application-defined search routine
    // to search for the requested string. 
    if (lpfr->Flags & FR_FINDNEXT) 
    {
        SearchFile(lpfr->lpstrFindWhat,
                   (BOOL) (lpfr->Flags & FR_DOWN), 
                   (BOOL) (lpfr->Flags & FR_MATCHCASE)); 
    }

    return 0; 
}
Where should i write the below part code?
1
2
3
LPFINDREPLACE lpfr;

if (message == uFindReplaceMsg)

You handle the registered message in the default handler of the switch for "message".

You do know that the WndProc you posted is broken?

If you do, it would be better to post something that is clearly a fragment rather than something that looks broken.

If you don't, you need to know how to work with WndProcs first and then have another go with FindText.

But note that:

1. RegisterWindowMessage need to be called just once, either in the WM_CREATE handler (or WM_INITDIALOG if the main window is a dialog) or in WinMain, and the message ID stored somewhere it can be seen.

2. FindText is called in the WM_COMMAND handler

3. And as I already said above, you handle the registered message in the default handler of the switch for "message".

Andy

PS It would also be easier for other people to see what's going on if you indent your code.
Sorry because of my time constrnt i did like that.Below iam writting the full code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <windows.h>
UINT uFindReplaceMsg;  // message identifier for FINDMSGSTRING 
HWND hdlg = NULL;     // handle to Find dialog box

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

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

     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 = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     wndclass.lpszMenuName  = NULL ;
     wndclass.lpszClassName = szAppName ;
     
     if (!RegisterClass (&wndclass))
     {
          MessageBox (NULL, TEXT ("Program requires Windows NT!"), 
                      szAppName, MB_ICONERROR) ;
          return 0 ;
     }
     
     hwnd = CreateWindow (szAppName, TEXT ("Mouse Button & Capture Demo"),
                          WS_OVERLAPPEDWINDOW,
                          CW_USEDEFAULT, CW_USEDEFAULT,
                          CW_USEDEFAULT, CW_USEDEFAULT,
                          NULL, NULL, hInstance, NULL) ;
     
     ShowWindow (hwnd, iCmdShow) ;
     UpdateWindow (hwnd) ;
     uFindReplaceMsg = RegisterWindowMessage(FINDMSGSTRING);//Regestering FINDMSGSTRING

     
     while (GetMessage (&msg, NULL, 0, 0))
     {
          TranslateMessage (&msg) ;
          DispatchMessage (&msg) ;//Getting access violation after creating Find dilog box & green arrow showing here.
     }
     return msg.wParam ;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
          
     switch (message)
     {
              
            WM_COMMAND:
			switch(LOWORD(wParam))
			{
				case ID_FIND40012:
				UINT uFindReplaceMsg;  // message identifier for FINDMSGSTRING 

				uFindReplaceMsg = RegisterWindowMessage(FINDMSGSTRING);

				FINDREPLACE fr;       // common dialog box structure
				HWND hwnd;            // owner window
				WCHAR 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);
				break;

			}
			break;
			 case WM_PAINT ://paint operation
						   return 0 ;
				  
			 case WM_DESTROY :
				  PostQuitMessage (0) ;
				  return 0 ;
			 
			default:

				LPFINDREPLACE lpfr;

				if (message == uFindReplaceMsg)
				{
					// Get pointer to FINDREPLACE structure from lParam.
					lpfr = (LPFINDREPLACE)lParam;

					// If the FR_DIALOGTERM flag is set, 
					// invalidate the handle that identifies the dialog box. 
					if (lpfr->Flags & FR_DIALOGTERM)
					{ 
						hdlg = NULL; 
						return 0; 
					} 

					// If the FR_FINDNEXT flag is set, 
					// call the application-defined search routine
					// to search for the requested string. 
					if (lpfr->Flags & FR_FINDNEXT) 
					{
						SearchFile(lpfr->lpstrFindWhat,
								   (BOOL) (lpfr->Flags & FR_DOWN), 
								   (BOOL) (lpfr->Flags & FR_MATCHCASE)); 
					}

					return 0; 
				} 

			

				
		}
		 return DefWindowProc (hwnd, message, wParam, lParam) ;
}
Please correct me.Please
Last edited on
Please correct me.Please

I will check your code if you first go back and sort out the indexing so I can follow it more easily.

I am not going to waste my time trying to read badly laid out code.

Andy
Last edited on
Please Help me..........
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <windows.h>
UINT uFindReplaceMsg;  // message identifier for FINDMSGSTRING 
HWND hdlg = NULL;     // handle to Find dialog box

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

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

     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 = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     wndclass.lpszMenuName  = NULL ;
     wndclass.lpszClassName = szAppName ;
     
     if (!RegisterClass (&wndclass))
     {
          MessageBox (NULL, TEXT ("Program requires Windows NT!"), 
                      szAppName, MB_ICONERROR) ;
          return 0 ;
     }
     
     hwnd = CreateWindow (szAppName, TEXT ("Mouse Button & Capture Demo"),
                          WS_OVERLAPPEDWINDOW,
                          CW_USEDEFAULT, CW_USEDEFAULT,
                          CW_USEDEFAULT, CW_USEDEFAULT,
                          NULL, NULL, hInstance, NULL) ;
     
     ShowWindow (hwnd, iCmdShow) ;
     UpdateWindow (hwnd) ;
     uFindReplaceMsg = RegisterWindowMessage(FINDMSGSTRING);//Regestering FINDMSGSTRING

     
     while (GetMessage (&msg, NULL, 0, 0))
     {
          TranslateMessage (&msg) ;
          DispatchMessage (&msg) ;//Getting access violation after creating Find dilog box & green arrow showing here.
     }
     return msg.wParam ;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
          
     switch (message)
     {
              
            WM_COMMAND:
			switch(LOWORD(wParam))
			{
				case ID_FIND40012:
				UINT uFindReplaceMsg;  // message identifier for FINDMSGSTRING 

				uFindReplaceMsg = RegisterWindowMessage(FINDMSGSTRING);

				FINDREPLACE fr;       // common dialog box structure
				HWND hwnd;            // owner window
				WCHAR 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);
				break;

			}
			break;
			 case WM_PAINT ://paint operation
						   return 0 ;
				  
			 case WM_DESTROY :
				  PostQuitMessage (0) ;
				  return 0 ;
			 
			default:

				LPFINDREPLACE lpfr;

				if (message == uFindReplaceMsg)
				{
					// Get pointer to FINDREPLACE structure from lParam.
					lpfr = (LPFINDREPLACE)lParam;

					// If the FR_DIALOGTERM flag is set, 
					// invalidate the handle that identifies the dialog box. 
					if (lpfr->Flags & FR_DIALOGTERM)
					{ 
						hdlg = NULL; 
						return 0; 
					} 

					// If the FR_FINDNEXT flag is set, 
					// call the application-defined search routine
					// to search for the requested string. 
					if (lpfr->Flags & FR_FINDNEXT) 
					{
						SearchFile(lpfr->lpstrFindWhat,
								   (BOOL) (lpfr->Flags & FR_DOWN), 
								   (BOOL) (lpfr->Flags & FR_MATCHCASE)); 
					}

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

				
		}
		return 0;
}
Please correct me.Please

Last edited on
I think you want case WM_COMMAND: in line 58 rather than just WM_COMMAND:, which is a reference for goto....
Last edited on
When I tried to compile your code, the WM_COMMAND line caused the compiler to emit an error message, so you should have been able to spot that yourself!

The crash is because the variables fr and FindWhat went out of scope. The MSDN example is a bit bad about that, but this is pretty simple stuff: that you cannot use the address of a local variable after the function has gone out of scope.

The following code uses the FindText function so search some text, for illustrative purposes.

Andy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <windows.h>
#include <tchar.h>
#include "resource.h"

////AWK something to search...
const TCHAR testdata[] =
_T("A \"Hello world\" program is a computer program that outputs \"Hello, world\" on a display")
_T(" device. Because it is typically one of the simplest programs possible in most programming")
_T(" languages, it is by tradition often used to illustrate to beginners the most basic syntax")
_T(" of a programming language, or to verify that a language or system is operating correctly.\r\n")
_T("")
_T("In a device that does not display text, a simple program to produce a signal, such as")
_T(" turning on an LED, is often substituted for \"Hello world\" as the introductory program.");
//// Text from:
//// Hello world program
//// http://en.wikipedia.org/wiki/Hello_world_program

HINSTANCE hInst = NULL;

//// AWT init uFindReplaceMsg at global scope
////UINT uFindReplaceMsg;  // message identifier for FINDMSGSTRING 
const UINT uFindReplaceMsg = RegisterWindowMessage(FINDMSGSTRING);//Regestering FINDMSGSTRING
HWND hdlg = NULL;     // handle to Find dialog box

int findFrom = 0;

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

//// AWK Pretend file search; searches text which is assumed to be
//// in step with the read-only edit control
int SearchFile(const TCHAR* find, BOOL down, BOOL ignoreCase)
{
	if(0 < _tcslen(find))
	{
		const TCHAR* start = &testdata[findFrom];
		const TCHAR* pos   = _tcsstr(start, find);

		if(NULL != pos)
		{
			int foundAt = (pos - testdata);
			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") ; //// AWK
     HWND         hwnd ;
     MSG          msg ;
     WNDCLASS     wndclass = {0}; //// AWK zero struct

     hInst = hInstance; //// AWK remember instance 

     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; //// AWK was (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     wndclass.lpszMenuName  = MAKEINTRESOURCE(IDC_MAIN); //// AWK added menu (was NULL)
     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) ;
	 //// AWK init now done at global scope
     ////uFindReplaceMsg = RegisterWindowMessage(FINDMSGSTRING);//Regestering FINDMSGSTRING

     
     while (GetMessage (&msg, NULL, 0, 0))
     {
          TranslateMessage (&msg) ;
          DispatchMessage (&msg) ;//Getting access violation after creating Find dilog box & green arrow showing here.
     }
     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"), testdata,
							  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(NULL != hEdit)
				 MoveWindow(hEdit, 0, 0, LOWORD(lParam), HIWORD(lParam), FALSE);
			 return 0;
              
            case WM_COMMAND:
			switch(LOWORD(wParam))
			{
				case IDM_FIND: // // AWK was ID_FIND40012
				////UINT uFindReplaceMsg;  // message identifier for FINDMSGSTRING
				////AWK Don't want the local variable, want the global 

				////uFindReplaceMsg = RegisterWindowMessage(FINDMSGSTRING);

				//// AWK need to be static as their addresses are being used later
				static FINDREPLACE fr;       // common dialog box structure
				////HWND hwnd;            // AWK owner window IS PASSED TO US
				static WCHAR 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; //// AWK was break;

				case IDM_EXIT:
					DestroyWindow(hwnd);
				return 0;


			}
			break;

			//// AWK make read-only Edit control white
			case WM_CTLCOLORSTATIC:
				return (LRESULT)hbrWhite;

			case WM_PAINT ://paint operation
						   return 0 ;

			//// AWK Edit control handles its own background, so do not
			//// repaint background (avoids flicker.)
			 case WM_ERASEBKGND :
				 return TRUE;

			 case WM_DESTROY :
				  PostQuitMessage (0) ;
				  return 0 ;
			 
			default:

				LPFINDREPLACE lpfr;

				if (message == uFindReplaceMsg)
				{
					// Get pointer to FINDREPLACE structure from lParam.
					lpfr = (LPFINDREPLACE)lParam;

					// If the FR_DIALOGTERM flag is set, 
					// invalidate the handle that identifies the dialog box. 
					if (lpfr->Flags & FR_DIALOGTERM)
					{ 
						hdlg = NULL; 
						return 0; 
					} 

					// If the FR_FINDNEXT flag is set, 
					// call the application-defined search routine
					// to search for the requested string. 
					if (lpfr->Flags & FR_FINDNEXT) 
					{
						//// AWK for illustrative purposes, just highlight words in Edit control
						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;
}


rc file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "winresrc.h"
#include "resource.h"

IDC_MAIN MENU
BEGIN
    POPUP "&File"
    BEGIN
        MENUITEM "E&xit",                IDM_EXIT
    END
    POPUP "&Edit"
    BEGIN
        MENUITEM "&Find",              IDM_FIND
    END
END



resource.h

1
2
3
#define IDC_MAIN   800
#define IDM_FIND  1000
#define IDM_EXIT  1001 


Last edited on
Topic archived. No new replies allowed.