Code:
/****************************************************************************
PROGRAM: Generic.c
PURPOSE: Generic template for Windows applications
FUNCTIONS:
WinMain() - calls initialization function, processes message loop
InitApplication() - initializes window data and registers window
InitInstance() - saves instance handle and creates main window
MainWndProc() - processes messages
About() - processes messages for "About" dialog box
COMMENTS:
Windows can have several copies of your application running at the
same time. The variable hInst keeps track of which instance this
application is so that processing will be to the correct window.
****************************************************************************/
#include "windows.h" /* required for all Windows applications */
#include "generic.h" /* specific to this program */
#include <commdlg.h>
#include <tchar.h> /* for _tcsstr (strstr for Unicode & non-Unicode) */
#define MAXSEARCHSTRING 80
/******Prototypes********/
long FAR PASCAL MainWndProc(HWND, UINT, WPARAM, LPARAM);
BOOL FAR PASCAL About(HWND, unsigned, WORD, LONG);
BOOL InitApplication(HANDLE);
BOOL InitInstance(HANDLE, int);
BOOL FAR PASCAL FindDialog (HWND);
BOOL PopFindFindText (HWND, int *, LPFINDREPLACE) ;
BOOL PopFindNextText (HWND, int *) ;
/* BOOL PopFindValidFind (void) ; */
HWND hWnd;
HANDLE hInst; /* current instance */
HWND hEditWnd; /* handle to edit control */
static HWND hDlgFind; /* handle to modeless FindText window */
static UINT wFRMsg; /* message used in communicating */
/* with Find/Replace dialog */
RECT Rect;
/* global search string */
static char lpszSearch[MAXSEARCHSTRING+1] = "";
/****************************************************************************
FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
PURPOSE: calls initialization function, processes message loop
COMMENTS:
Windows recognizes this function by name as the initial entry point
for the program. This function calls the application initialization
routine, if no other instance of the program is running, and always
calls the instance initialization routine. It then executes a message
retrieval and dispatch loop that is the top-level control structure
for the remainder of execution. The loop is terminated when a WM_QUIT
message is received, at which time this function exits the application
instance by returning the value passed by PostQuitMessage().
If this function must abort before entering the message loop, it
returns the conventional value NULL.
****************************************************************************/
int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG msg; /* message */
if (!hPrevInstance)
if (!InitApplication(hInstance))
return (FALSE);
if (!InitInstance(hInstance, nCmdShow))
return (FALSE);
/* Acquire and dispatch messages until a WM_QUIT message is received. */
while (GetMessage(&msg, /* message structure */
NULL, /* handle of window receiving the message */
0, /* lowest message to examine */
0)) /* highest message to examine */
{
if ((!hDlgFind || !IsDialogMessage(hDlgFind, &msg))) {
TranslateMessage(&msg); /* Translates virtual key codes */
DispatchMessage(&msg); /* Dispatches message to window */
}
}
return (msg.wParam); /* Returns the value from PostQuitMessage */
}
/****************************************************************************
FUNCTION: InitApplication(HANDLE)
PURPOSE: Initializes window data and registers window class
****************************************************************************/
BOOL InitApplication(HANDLE hInstance)
{
WNDCLASS wc;
/* Fill in window class structure with parameters that describe the */
/* main window. */
wc.style = 0; /* Class style(s). */
wc.lpfnWndProc = MainWndProc; /* Function to retrieve messages for */
/* windows of this class. */
wc.cbClsExtra = 0; /* No per-class extra data. */
wc.cbWndExtra = 0; /* No per-window extra data. */
wc.hInstance = hInstance; /* Application that owns the class. */
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = "GenericMenu"; /* Name of menu resource in .RC file. */
wc.lpszClassName = "GenericWClass"; /* Name used in call to CreateWindow. */
/* Register the window class and return success/failure code. */
return (RegisterClass(&wc));
}
/****************************************************************************
FUNCTION: InitInstance(HANDLE, int)
PURPOSE: Saves instance handle and creates main window
****************************************************************************/
BOOL InitInstance(HANDLE hInstance, int nCmdShow)
{
RECT Rect;
hInst = hInstance;
/* Create a main window for this application instance. */
hWnd = CreateWindow(
"GenericWClass", /* See RegisterClass() call. */
"Generic Sample Application", /* Text for window title bar. */
WS_OVERLAPPEDWINDOW, /* Window style. */
0, /* Default horizontal position. */
0, /* Default vertical position. */
CW_USEDEFAULT, /* Default width. */
CW_USEDEFAULT, /* Default height. */
NULL, /* Overlapped windows have no parent. */
NULL, /* Use the window class menu. */
hInstance, /* This instance owns this window. */
NULL /* Pointer not needed. */
);
if (!hWnd)
return (FALSE);
/* determine the message number to be used for communication with
* Find dialog
*/
wFRMsg = RegisterWindowMessage ((LPSTR)FINDMSGSTRING);
GetClientRect(hWnd, (LPRECT) &Rect);
/* Create a child window */
hEditWnd = CreateWindow("Edit",
NULL,
WS_CHILD | WS_VISIBLE |
ES_MULTILINE |
WS_VSCROLL | WS_HSCROLL |
ES_AUTOHSCROLL | ES_AUTOVSCROLL,
0,
0,
(Rect.right-Rect.left),
(Rect.bottom-Rect.top),
hWnd,
(HMENU) IDC_EDIT, /* Child control i.d. */
hInst,
NULL);
if (!hEditWnd) {
DestroyWindow(hWnd);
return (0);
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return (TRUE);
}
/****************************************************************************
FUNCTION: MainWndProc(HWND, UINT, WPARAM, LPARAM)
PURPOSE: Processes messages
MESSAGES:
WM_COMMAND - application menu (About dialog box)
WM_DESTROY - destroy window
COMMENTS:
To process the IDM_ABOUT message, call MakeProcInstance() to get the
current instance address of the About() function. Then call Dialog
box which will create the box according to the information in your
generic.rc file and turn control over to the About() function. When
it returns, free the intance address.
****************************************************************************/
long FAR PASCAL MainWndProc(hWnd, message, wParam, lParam) /* LRESULT CALLBACK */
HWND hWnd; /* window handle */
UINT message; /* type of message */
WPARAM wParam; /* additional information */
LPARAM lParam; /* additional information */
{
static int iOffset ;
FINDREPLACE far* lpfr;
/* LPFINDREPLACE lpfr; */
FARPROC lpProcAbout; /* pointer to the "About" function */
switch (message) {
case WM_COMMAND: /* message: command from application menu */
switch (LOWORD(wParam)) {
case IDM_SEARCHFIND:
SendMessage (hEditWnd, EM_GETSEL, 0, (LPARAM) &iOffset) ;
hDlgFind = FindDialog (hWnd) ;
break;
case IDM_SEARCHNEXT:
SendMessage (hEditWnd, EM_GETSEL, 0, (LPARAM) &iOffset) ;
if (lpszSearch != '\0')
PopFindNextText (hEditWnd, &iOffset) ;
else
hDlgFind = FindDialog (hWnd) ;
break;
case IDM_SEARCHPREV:
/* locate previous search string in edit control */
break;
case IDM_ABOUT: {
lpProcAbout = MakeProcInstance(About, hInst);
DialogBox(hInst, /* current instance */
"AboutBox", /* resource to use */
hWnd, /* parent handle */
lpProcAbout); /* About() instance address */
FreeProcInstance(lpProcAbout);
}
break;
default:
return (DefWindowProc(hWnd, message, wParam, lParam));
}
break;
case WM_SETFOCUS:
SetFocus (hEditWnd);
break;
case WM_DESTROY: /* message: window being destroyed */
PostQuitMessage(0);
break;
default: /* Passes it on if unproccessed */
/* this can be a message from the modeless Find Text window */
if (message == wFRMsg)
{
lpfr = (LPFINDREPLACE)lParam;
if (lpfr->Flags & FR_DIALOGTERM)
{
hDlgFind = NULL; /* invalidate modeless window handle */
}
else if (lpfr->Flags & FR_FINDNEXT)
if (!PopFindFindText (hEditWnd, &iOffset, lpfr))
MessageBox (hWnd, "String Not Found", "Editfile", MB_OK);
break;
}
return (DefWindowProc(hWnd, message, wParam, lParam));
}
return (0);
}
/****************************************************************************
FUNCTION: About(HWND, unsigned, WORD, LONG)
PURPOSE: Processes messages for "About" dialog box
MESSAGES:
WM_INITDIALOG - initialize dialog box
WM_COMMAND - Input received
COMMENTS:
No initialization is needed for this particular dialog box, but TRUE
must be returned to Windows.
Wait for user to click on "Ok" button, then close the dialog box.
****************************************************************************/
BOOL FAR PASCAL About(hDlg, message, wParam, lParam) /* LRESULT CALLBACK */
HWND hDlg; /* window handle of the dialog box */
unsigned message; /* type of message */
WORD wParam; /* message-specific information */
LONG lParam;
{
switch (message) {
case WM_INITDIALOG: /* message: initialize dialog box */
return (TRUE);
case WM_COMMAND: /* message: received a command */
switch (wParam) {
case IDOK:
EndDialog(hDlg, TRUE); /* Exits the dialog box */
return (TRUE);
}
break;
}
return (FALSE); /* Didn't process a message */
}
/* invoke the common search/replace dialog */
BOOL FAR PASCAL FindDialog (HWND hWnd)
{
static FINDREPLACE fr; /* Passed to FindText() */
fr.lStructSize = sizeof (FINDREPLACE);
fr.hwndOwner = hWnd;
fr.hInstance = (HANDLE)NULL;
fr.Flags = FR_HIDEUPDOWN | FR_HIDEMATCHCASE | FR_HIDEWHOLEWORD;
fr.lpstrFindWhat = lpszSearch;
fr.lpstrReplaceWith = NULL;
fr.wFindWhatLen = MAXSEARCHSTRING+1;
fr.wReplaceWithLen = 0;
fr.lCustData = 0;
fr.lpfnHook = NULL;
fr.lpTemplateName = NULL;
/* call common search dialog */
if (hDlgFind = FindText (&fr))
return TRUE;
else
return FALSE;
}
BOOL PopFindFindText (HWND hEditWnd, int * piSearchOffset, LPFINDREPLACE pfr)
{
int iLength, iPos ;
PTSTR pstrDoc, pstrPos ;
// Read in the edit document
iLength = GetWindowTextLength (hEditWnd) ;
if (NULL == (pstrDoc = (PTSTR) malloc ((iLength + 1) * sizeof (TCHAR))))
return FALSE ;
GetWindowText (hEditWnd, pstrDoc, iLength + 1) ;
// Search the document for the find string
pstrPos = _tcsstr (pstrDoc + * piSearchOffset, pfr->lpstrFindWhat) ;
free (pstrDoc) ;
// Return an error code if the string cannot be found
if (pstrPos == NULL)
return FALSE ;
// Find the position in the document and the new start offset
iPos = pstrPos - pstrDoc ;
* piSearchOffset = iPos + lstrlen (pfr->lpstrFindWhat) ;
// Select the found text
SendMessage (hEditWnd, EM_SETSEL, iPos, * piSearchOffset) ;
SendMessage (hEditWnd, EM_SCROLLCARET, 0, 0) ;
return TRUE ;
}
BOOL PopFindNextText (HWND hEditWnd, int * piSearchOffset)
{
FINDREPLACE fr ;
fr.lpstrFindWhat = lpszSearch ;
return PopFindFindText (hEditWnd, piSearchOffset, &fr) ;
}
/* BOOL PopFindValidFind (void)
{
return * lpszSearch != '\0' ;
} */