Find/Replace problem

This is a discussion on Find/Replace problem within the Windows Programming forums, part of the Platform Specific Boards category; Maybe someone can explain why the Find/Replace modeless dialog box (common dialog) doesn't quite work correctly in the following C ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    87

    Find/Replace problem

    Maybe someone can explain why the Find/Replace modeless dialog box (common dialog) doesn't quite work correctly in the following C source code. When compiled, the Find fuction works as it's supposed to with one exception: the found string isn't highlighted in the edit control unless you click on the program application. Basically, it's not quite true modeless dialog as it should be.
    The program is just Microsoft's Generic sample application with the Find stuff from Petz Poppad application added.
    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' ;
    } */

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    87
    Wow...Kinda surprised no one even has a guess as to why the modeless dialog doesn't quite work right for the Find function.

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    Because you are not returning 0 for the wFRMsg msg to show you have processed it (and you are then calling the default processing)?


    I would not use hDlgFind as it will contain true/false most of the time (which may be the problem, as 'true' may also be considered, or found to be, an invalid handle value).

    Code:
    hDlgFind = FindDialog (hWnd) ;
    
    //but FindDialog() returns a BOOL
    
    BOOL FAR PASCAL FindDialog (HWND hWnd)
    {
        /* call common search dialog */
        if (hDlgFind = FindText (&fr))
    	return TRUE; 
        else
    	return FALSE;
    }
    Last edited by novacain; 08-14-2010 at 10:18 PM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    87
    Even if I change the call to FindDialog to the following:

    Code:
    HWND FindDialog (HWND hWnd)
    {
    static FINDREPLACE fr;
    
        fr.lStructSize = sizeof (FINDREPLACE);
        /* the rest of struct */
    
        /* call common search dialog */
        return FindText (&fr)
    }
    The results are the same as I posted before.

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    Did you add the 'returns' to the msg handlers (instead of allowing them to call the default processing after you have already processed the msg)? The MSDN examples return 0 for the 'find' msg.


    In general;

    Call ZeroMemory() or similar on any structs before passing them to WIN32 functions.

    Initialise all your variables (ie what is in iOffset and is it valid/what you expect?).

    Return the correct code and do not call the default msg handler on any msg you process (ie WM_COMMAND returns 0 for successful processing).
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    87
    I tried changing MainWndProc to the following and still the same results.

    Code:
    LRESULT CALLBACK MainWndProc(hWnd, message, wParam, lParam)
    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_SETFOCUS:
               SetFocus (hEditWnd);
               return 0;
    
    	case WM_COMMAND:	   /* message: command from application menu */
              switch (LOWORD(wParam)) {
                case IDM_SEARCHFIND:
                    SendMessage (hEditWnd, EM_GETSEL, 0, (LPARAM) &iOffset) ;
                    hDlgFind = FindDialog (hWnd) ;
                    return 0;
    
                case IDM_SEARCHNEXT:
                    SendMessage (hEditWnd, EM_GETSEL, 0, (LPARAM) &iOffset) ;
    
                    if (PopFindValidFind())
                        PopFindNextText (hEditWnd, &iOffset) ;
                    else
                        hDlgFind = FindDialog (hWnd) ;
                        return 0;
    
                case IDM_SEARCHPREV:
                    /* locate previous search string in edit control */
                    return 0;
    
                case IDM_ABOUT: {
    		    lpProcAbout = MakeProcInstance(About, hInst);
    
    		    DialogBox(hInst,		 /* current instance	     */
    		        "AboutBox",		 /* resource to use	     */
    		        hWnd,			 /* parent handle	     */
    		        lpProcAbout);		 /* About() instance address */
    
    		    FreeProcInstance(lpProcAbout);
                    }
    		    return 0;
    
               }
               break;
    
    	case WM_DESTROY:		  /* message: window being destroyed */
    	    PostQuitMessage(0);
    	    return 0;
    
    	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 */
                        return 0;
                    }
                    else if (lpfr->Flags & FR_FINDNEXT)
                        if (!PopFindFindText (hEditWnd, &iOffset, lpfr))
                            MessageBox (hWnd, "String Not Found", "Editfile", MB_OK);
                    return 0;
                }
                break;
    
        }
        return (DefWindowProc(hWnd, message, wParam, lParam));
    }

    I also added
    Code:
    memset(&fr, 0, sizeof(fr));
    to the FINDREPLACE structure.....Still the same results.

  7. #7
    Registered User
    Join Date
    Jun 2009
    Posts
    87
    Ok, I figured out what's wrong....I forgot to add "ES_NOHIDESEL" to the Style for the Edit control's (hEditWnd) CreateWindow function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21