help finding a bug

This is a discussion on help finding a bug within the Windows Programming forums, part of the Platform Specific Boards category; Code: #include <windows.h> #include "resource.h" char* revstr(char*); BOOL CALLBACK DlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { switch(message) { ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    204

    help finding a bug

    Code:
    #include <windows.h>
    #include "resource.h"
    
    char* revstr(char*);
    
    BOOL CALLBACK DlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch(message)
        {
            case WM_INITDIALOG:
                return TRUE;
            break;
    
            case WM_COMMAND:
                switch(LOWORD(wParam))
                {
                    case ID_INVERT:
                    {
                        int length = GetWindowTextLength(GetDlgItem(hDlg, ID_TXT_NORMAL));
                        if(length > 0){
                            char* text;
                            char* reversed;
    
                            text = (char*)GlobalAlloc(GPTR, length + 1);
                            GetDlgItemText(hDlg, ID_TXT_NORMAL, text, length + 1);
    
                            reversed = revstr(text);
    
                            SetDlgItemText(hDlg, ID_TXT_NORMAL, reversed);
                            SendDlgItemMessage(hDlg, ID_TXT_NORMAL, WM_SETFOCUS, 0, 0);
                            SendDlgItemMessage(hDlg, ID_TXT_NORMAL, EM_SETSEL, 0, -1);
                            GlobalFree((HANDLE)text);
                        }
    
                        else
                            MessageBox(hDlg, "Digite alguma coisa...", "Aviso", MB_OK | MB_ICONWARNING);
                    }
                    return TRUE;
    
                    case ID_COPY:
                    {
                        int length = GetWindowTextLength(GetDlgItem(hDlg, ID_TXT_NORMAL));
                        if(length > 0)
                            SendDlgItemMessage(hDlg, ID_TXT_NORMAL, WM_CUT, 0, 0);
    
                        else
                            MessageBox(hDlg, "Não tem nada para ser copiado...", "Aviso", MB_OK | MB_ICONWARNING);
                    }
                    return TRUE;
    
                    case ID_PASTE:
                        SendDlgItemMessage(hDlg, ID_TXT_INVERTED, WM_PASTE, 0, 0);
                    return TRUE;
    
                    case ID_TRANSLATE:
                    {
                        int length = GetWindowTextLength(GetDlgItem(hDlg, ID_TXT_INVERTED));
                        if(length > 0){
                            char* text;
                            char* reversed;
    
                            reversed = (char*)GlobalAlloc(GPTR, length + 1);
                            GetDlgItemText(hDlg, ID_TXT_INVERTED, reversed, length + 1);
    
                            text = revstr(reversed);
                            SetDlgItemText(hDlg, ID_TXT_INVERTED, text);
                            GlobalFree((HANDLE)reversed);
                        }
    
                        else
                            MessageBox(hDlg, "Não há nada para traduzir...", "Aviso", MB_OK | MB_ICONWARNING);
                    }
                    return TRUE;
                }
            break;
    
            case WM_CLOSE:
                EndDialog(hDlg, 0);
            return TRUE;
    
            default:
                return FALSE;
        }
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                       PSTR szCmdLine, int iCmdShow)
    {
        return (int)DialogBox(hInstance, MAKEINTRESOURCE(ID_MAIN), NULL, DlgProc);
    }
    
    char* revstr(char* a)
    {
      size_t temp, x, y = strlen(a) - 1;
    
      for ( x = 0; x < y; x++, y-- ){
        temp = a[x]; 
        a[x] = a[y]; 
        a[y] = temp;
      }
    
      return a;
    }
    If I type something in ID_TXT_NORMAL, click the ID_COPY button and paste what's on the clipboard to whatever program, when I click on the dialog box again I am not able to input text on ID_TXT_NORMAL without using that tab key on the keyboard. I noticed that this happens because the focus is still on the button I last clicked. Can you guys help me find the problem? Thanks.

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Try using the SetFocus function to transfer focus back to the text control. Also, consider what will happen when a zero length string is passed to revstr.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gaks bug?
    By Yarin in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 08-31-2008, 03:47 PM
  2. Debugging a rare / unreproducible bug..
    By g4j31a5 in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 08-05-2008, 01:56 PM
  3. ATL bug of CComPtr?
    By George2 in forum Windows Programming
    Replies: 6
    Last Post: 04-07-2008, 08:52 AM
  4. Need help finding bug
    By Guti14 in forum C++ Programming
    Replies: 2
    Last Post: 10-05-2003, 03:21 AM
  5. MFC :: Finding Child Window of a CWnd* Object?
    By SyntaxBubble in forum Windows Programming
    Replies: 2
    Last Post: 09-06-2003, 10:06 AM

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