[RESOLVED] Dialogue box button not working
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Thread: [RESOLVED] Dialogue box button not working

  1. #1
    Join Date
    Jan 2013
    Posts
    19

    [RESOLVED] Dialogue box button not working

    Dialogue box in the program is displayed but button will not respond.

    I created dialogue box using resource wizard,
    code in Resource.rc for dialogue box is generated as below
    Code:
    IDD_DIALOG1 DIALOGEX 0, 0, 131, 69
    STYLE DS_SETFONT | DS_FIXEDSYS | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
    EXSTYLE WS_EX_APPWINDOW
    CAPTION "Test dialogue"
    FONT 8, "MS Shell Dlg", 400, 0, 0x1
    BEGIN
        DEFPUSHBUTTON   "That is Good",IDOK,30,35,66,25
        LTEXT           "Test is successful .",IDC_STATIC,33,15,75,11
    END
    Dialogue box is associated with a menu item by the code

    Code:
    case IDM_ABOUT:
    		CreateDialog(hInst, MAKEINTRESOURCE(IDD_DIALOG1),hwindp, Dialog1Proc);
    		return 0;
    		break;
    message processor for the dialogue box is as below

    Code:
    INT_PTR CALLBACK Dialog1Proc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	
    switch(message)
    	{	case WM_INITDIALOG:
    			return (INT_PTR)TRUE;
    			return a;
    			break;
    
    		case WM_COMMAND:
    		if (LOWORD(wParam) == IDOK)
    		{
    			EndDialog(hDlg, LOWORD(wParam));
    			return (INT_PTR)TRUE;
    		}
    		break;
    
    //		return (INT_PTR) TRUE;
    	}
    
    
    	return (INT_PTR) FALSE;
    
    }
    Adding a case statement IDOK did not help

    Code:
    case IDOK:
    		{
    			EndDialog(hDlg, LOWORD(wParam));
    			return (INT_PTR)TRUE;
    		}
    		break;
    I really need some help at this point, i will be glad if i can get help now.
    Attached Files Attached Files
    Last edited by pavankn18; February 4th, 2013 at 08:52 AM.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,431

    Re: Dialogue box button not working

    Quote Originally Posted by pavankn18 View Post
    Adding a case statement IDOK did not help.
    Instead of adding code and hoping it works, did you debug your code? Did you set a breakpoint in the dialog procedure to see what is happening?

    You're supposed to first diagnose the problem, see where it breaks down, read the documentation, and then add code to correct the problem. When it comes to Windows programming, you don't just add code just to see if it fixes something -- you need to know the exact reason why something fails first.

    A Win32 program requires all these moving parts to be correct, resource file, window procedure, resource constants, etc. Just showing us the dialog procedure doesn't prove whether you have all the pieces working correctly.

    That's why I mentioned to you in another thread that you take a full, working example of a simple Win32 dialog, where you don't need to change any code, compile it, run it. Once you do that, then and only then can you go any further. Did you do that, or are you trying to write the program from scratch? There are thousands of simple, working, full source samples of a Win32 dialog, and that is where you should be starting from.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; February 3rd, 2013 at 08:55 AM.

  3. #3
    Join Date
    Jan 2013
    Posts
    19

    Re: Dialogue box button not working

    Yes i used Breakpoints at 2 locations
    1. at switch statement
    INT_PTR CALLBACK Dialog1Proc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
    {

    switch(message)
    2. at WM_COMMAND of Dialog1Proc

    case WM_COMMAND:
    if (LOWORD(wParam) == IDOK)
    {
    EndDialog(hDlg, LOWORD(wParam));
    return (INT_PTR)TRUE;
    }
    break;
    switch statement is executed and WM_COMMAND is never reached (verified by removing first breakpoint and ">continue")
    so i tried that case statement.

    I see no problem with windowsproc as except for the dialog box button everything is working fine, include other menu items, There is no problem with displaying dialogue box also,

    BEGIN
    DEFPUSHBUTTON "That is Good",IDOK,30,35,66,25
    END
    That was the part of the code associated with the button in resource file, so i showed the complete code generated for dialogue box;
    I did run a program from msdn with no problem, i did not face this problem when adding dialogue box to template generated vs, But i am facing this problem when i try to write complete program starting from a empty project, But now if compare with other 2 programs i see no difference, Now i cant even guess were i might be going wrong.

    So, trying so many things, now somehow i want to know the problem, so i am looking for help.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,423

    Re: Dialogue box button not working

    Quote Originally Posted by pavankn18 View Post
    I did run a program from msdn with no problem, i did not face this problem when adding dialogue box to template generated vs, But i am facing this problem when i try to write complete program starting from a empty project...
    Why did you start from a empty project? Why not from a Win32 one?
    Besides, your problem is more related to the C++ and WinAPI Forum (which "Discuss Windows API related issues using C++ (and Visual C++). This is a non-MFC forum") rather than Visual C++ Programming one ("Ask questions about Windows programming with Visual C++ and help others by answering their questions.")
    Victor Nijegorodov

  5. #5
    Join Date
    Jan 2013
    Posts
    19

    Re: Dialogue box button not working

    i'm not using mfc, i have not touched mfc yet, i am a beginner, i don't know why, if i do the same things with win32 template everything works smoothly, for now i badly want to know what is really wrong.

  6. #6
    Join Date
    Jan 2013
    Posts
    19

    Re: Dialogue box button not working

    To simplify the question : any guess why a button in dialog box may not create "WM_COMMAND" message upon clicking it?

  7. #7
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,464

    Re: Dialogue box button not working

    Programmers don't guess!! They make insiteful observations based upon the information available. You need to understand and debug your code.

    As Paul said

    You're supposed to first diagnose the problem, see where it breaks down, read the documentation, and then add code to correct the problem. When it comes to Windows programming, you don't just add code just to see if it fixes something -- you need to know the exact reason why something fails first.
    You've only posted a part of the code with which you are having problems. But as all parts of a WIN32 program work together with the OS calling your routines and your routines calling Windows routines, the problem could be anywhere.

    You need to start with a known working example of a WIN32 program that uses a dialog box. Compile that and make sure it works as expected. Then add/change as required and after each step make sure the program still works. If it doesn't you then know that the problem lies with the last change.

    You must understand how a WIN32 program is expected to work. This has to be learnt - it can't just be found by trying some ad-hoc code and hoping for the best.

    I suggest you read, digest and understand

    Programming Windows 5th Edition Book/CD Package: The definitive guide to the Win32 API (Microsoft Programming Series) Charles Petzold
    http://www.amazon.co.uk/Programming-...9984984&sr=1-4

    Win32 Programming (Advanced Windows) Rector & Newcomer
    http://www.amazon.co.uk/Win32-Progra...9985057&sr=1-1

  8. #8
    Join Date
    Jan 2013
    Posts
    19

    Re: Dialogue box button not working

    When a dialog is created only dialog window is active so only all msgs now received by dialog proc,
    1. soon a dialog window is created case "WM_INITDIALOG" is entered.
    2. even if i click the only button in the dialog window now, n "WM_COMMAND" is generated but it is expected.
    3. I checked the ID associated with the button, it is ok.
    4. checking properties in for the button i confirm the button is not disabled.
    5. But if i do the same starting from win32 template, everything works fine
    6. Comparing code generated with that of working porgram, i see no difference.

    I tried to tackle the problem with strategic manner but i failed. after doing everything above i did not know what to do next. So i came here searching for help.
    and now i will attach all the files in the project. But i do not really think any code outside what i have posted in the original problem will have much relevance to the problem.

    Thank you for all your support.

  9. #9
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,990

    Re: Dialogue box button not working

    Quote Originally Posted by pavankn18 View Post
    To simplify the question : any guess why a button in dialog box may not create "WM_COMMAND" message upon clicking it?
    There is no problem with your dialog template, and your dialog procedure contains one problem that has nothing to do with your current issue. But for sure there is a problem with your message pump. As I can see it, the most probable reason is that your code pumps messages for your frame window only, and other windows' messages remain not dispatched. This never happens when you let wizard generate the message pump (this or that way, specific to the framework in use)
    Best regards,
    Igor

  10. #10
    Join Date
    Jan 2013
    Posts
    19

    Re: Dialogue box button not working

    Thanks sir,

    Though i could not solve the problem, i can move on now because that reason is convincing but i am still a little sad that i failed to write such simple application from an empty file.

    I hope one day in future i can look back to fix it
    For now i am feeling a little comfortable to move on atleast, thank you again

  11. #11
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,990

    Re: Dialogue box button not working

    Quote Originally Posted by pavankn18 View Post
    I hope one day in future i can look back to fix it
    Why not now? Do you know what the message pump is? What it looks like? And where to look it for? If all the answers are 'yes', why don't you go and slay the dragon right now?
    Best regards,
    Igor

  12. #12
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,990

    Re: Dialogue box button not working

    Okay, here comes the confirmation from Project1.cpp.txt:
    Code:
    	HWND hWnd;
    	hWnd = CreateWindowEx ( 0,                              // Optional window styles.
            CLASS_NAME,                     // Window class
    
            "Starter App",                 // Window text
            WS_OVERLAPPEDWINDOW,            // Window style
    
            // Size and position
            CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
    
            NULL,       // Parent window    
            NULL,       // Menu
            hInstance,  // Instance handle
            NULL        // Additional application data
            );
    
    //	printf("Window created");
    	if(!hWnd)
    	{return 0;}
    //	printf("window verified");
    	ShowWindow(hWnd, nCmdShow);
    
    	hwindp = hWnd;
    
    	MSG Msg;
    	while(GetMessage(&Msg, hWnd, 0, 0))
    	{
    		TranslateMessage(&Msg);
    		DispatchMessage(&Msg);
    	}
    
    
    		return (int)Msg.wParam;
    A correct message pump should look like this:
    Code:
    	MSG Msg;
    	while(0 < GetMessage(&Msg, NULL, 0, 0))
    	{
    		TranslateMessage(&Msg);
    		DispatchMessage(&Msg);
    	}
    As for the problem in dialog procedure I mentioned. You never destroy dialog with EndDialog() in case the dialog is not modal, i.e. created with CreateDialog(). And you use DestroyWindow() in such case.
    Last edited by Igor Vartanov; February 4th, 2013 at 11:07 AM.
    Best regards,
    Igor

  13. #13
    Join Date
    Jan 2013
    Posts
    19

    Re: Dialogue box button not working

    Thanks a lot sir.igor.

    feeling better now.

  14. #14
    Join Date
    Apr 1999
    Posts
    27,431

    Re: Dialogue box button not working

    Quote Originally Posted by pavankn18 View Post
    Thanks sir,

    Though i could not solve the problem, i can move on now because that reason is convincing but i am still a little sad that i failed to write such simple application from an empty file.
    No. You never should be writing Win32 event driven applications from scratch! The reason why is that Win32 programming is not simple. That's why I stated you take a working example and change it until you break something. You never should learn Windows programming by writing a program out of thin air. Even professional programmers don't do this.

    http://www.winprog.org/tutorial/modeless_dialogs.html

    In that example, do you see the erroneous message pump that you wrote? No. You see the second parameter being NULL to GetMessage(), just as Igor pointed out. I can show you another thousand examples of dialog box code from different web sites and authors with the same correct message loop. So why is it your code has that bogus message loop? The only reason is that you didn't take the advice of getting working code and learning from it.

    Regards,

    Paul McKenzie

  15. #15
    Join Date
    Jan 2013
    Posts
    19

    Re: Dialogue box button not working

    I had working code with me, one from msdn and as saidfrom i had no problem when a start from win32 template. I wasn't looking at msg pump as dialogue box was receiving "WM_INITDIALOG". I never really ignored your advice, Thank you for your support.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center