--- q_a/samples/cmndlg/cmndlg.c 2018/08/09 18:29:34 1.1.1.2 +++ q_a/samples/cmndlg/cmndlg.c 2018/08/09 18:29:59 1.1.1.3 @@ -1,3 +1,14 @@ + +/******************************************************************************\ +* This is a part of the Microsoft Source Code Samples. +* Copyright (C) 1993 Microsoft Corporation. +* All rights reserved. +* This source code is only intended as a supplement to +* Microsoft Development Tools and/or WinHelp documentation. +* See these sources for detailed information regarding the +* Microsoft samples programs. +\******************************************************************************/ + /**************************************************************************** * * @@ -47,9 +58,6 @@ * * Each dialog box is demonstrated being used in three different ways: * standard, using a modified template and using a hook function. -* Currently, a hook procedure will not work with the ChooseColor() -* common dialog, so this option is disabled when the user chooses -* either Hook or Custom as their current mode. * * ****************************************************************************/ @@ -81,13 +89,13 @@ CHAR szFindString[64] = ""; CHAR szReplaceString[64] = ""; FINDREPLACE frText; LPFINDREPLACE lpFR; -CHAR * BufPtr; +CHAR * lpBufPtr = FileBuf; CHOOSEFONT chf; CHOOSECOLOR chsclr; COLORREF crColor; LOGFONT lf; WORD wMode = IDM_STANDARD; -HWND hDlgFR; +HWND hDlgFR = NULL; PRINTDLG pd; /**************************************************************************** @@ -102,18 +110,18 @@ PRINTDLG pd; ****************************************************************************/ int APIENTRY WinMain( - HANDLE hInstance, - HANDLE hPrevInstance, + HINSTANCE hInstance, + HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) { - MSG msg; /* message */ + MSG msg; /* message */ - if (!hPrevInstance) /* Other instances of app running? */ - if (!InitApplication(hInstance)) /* Initialize shared things */ - return (FALSE); /* Exits if unable to initialize */ + if (!hPrevInstance) /* Other instances of app running? */ + if (!InitApplication(hInstance)) /* Initialize shared things */ + return (FALSE); /* Exits if unable to initialize */ hInst = hInstance; @@ -127,15 +135,16 @@ int APIENTRY WinMain( /* Acquire and dispatch messages until a WM_QUIT message is received. */ - while (GetMessage(&msg, /* message structure */ - NULL, /* handle of window receiving the message */ - NULL, /* lowest message to examine */ - NULL)) /* highest message to examine */ - { - TranslateMessage(&msg); /* Translates virtual key codes */ - DispatchMessage(&msg); /* Dispatches message to window */ - } - return (msg.wParam); /* Returns the value from PostQuitMessage */ + while (GetMessage(&msg, /* message structure */ + NULL, /* handle of window receiving the message */ + 0, /* lowest message to examine */ + 0)) /* highest message to examine */ + if ( !hDlgFR || !IsWindow(hDlgFR) || !IsDialogMessage( hDlgFR, &msg ) ) + { + TranslateMessage(&msg); /* Translates virtual key codes */ + DispatchMessage(&msg); /* Dispatches message to window */ + } + return (msg.wParam); /* Returns the value from PostQuitMessage */ // avoid compiler warnings at W3 lpCmdLine; @@ -156,19 +165,19 @@ int APIENTRY WinMain( * ****************************************************************************/ -BOOL InitApplication(HANDLE hInstance) /* current instance */ +BOOL InitApplication(HANDLE hInstance) /* current instance */ { WNDCLASS wc; /* Fill in window class structure with parameters that describe the */ /* main window. */ - wc.style = NULL; /* Class style(s). */ + wc.style = 0; /* Class style(s). */ wc.lpfnWndProc = (WNDPROC)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.hInstance = hInstance; /* Application that owns the class. */ wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = GetStockObject(WHITE_BRUSH); @@ -253,13 +262,13 @@ BOOL InitInstance( ****************************************************************************/ LONG APIENTRY MainWndProc( - HWND hWnd, /* window handle */ - UINT message, /* type of message */ - UINT wParam, /* additional information */ - LONG lParam) /* additional information */ + HWND hWnd, /* window handle */ + UINT message, /* type of message */ + UINT wParam, /* additional information */ + LONG lParam) /* additional information */ { - FARPROC lpProcAbout; /* pointer to the "About" function */ - FARPROC lpProcEnterNew; /* pointer to the "EnterNew" function */ + FARPROC lpProcAbout; /* pointer to the "About" function */ + FARPROC lpProcEnterNew; /* pointer to the "EnterNew" function */ HDC hDC; PAINTSTRUCT ps; INT nDrawX; @@ -274,6 +283,7 @@ LONG APIENTRY MainWndProc( case WM_CREATE: //initialize the output on the screen strcpy( FileBuf, "Hello World!"); + lpBufPtr = FileBuf; dwFileSize = strlen(FileBuf); crColor = 0; NewFont = FALSE; @@ -281,7 +291,7 @@ LONG APIENTRY MainWndProc( case WM_PAINT: - /* Set up a display context to begin painting */ + /* Set up a display context to begin painting */ hDC = BeginPaint (hWnd, &ps); /* Initialize drawing position to 1/4 inch from the top */ @@ -305,7 +315,7 @@ LONG APIENTRY MainWndProc( break; - case WM_COMMAND: /* message: command from application menu */ + case WM_COMMAND: /* message: command from application menu */ switch( LOWORD( wParam )) { @@ -372,9 +382,7 @@ LONG APIENTRY MainWndProc( break; case IDM_STANDARD: - -#ifdef NOTIMPLEMENTED - // enable the ChooseColor() option + // enable the ChooseColor() option EnableMenuItem(GetMenu(hWnd), IDM_CHOOSECOLOR, MF_BYCOMMAND | MF_ENABLED ); // uncheck previous selection @@ -385,14 +393,11 @@ LONG APIENTRY MainWndProc( CheckMenuItem( GetMenu( hWnd ), wMode, MF_CHECKED | MF_BYCOMMAND); DrawMenuBar( hWnd); break; -#endif case IDM_HOOK: case IDM_CUSTOM: -#ifdef NOTIMPLEMENTED - // disable the ChooseColor() option + // disable the ChooseColor() option EnableMenuItem(GetMenu(hWnd), IDM_CHOOSECOLOR, MF_BYCOMMAND | MF_GRAYED ); -#endif // uncheck previous selection CheckMenuItem( GetMenu( hWnd ), wMode, MF_UNCHECKED | MF_BYCOMMAND); //reset mode @@ -403,56 +408,58 @@ LONG APIENTRY MainWndProc( break; case IDM_ENTERNEW: - lpProcEnterNew = MakeProcInstance((FARPROC)EnterNew, hInst); + lpProcEnterNew = MakeProcInstance((FARPROC)EnterNew, hInst); - if (DialogBox(hInst, /* current instance */ - "EnterNewBox", /* resource to use */ - hWnd, /* parent handle */ - (DLGPROC)lpProcEnterNew) == TRUE) + if (DialogBox(hInst, /* current instance */ + "EnterNewBox", /* resource to use */ + hWnd, /* parent handle */ + (DLGPROC)lpProcEnterNew) == TRUE) InvalidateRect( hWnd, NULL, TRUE ); - FreeProcInstance(lpProcEnterNew); - break; + FreeProcInstance(lpProcEnterNew); + break; case IDM_ABOUT: - lpProcAbout = MakeProcInstance((FARPROC)About, hInst); + lpProcAbout = MakeProcInstance((FARPROC)About, hInst); - DialogBox(hInst, /* current instance */ - "AboutBox", /* resource to use */ - hWnd, /* parent handle */ - (DLGPROC)lpProcAbout); /* About() instance address */ + DialogBox(hInst, /* current instance */ + "AboutBox", /* resource to use */ + hWnd, /* parent handle */ + (DLGPROC)lpProcAbout); /* About() instance address */ - FreeProcInstance(lpProcAbout); - break; + FreeProcInstance(lpProcAbout); + break; default: - return (DefWindowProc(hWnd, message, wParam, lParam)); + return (DefWindowProc(hWnd, message, wParam, lParam)); } break; - case WM_DESTROY: /* message: window being destroyed */ - PostQuitMessage(0); - break; + case WM_DESTROY: /* message: window being destroyed */ + PostQuitMessage(0); + break; - default: + default: // Handle the special findreplace message (FindReplaceMsg) which // was registered at initialization time. if ( message == FindReplaceMsg ) { - lpFR = (LPFINDREPLACE) lParam; - if (lpFR->Flags & FR_DIALOGTERM ) // terminating dialog - return (NULL); - SearchFile( lpFR ); - InvalidateRect( hWnd, NULL, TRUE ); - return (NULL); + if ( lpFR = (LPFINDREPLACE) lParam ) + { + if (lpFR->Flags & FR_DIALOGTERM ) // terminating dialog + return (0); + SearchFile( lpFR ); + InvalidateRect( hWnd, NULL, TRUE ); + } + return (0); } - return (DefWindowProc(hWnd, message, wParam, lParam)); + return (DefWindowProc(hWnd, message, wParam, lParam)); } - return (NULL); + return (0); } @@ -470,35 +477,36 @@ LONG APIENTRY MainWndProc( ****************************************************************************/ BOOL APIENTRY EnterNew( - HWND hDlg, /* window handle of the dialog box */ - UINT message, /* type of message */ - UINT wParam, /* message-specific information */ - LONG lParam) + HWND hDlg, /* window handle of the dialog box */ + UINT message, /* type of message */ + UINT wParam, /* message-specific information */ + LONG lParam) { CHAR Buf[FILE_LEN-1]; switch (message) { - case WM_INITDIALOG: /* message: initialize dialog box */ - return (TRUE); + case WM_INITDIALOG: /* message: initialize dialog box */ + return (TRUE); - case WM_COMMAND: /* message: received a command */ - if (LOWORD(wParam) == IDOK) + case WM_COMMAND: /* message: received a command */ + if (LOWORD(wParam) == IDOK) { GetDlgItemText( hDlg, IDEDIT, Buf, FILE_LEN-1); strcpy( FileBuf, Buf); + lpBufPtr = FileBuf; dwFileSize = strlen(FileBuf); EndDialog( hDlg, TRUE ); return (TRUE); } - else if (LOWORD(wParam) == IDCANCEL) - { /* System menu close command? */ - EndDialog(hDlg, FALSE); /* Exits the dialog box */ - return (TRUE); - } - break; + else if (LOWORD(wParam) == IDCANCEL) + { /* System menu close command? */ + EndDialog(hDlg, FALSE); /* Exits the dialog box */ + return (TRUE); + } + break; } - return (FALSE); /* Didn't process a message */ + return (FALSE); /* Didn't process a message */ // avoid compiler warnings at W3 lParam; @@ -513,33 +521,33 @@ BOOL APIENTRY EnterNew( * * COMMENTS: * -* No initialization is needed for this particular dialog box, but TRUE -* must be returned to Windows. +* 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. +* Wait for user to click on "Ok" button, then close the dialog box. * ****************************************************************************/ BOOL APIENTRY About( - HWND hDlg, /* window handle of the dialog box */ - UINT message, /* type of message */ - UINT wParam, /* message-specific information */ - LONG lParam) + HWND hDlg, /* window handle of the dialog box */ + UINT message, /* type of message */ + UINT wParam, /* message-specific information */ + LONG lParam) { switch (message) { - case WM_INITDIALOG: /* message: initialize dialog box */ - return (TRUE); + case WM_INITDIALOG: /* message: initialize dialog box */ + return (TRUE); - case WM_COMMAND: /* message: received a command */ - if (LOWORD(wParam) == IDOK /* "OK" box selected? */ - || LOWORD(wParam) == IDCANCEL) { /* System menu close command? */ - EndDialog(hDlg, TRUE); /* Exits the dialog box */ - return (TRUE); - } - break; + case WM_COMMAND: /* message: received a command */ + if (LOWORD(wParam) == IDOK /* "OK" box selected? */ + || LOWORD(wParam) == IDCANCEL) { /* System menu close command? */ + EndDialog(hDlg, TRUE); /* Exits the dialog box */ + return (TRUE); + } + break; } - return (FALSE); /* Didn't process a message */ + return (FALSE); /* Didn't process a message */ // avoid compiler warnings at W3 lParam; @@ -568,10 +576,10 @@ BOOL APIENTRY About( ****************************************************************************/ BOOL APIENTRY FileOpenHookProc( - HWND hDlg, /* window handle of the dialog box */ - UINT message, /* type of message */ - UINT wParam, /* message-specific information */ - LONG lParam) + HWND hDlg, /* window handle of the dialog box */ + UINT message, /* type of message */ + UINT wParam, /* message-specific information */ + LONG lParam) { int hFile; @@ -582,8 +590,8 @@ BOOL APIENTRY FileOpenHookProc( switch (message) { - case WM_COMMAND: - if (LOWORD(wParam) == IDOK) + case WM_COMMAND: + if (LOWORD(wParam) == IDOK) { GetDlgItemText( hDlg, edt1, szTempText, sizeof( szTempText ) - 1); @@ -616,6 +624,7 @@ BOOL APIENTRY FileOpenHookProc( } strcpy(FileBuf, "Empty"); + lpBufPtr = FileBuf; dwFileSize = strlen(FileBuf); if (_lwrite( hFile, (LPSTR)&FileBuf[0], dwFileSize)==-1) MessageBox( hDlg, "Error writing file.", NULL, MB_OK ); @@ -624,8 +633,8 @@ BOOL APIENTRY FileOpenHookProc( _lclose( hFile ); } - } - break; + } + break; } return (FALSE); @@ -717,6 +726,7 @@ BOOL OpenNewFile( HWND hWnd ) } // read it's contents into a buffer cBufLen = _lread( hFile, FileBuf, FILE_LEN ); + lpBufPtr = FileBuf; if (cBufLen == -1) { @@ -812,18 +822,18 @@ BOOL SaveToFile( HWND hWnd ) ****************************************************************************/ BOOL APIENTRY FileSaveHookProc( - HWND hDlg, /* window handle of the dialog box */ - UINT message, /* type of message */ - UINT wParam, /* message-specific information */ - LONG lParam) + HWND hDlg, /* window handle of the dialog box */ + UINT message, /* type of message */ + UINT wParam, /* message-specific information */ + LONG lParam) { CHAR szTempText[256]; CHAR szString[256]; switch (message) { - case WM_COMMAND: - if (LOWORD(wParam) == IDOK) + case WM_COMMAND: + if (LOWORD(wParam) == IDOK) { GetDlgItemText( hDlg, edt1, szTempText, sizeof( szTempText ) - 1); @@ -846,8 +856,8 @@ BOOL APIENTRY FileSaveHookProc( return(TRUE); } - } - break; + } + break; } return (FALSE); @@ -924,7 +934,7 @@ BOOL SaveAs( HWND hWnd ) else { ProcessCDError(CommDlgExtendedError(), hWnd ); - return FALSE; + return FALSE; } return (FALSE); @@ -952,24 +962,24 @@ BOOL SaveAs( HWND hWnd ) ****************************************************************************/ BOOL APIENTRY ChooseColorHookProc( - HWND hDlg, /* window handle of the dialog box */ - UINT message, /* type of message */ - UINT wParam, /* message-specific information */ - LONG lParam) + HWND hDlg, /* window handle of the dialog box */ + UINT message, /* type of message */ + UINT wParam, /* message-specific information */ + LONG lParam) { switch (message) { - case WM_COMMAND: - if (LOWORD(wParam) == IDOK) + case WM_COMMAND: + if (LOWORD(wParam) == IDOK) { if (MessageBox( hDlg, "Are you sure you want to change the color?", "Information", MB_YESNO ) == IDYES ) break; return (TRUE); - } - break; + } + break; } return (FALSE); @@ -991,9 +1001,6 @@ BOOL APIENTRY ChooseColorHookProc( * customized template. It then calls the ChooseColor() * common dialog function. * -* Hook procedure in Win 32 for ChoseColor() common dialog is -* not implemented yet. -* * RETURN VALUES: * TRUE - A new color was chosen. * FALSE - No new color was chosen. @@ -1024,13 +1031,10 @@ BOOL ChooseNewColor( HWND hWnd ) { case IDM_HOOK: case IDM_CUSTOM: - -#ifdef NOTIMPLEMENTED -// chsclr.Flags = CC_PREVENTFULLOPEN | CC_ENABLEHOOK; -// chsclr.lpfnHook = (FARPROC)MakeProcInstance(ChooseColorHookProc, NULL); -// chsclr.lpTemplateName = (LPSTR)NULL; -// break; -#endif + chsclr.Flags = CC_PREVENTFULLOPEN | CC_ENABLEHOOK; + chsclr.lpfnHook = (LPCCHOOKPROC)MakeProcInstance(ChooseColorHookProc, NULL); + chsclr.lpTemplateName = (LPSTR)NULL; + break; case IDM_STANDARD: chsclr.Flags = CC_PREVENTFULLOPEN; @@ -1078,10 +1082,10 @@ BOOL ChooseNewColor( HWND hWnd ) ****************************************************************************/ BOOL APIENTRY ChooseFontHookProc( - HWND hDlg, /* window handle of the dialog box */ - UINT message, /* type of message */ - UINT wParam, /* message-specific information */ - LONG lParam) + HWND hDlg, /* window handle of the dialog box */ + UINT message, /* type of message */ + UINT wParam, /* message-specific information */ + LONG lParam) { switch (message) @@ -1094,16 +1098,16 @@ BOOL APIENTRY ChooseFontHookProc( } break; - case WM_COMMAND: - if (LOWORD(wParam) == IDOK) + case WM_COMMAND: + if (LOWORD(wParam) == IDOK) { if (MessageBox( hDlg, "Are you sure you want to change the font?", "Information", MB_YESNO ) == IDYES ) break; return (TRUE); - } - break; + } + break; } return (FALSE); @@ -1177,7 +1181,7 @@ BOOL ChooseNewFont( HWND hWnd ) { DeleteDC( hDC ); ProcessCDError(CommDlgExtendedError(), hWnd ); - return FALSE; + return FALSE; } @@ -1207,15 +1211,15 @@ BOOL ChooseNewFont( HWND hWnd ) ****************************************************************************/ BOOL APIENTRY PrintSetupHookProc( - HWND hDlg, /* window handle of the dialog box */ - UINT message, /* type of message */ - UINT wParam, /* message-specific information */ - LONG lParam) + HWND hDlg, /* window handle of the dialog box */ + UINT message, /* type of message */ + UINT wParam, /* message-specific information */ + LONG lParam) { switch (message) { - case WM_INITDIALOG: + case WM_INITDIALOG: if (pd.Flags & PD_ENABLESETUPTEMPLATE ) { ShowWindow( GetDlgItem(hDlg, psh1), SW_HIDE ); @@ -1258,10 +1262,10 @@ BOOL APIENTRY PrintSetupHookProc( ****************************************************************************/ BOOL APIENTRY PrintDlgHookProc( - HWND hDlg, /* window handle of the dialog box */ - UINT message, /* type of message */ - UINT wParam, /* message-specific information */ - LONG lParam) + HWND hDlg, /* window handle of the dialog box */ + UINT message, /* type of message */ + UINT wParam, /* message-specific information */ + LONG lParam) { switch (message) @@ -1278,16 +1282,16 @@ BOOL APIENTRY PrintDlgHookProc( "Information", MB_OK ); return (TRUE); - case WM_COMMAND: - if (LOWORD(wParam) == IDOK) + case WM_COMMAND: + if (LOWORD(wParam) == IDOK) { if (MessageBox( hDlg, "Are you sure you want to print?", "Information", MB_YESNO ) == IDYES ) break; return (TRUE); - } - break; + } + break; } return (FALSE); @@ -1409,22 +1413,20 @@ void PrintFile( HWND hWnd ) ****************************************************************************/ BOOL APIENTRY ReplaceTextHookProc( - HWND hDlg, /* window handle of the dialog box */ - UINT message, /* type of message */ - UINT wParam, /* message-specific information */ - LONG lParam) + HWND hDlg, /* window handle of the dialog box */ + UINT message, /* type of message */ + UINT wParam, /* message-specific information */ + LONG lParam) { - switch (message) { - case WM_INITDIALOG: + case WM_INITDIALOG: if (frText.Flags & FR_ENABLETEMPLATE ) - { - ShowWindow( GetDlgItem(hDlg, psh2), SW_HIDE ); - ShowWindow( GetDlgItem(hDlg, chx1), SW_HIDE ); - ShowWindow( GetDlgItem(hDlg, chx2), SW_HIDE ); - return(TRUE); - } + { + ShowWindow( GetDlgItem(hDlg, psh2), SW_HIDE ); + ShowWindow( GetDlgItem(hDlg, chx1), SW_HIDE ); + ShowWindow( GetDlgItem(hDlg, chx2), SW_HIDE ); + } MessageBox( hDlg, "Hook installed.", "Information", MB_OK ); @@ -1432,7 +1434,7 @@ BOOL APIENTRY ReplaceTextHookProc( default: - break; + break; } return (FALSE); @@ -1461,15 +1463,15 @@ BOOL APIENTRY ReplaceTextHookProc( ****************************************************************************/ BOOL APIENTRY FindTextHookProc( - HWND hDlg, /* window handle of the dialog box */ - UINT message, /* type of message */ - UINT wParam, /* message-specific information */ - LONG lParam) + HWND hDlg, /* window handle of the dialog box */ + UINT message, /* type of message */ + UINT wParam, /* message-specific information */ + LONG lParam) { switch (message) { - case WM_INITDIALOG: + case WM_INITDIALOG: if (frText.Flags & FR_ENABLETEMPLATE ) { ShowWindow(GetDlgItem(hDlg, chx1), SW_HIDE); @@ -1477,7 +1479,6 @@ BOOL APIENTRY FindTextHookProc( ShowWindow(GetDlgItem(hDlg, chx2), SW_HIDE); ShowWindow(GetDlgItem(hDlg, rad1), SW_HIDE); ShowWindow(GetDlgItem(hDlg, rad2), SW_HIDE); - return(TRUE); } MessageBox( hDlg, "Hook installed.", @@ -1486,7 +1487,7 @@ BOOL APIENTRY FindTextHookProc( default: - break; + break; } return (FALSE); @@ -1524,11 +1525,12 @@ void CallFindText( HWND hWnd ) 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.Flags = FR_NOMATCHCASE | FR_NOUPDOWN | FR_NOWHOLEWORD; frText.lpfnHook = (LPFRHOOKPROC)(FARPROC)NULL; frText.lpTemplateName = (LPSTR)NULL; break; @@ -1581,6 +1583,7 @@ void CallReplaceText( HWND hWnd ) frText.wFindWhatLen = sizeof(szFindString); frText.wReplaceWithLen = sizeof( szReplaceString ); frText.lCustData = 0; + lpBufPtr = FileBuf; switch( wMode ) { @@ -1605,8 +1608,8 @@ void CallReplaceText( HWND hWnd ) break; } - if ((hDlgFR = ReplaceText( &frText )) == NULL) - ProcessCDError(CommDlgExtendedError(), hWnd ); + if ( (hDlgFR = ReplaceText( &frText )) == NULL ) + ProcessCDError(CommDlgExtendedError(), hWnd ); } @@ -1633,42 +1636,82 @@ void SearchFile( LPFINDREPLACE lpFR ) CHAR Buf[FILE_LEN]; CHAR *pStr; - int count; - - - strnset(Buf, '\0', FILE_LEN -1); - if ((pStr = strstr( FileBuf, lpFR->lpstrFindWhat )) == NULL) + int count, newcount; + static BOOL bFoundLast = FALSE; + +if ( lpFR->Flags & ( FR_FINDNEXT | FR_REPLACE | FR_REPLACEALL ) ) { - sprintf( Buf, "%s not found.", lpFR->lpstrFindWhat ); - MessageBox( lpFR->hwndOwner, Buf, "No luck", MB_OK | MB_TASKMODAL); - } + memset(Buf, '\0', FILE_LEN -1); + if ( bFoundLast ) + { + if ( (lpBufPtr != FileBuf) && (lpFR->Flags & FR_FINDNEXT) ) + lpBufPtr++; + bFoundLast = FALSE; + } - else - { - if ( lpFR->wReplaceWithLen == 0) + if (!*lpBufPtr || !(pStr = strstr( lpBufPtr, lpFR->lpstrFindWhat ) ) ) { - sprintf( Buf, "%s found!", lpFR->lpstrFindWhat ); - MessageBox( lpFR->hwndOwner, Buf, "Success!", MB_OK | MB_TASKMODAL ); + sprintf( Buf, "'%s' not found!", lpFR->lpstrFindWhat ); + lpBufPtr = FileBuf; + MessageBox( lpFR->hwndOwner, Buf, "No luck", MB_OK | MB_TASKMODAL); } - else + else { + if ( lpFR->Flags & FR_FINDNEXT ) + { + sprintf( Buf, "Found Next '%s'!\nSubstring: '%.10s'", + lpFR->lpstrFindWhat, pStr ); + lpBufPtr = pStr; + bFoundLast = TRUE; + MessageBox( lpFR->hwndOwner, Buf, "Success!", MB_OK | MB_TASKMODAL ); + } + else if ( lpFR->Flags & FR_REPLACE ) + { // replace string specified in the replace with found string // copy up to found string into new buffer - for (count=0; ; count++) - if (*pStr == *(FileBuf+count)) - break; - strncpy( Buf, FileBuf, count ); + for( count=0; + *pStr && lpBufPtr[count] && *pStr != lpBufPtr[count]; + count++); + strncpy( Buf, lpBufPtr, count ); // concatenate new string strcat( Buf, lpFR->lpstrReplaceWith ); // copy rest of string (less the found string) - count += strlen(lpFR->lpstrFindWhat); - strcat( Buf, &FileBuf[count]); - strcpy( FileBuf, Buf ); + newcount = count + strlen(lpFR->lpstrFindWhat); + strcat( Buf, lpBufPtr+newcount); + strcpy( lpBufPtr, Buf ); + lpBufPtr += count + strlen(lpFR->lpstrReplaceWith); dwFileSize = strlen(FileBuf); - MessageBox( lpFR->hwndOwner, Buf, "Success!", MB_OK | MB_TASKMODAL ); + MessageBox( lpFR->hwndOwner, FileBuf, "Success!", MB_OK | MB_TASKMODAL ); + } + else if ( lpFR->Flags & FR_REPLACEALL) + { + do + { + // replace string specified in the replace with found string + // copy up to found string into new buffer + memset(Buf, '\0', FILE_LEN -1); + for( count=0; + *pStr && lpBufPtr[count] && *pStr != lpBufPtr[count]; + count++); + strncpy( Buf, lpBufPtr, count ); + // concatenate new string + strcat( Buf, lpFR->lpstrReplaceWith ); + // copy rest of string (less the found string) + newcount = count + strlen(lpFR->lpstrFindWhat); + strcat( Buf, lpBufPtr + newcount); + strcpy( lpBufPtr, Buf ); + lpBufPtr += count + strlen(lpFR->lpstrReplaceWith); + } + while ( *lpBufPtr && + (pStr = strstr( lpBufPtr, lpFR->lpstrFindWhat ) ) ); + dwFileSize = strlen(FileBuf); + lpBufPtr = FileBuf; + MessageBox( lpFR->hwndOwner, FileBuf, + "Success!", MB_OK | MB_TASKMODAL ); + } + } } - EndDialog( hDlgFR, FALSE ); }