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;
}
|