Pressing a button works sometimes

This is a discussion on Pressing a button works sometimes within the Windows Programming forums, part of the Platform Specific Boards category; I have a simple dialog program with a couple of buttons on it and most of the times the buttons ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    76

    Unhappy Pressing a button works sometimes

    I have a simple dialog program with a couple of buttons on it and most of the times the buttons on it work, but not always.
    If i press on the exit button i always can see the button going down and when i release the mouse button it commes up again. So graphicaly the button always works as expected. The problem is that the program doesnt always exit. If it doesnt exit it doesnt matter how often i click on it. But when i move the window only a pixel it works again....... Has anybody got an idea what i can do to find the cause of this problem? This problem doesn't happen very often, but the problem does happen. I have tried to "force" this to happen but it only happens when i least expect it

    To avoid confusion... im talking about my own custom exit button... not that little x at the top right corner!!

    Im using devcpp with winxp.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Post your dialog procedure, or cut down version that demonstrates the problem.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    76
    Here is my dialog procedure, but i dont know if this has the problem.


    in winmain i also got
    InitCommonControls();
    LoadLibrary( "Riched20.dll");

    If there are some strange contructions in the code its because its my first program


    The problem just happened again, and i noticed that the button works if i move the dialog a couple of pixel, but the exit buttons still doesnt work if i maximize with the button on the right top

    Code:
    BOOL CALLBACK MainDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        HWND hwndObj;
        HMENU hwndMenu;
        LVCOLUMN lvc;
        NM_LISTVIEW* lvMsg;
        int columnNr;
        switch(msg)
        {
            case WM_INITDIALOG:
                mainWinHandle = hwnd;
                listviewHandle = GetDlgItem(hwnd,IDCLISTV);
                hwndObj = GetDlgItem(hwnd,IDBADD);
                      SetFocus (hwndObj);
                hwndMenu = GetMenu(hwnd);
                if (prefs.onTop)
                {
                    SetWindowPos(hwnd,HWND_TOPMOST,1,1,1,1,SWP_NOMOVE|SWP_NOSIZE );
                    CheckMenuItem(hwndMenu,IDMONTOP,MF_BYCOMMAND|MF_CHECKED);
                }
                else
                {
                    SetWindowPos(hwnd,HWND_NOTOPMOST,1,1,1,1,SWP_NOMOVE|SWP_NOSIZE );
                    CheckMenuItem(hwndMenu,IDMONTOP,MF_BYCOMMAND|MF_UNCHECKED);
                }
                char dllFile [FILENAMESIZE];
                strcpy(dllFile,mainDir);
                strcat(dllFile,"test.dll");
                lib = NULL;
                if(FileExists(dllFile))
                    lib = LoadLibrary(dllFile);
                if(lib != NULL)
                {
                    _dllFunc = (DLLFUNC) GetProcAddress(lib,TEXT_DLLFUNC);
                }
                if(lib == NULL || _dllFunc == NULL )
                {
                    MessageBox(hwnd, "test.dll is missing or invallid", dllFile,MB_OK | MB_ICONEXCLAMATION);
                    exit(-1);
                }
                strcpy(dllFile,mainDir);
                strcat(dllFile,"test2.dll");
                lib2 = NULL;
                if(FileExists(dllFile))
                    lib2 = LoadLibrary(dllFile);
                if (lib2 == NULL)
                {
                        MessageBox(hwnd, "test2.dll is missing", dllFile,MB_OK | MB_ICONEXCLAMATION);
                }
                else
                {
                    _dll2Func1   = (DLL2FUNC1)   GetProcAddress(lib2, TEXT_DLL2FUNC1);
                    _dll2Func2   = (DLL2FUNC2)   GetProcAddress(lib2, TEXT_DLL2FUNC2);
                    _dll2Func3   = (DLL2FUNC3)   GetProcAddress(lib2, TEXT_DLL2FUNC3);
                    if(_dll2Func1 == NULL || _dll2Func2 == NULL || _dll2Func3 == NULL)
                    {
                        MessageBox(hwnd, "dll2 error", "WARNING",MB_OK | MB_ICONEXCLAMATION);
                        lib2 = NULL;
                    }
                }
                SendDlgItemMessage(hwnd, IDCLISTB,LB_SETHORIZONTALEXTENT,(WPARAM) 700,0);
                PlaceControlsMainDlg (hwnd);
                hwndObj = GetDlgItem(hwnd,IDCLISTV);
                SendMessage(hwndObj, LVM_SETEXTENDEDLISTVIEWSTYLE,(WPARAM) (DWORD)LVS_EX_HEADERDRAGDROP | LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT ,LVS_EX_HEADERDRAGDROP  | LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT );
                ZeroMemory(&lvc,sizeof(lvc));
                lvc.mask=LVCF_TEXT | LVCF_WIDTH ;
                lvc.cx=100;
                lvc.pszText="C1";
                SendMessage(hwndObj,LVM_INSERTCOLUMN,1,(LPARAM)&lvc);
                lvc.cx=70;
                lvc.pszText="C2";
                SendMessage(hwndObj,LVM_INSERTCOLUMN,2,(LPARAM)&lvc);
                lvc.cx=50;
                lvc.pszText="C3";
                SendMessage(hwndObj,LVM_INSERTCOLUMN,3,(LPARAM)&lvc);
                lvc.cx=50;
                lvc.pszText="C4";
                SendMessage(hwndObj,LVM_INSERTCOLUMN,4,(LPARAM)&lvc);
                lvc.cx=65;
                lvc.pszText="C5";
                SendMessage(hwndObj,LVM_INSERTCOLUMN,5,(LPARAM)&lvc);
                lvc.cx=80;
                lvc.pszText="C6";
                SendMessage(hwndObj,LVM_INSERTCOLUMN,6,(LPARAM)&lvc);
                lvc.cx=70;
                lvc.pszText="C7";
                SendMessage(hwndObj,LVM_INSERTCOLUMN,7,(LPARAM)&lvc);
                lvc.cx=70;
                lvc.pszText="C8";
                SendMessage(hwndObj,LVM_INSERTCOLUMN,8,(LPARAM)&lvc);
                WriteInitFile();
                return true;
            break;
            case WM_CLOSE:
                PostQuitMessage(WM_QUIT);
                return true;
            break;
            case WM_DROPFILES:
                ProcessDragAndDrop ( (HDROP*) wParam, hwnd);
                DragFinish((HDROP) wParam);
                return true;
            break;
            case WM_SIZE:
                PlaceControlsMainDlg (hwnd);
                return true;
            break;
            case WM_MOVE:
                PlaceControlsMainDlg (hwnd);
                return true;
            break;
            case WM_COMMAND:
                switch(LOWORD(wParam))
                {
                    case IDBADD:
                        AddItemToListBox(hwnd);
                        return true;
                    break;
                    case IDBDELETE:
                        DeleteItemFromListB();
                        return true;
                    break;
                    case IDBEXIT:
                        PostQuitMessage(WM_QUIT);
                        return true;
                    break;
                    case IDBCLEARLIST:
                        SendDlgItemMessage(hwnd, IDCLISTB,LB_RESETCONTENT,0,0);
                        return true;
                    break;
                    case IDCLISTB:
                        return true;
                    break;
                    case IDMEXIT:
                        PostQuitMessage(WM_QUIT);
                        return true;
                    break;
                    case IDMPREF:
                        DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDDPREF), hwnd, PrefDlgProc);
                        return true;
                    break;
                    case IDMONTOP:
                        hwndMenu = GetMenu(hwnd);
                        prefs.onTop = !prefs.onTop;
                        if (prefs.onTop)
                        {
                            SetWindowPos(hwnd,HWND_TOPMOST,1,1,1,1,SWP_NOMOVE|SWP_NOSIZE );
                            CheckMenuItem(hwndMenu,IDMONTOP,MF_BYCOMMAND|MF_CHECKED);
                        }
                        else
                        {
                            SetWindowPos(hwnd,HWND_NOTOPMOST,1,1,1,1,SWP_NOMOVE|SWP_NOSIZE );
                            CheckMenuItem(hwndMenu,IDMONTOP,MF_BYCOMMAND|MF_UNCHECKED);
                        }
                        WriteInitFile();
                        return true;
                    break;
                    case IDMABOUT:
                        DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDDABOUT), hwnd, AboutDlgProc);
                        return true;
                    break;
                    default:
                    return false;
                }
            break;
            default:
            return false;
        }
        return false;
    }
    Last edited by johny145; 05-12-2005 at 08:24 PM.

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,854
    Use DestroyWindow() in the WM_CLOSE and SendMessage() a WM_CLOSE from the button. (or just call DestroyWindow() if you don't mind repeating the code)

    Then let the default handler do the work.

    PostQuitMessage() is called (by Default) from a WM_DESTROY msg.
    "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

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    76
    It doesnt work exactly as i expected

    i tried

    Code:
    case WM_CLOSE:
    	DestroyWindow(mainWin);	
    	return true;
    break;
    this only closes the window
    then i tried to "Then let the default handler do the work."
    i experimented some with DefDlgProc and using the folowing code

    Code:
    case WM_CLOSE:
    	DestroyWindow(mainWin);	
    	return false;
    break;
    but this didnt work either....

    could somebody please explain me what i am doing wrong and maybe even why postquitmessage almost always works, and sometimes fails.... is this a bug in my program or something??
    Last edited by johny145; 05-15-2005 at 09:08 AM.

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    When you want the dialog to exit call EndDialog:
    Quote Originally Posted by MSDN EndDialog
    The EndDialog function destroys a modal dialog box, causing the system to end any processing for the dialog box.

    Dialog boxes created by the DialogBox, DialogBoxParam, DialogBoxIndirect, and DialogBoxIndirectParam functions must be destroyed using the EndDialog function. An application calls EndDialog from within the dialog box procedure; the function must not be used for any other purpose.
    Quote Originally Posted by MSDN DialogBox
    When the dialog box procedure calls the EndDialog function, DialogBox destroys the dialog box, ends the message loop, enables the owner window (if previously enabled), and returns the nResult parameter specified by the dialog box procedure when it called EndDialog.
    Do not call PostQuitMessage.

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    76
    Thanks for you suggestion, but im not using DialogBox, DialogBoxParam, DialogBoxIndirect or DialogBoxIndirectParam. My startpost was not accurate when i said "I have a simple dialog". Actualy im creating it with CreateDialog which in turn uses CreateWindowEx.

  8. #8
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Could you post the PlaceControlsMainDlg function? If you are using the MoveWindow function in the function the bRepaint argument should be TRUE.

  9. #9
    Registered User
    Join Date
    Mar 2005
    Posts
    76
    thx for the quick replies
    i checked it and i believe the bbrepaint is always true
    Code:
    void PlaceControlsMainDlg(HWND hwnd)
    {
        RECT buttonRect;
        HWND hwndBut;
        WINDOWINFO mainWinInf;
        mainWinInf.cbSize = sizeof(WINDOWINFO);
        GetWindowInfo (hwnd, &mainWinInf);
        int winWidth = mainWinInf.rcWindow.right-mainWinInf.rcWindow.left;
        int winHeight = mainWinInf.rcWindow.bottom-mainWinInf.rcWindow.top;
        MoveWindow(hwnd,mainWinInf.rcWindow.left,mainWinInf.rcWindow.top,MAX(winWidth,300),MAX(winHeight,180),TRUE); // set minimal window size
        GetWindowInfo (hwnd, &mainWinInf);
        winWidth = mainWinInf.rcClient.right - mainWinInf.rcClient.left;
        winHeight = mainWinInf.rcClient.bottom - mainWinInf.rcClient.top;
        hwndBut = GetDlgItem(hwnd,IDBADD);
        GetClientRect(hwndBut,&buttonRect);
        MoveWindow(hwndBut,winWidth-100, 10 ,buttonRect.right,buttonRect.bottom,TRUE);
        hwndBut = GetDlgItem(hwnd,IDBDELETE);
        GetClientRect(hwndBut,&buttonRect);
        MoveWindow(hwndBut, winWidth-100, 45 ,buttonRect.right,buttonRect.bottom,TRUE);
        hwndBut = GetDlgItem(hwnd,IDBCLEARLIST);
        GetClientRect(hwndBut,&buttonRect);
        MoveWindow(hwndBut,winWidth-100, 80 ,buttonRect.right,buttonRect.bottom,TRUE);
        hwndBut = GetDlgItem(hwnd,IDBEXIT);
        GetClientRect(hwndBut,&buttonRect);
        MoveWindow(hwndBut,25, winHeight-30 ,buttonRect.right,buttonRect.bottom,TRUE);
        hwndBut = GetDlgItem(hwnd,IDBCONVERT);
        GetClientRect(hwndBut,&buttonRect);
        MoveWindow(hwndBut,winWidth-183, winHeight-30 ,buttonRect.right,buttonRect.bottom,TRUE);
        hwndBut = GetDlgItem(hwnd,IDCLISTV);
        GetClientRect(hwndBut,&buttonRect);
        MoveWindow(hwndBut,25,10,winWidth-140,winHeight-60,TRUE);	
        InvalidateRgn(hwnd,NULL,TRUE);
        UpdateWindow(hwnd);
    }
    nobody has asked for it(yet), but just to be shure my code is ok.... here is my message loop:

    Code:
        while(GetMessage(&msg,hwndMain,0,0))
        {
            if ( TranslateAccelerator(hwndMain, hAccelTable, (LPMSG) &msg ) || IsDialogMessage (hwndMain,(LPMSG) &msg))
            {
    			/* SKIP */
            }
            else
            {
                TranslateMessage(&msg);
                DispatchMessage (&msg);
            }
        }
    Last edited by johny145; 05-15-2005 at 11:16 AM.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    I don't know why I didn't spot it but anonytmouse is right use EndDialog. CreateDialog is a wrapper around CreateDialogParam.

  11. #11
    Registered User
    Join Date
    Mar 2005
    Posts
    76
    Quote Originally Posted by Quantum1024
    I don't know why I didn't spot it but anonytmouse is right use EndDialog. CreateDialog is a wrapper around CreateDialogParam.

    this is what msdn says:

    "After CreateDialog returns, the application displays the dialog box (if it is not already displayed) by using the ShowWindow function. The application destroys the dialog box by using the DestroyWindow function."

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    You're right, sorry for the confusion. I've always ended dialog boxes with EndDialog, seams I've been doing it wrong

  13. #13
    Registered User
    Join Date
    Mar 2005
    Posts
    76
    talking about bad habits.....
    A couple of days ago i noticed that my old code contained the following construcion more then a dozen times.
    Code:
    strcpy (dirName,strcat(dirName,"\\"));


    This is probabely my biggest mistake ever

    When i was thinking about this and wondering why on earth i had used this construction over and over again, i remebered that i used it once, and since it worked perfect i kept on using/copying it. I guess thats how most bad programming habits sneak in.....

  14. #14
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
      while(GetMessage(&msg,hwndMain,0,0))
    I doubt this is the problem but you should use NULL as the second argument ot GetMessage to process all messages. Also try returning false from WM_MOVE and WM_SIZE. Other than that, I'm all out of ideas.

  15. #15
    Registered User
    Join Date
    Mar 2005
    Posts
    76
    Quote Originally Posted by anonytmouse
    Code:
      while(GetMessage(&msg,hwndMain,0,0))
    I doubt this is the problem but you should use NULL as the second argument ot GetMessage to process all messages. Also try returning false from WM_MOVE and WM_SIZE. Other than that, I'm all out of ideas.
    I didnt expect it, but somehow the problem has vanished. Maybe you did give the right solution, or maybe i was just lucky and i fixed the problem without knowing this. Now im just hoping it doesnt return .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. allegro, small problem
    By IM back! in forum C++ Programming
    Replies: 8
    Last Post: 11-20-2008, 01:29 PM
  2. elliptical button
    By geek@02 in forum Windows Programming
    Replies: 0
    Last Post: 11-21-2006, 01:15 AM
  3. Remove dialog button border
    By RedZone in forum Windows Programming
    Replies: 4
    Last Post: 08-21-2006, 01:20 PM
  4. Button Displaying Image
    By mrafcho001 in forum Windows Programming
    Replies: 4
    Last Post: 12-03-2005, 01:14 PM
  5. Virtually pressing a button
    By Queatrix in forum Windows Programming
    Replies: 4
    Last Post: 08-19-2005, 10:19 AM

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