Thread: DialogBox won't close with the X

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    118

    DialogBox won't close with the X

    Hello, I'm new to the Windows API and I'm trying to make a dialog based applicaion, so I added a dialog resource which has a frame with the X button on top, the only problem is that when I press the X in the frame it won't close, or if I want to click on the frame to drag the dialog around, it just wont work.

    here is my code:
    Code:
    #include <windows.h>
    #include "resource.h"
    
    INT_PTR CALLBACK DialogProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
    
    INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int cmdShow)
    {
    	MSG Msg;
    	HWND hWnd;
    
    	hWnd = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_DIALOG), NULL, (DLGPROC)DialogProc);
    
    	ShowWindow(hWnd, cmdShow);
    	UpdateWindow(hWnd);
    
    	while(GetMessage(&Msg, NULL, 0, 0))
            {
                TranslateMessage(&Msg);
                DispatchMessage(&Msg);
            }
    
        return 0;
    }
    
    INT_PTR CALLBACK DialogProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
    	switch (uMsg)
    	{
    	case WM_CLOSE:
    		PostQuitMessage(0);
    		break;
    	case WM_COMMAND:
    		switch (LOWORD(wParam))
    		{
    		case IDCANCEL:
    			PostQuitMessage(0);
    			break;
    		default:
    			return DefWindowProc(hWnd, uMsg, wParam, lParam);
    		}
    		break;
    	default:
    		return DefWindowProc(hWnd, uMsg, wParam, lParam);
    	}
    
    	return 0;
    }
    Why drink and drive when you can smoke and fly?

  2. #2
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    Code:
    BOOL EndDialog(
    
        HWND hDlg,	// handle to dialog box
        int nResult 	// value to return
       );
    Code:
             case IDCANCEL:
    		EndDialog(hwnd, IDCANCEL);
    edit
    here is what i do from winproc. how messages used.

    Code:
    		break;
    		case WM_CLOSE:
    			DestroyWindow(hwnd);
    		break;
    		case WM_DESTROY:
    			PostQuitMessage(0);
    		break;
    Last edited by kryptkat; 10-26-2006 at 03:04 PM.

  3. #3
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Although the call to DestroyWindow in the WM_CLOSE handler is redundant since that's what the default system handler does anyway.

    Also, CreateDialog creates a modeless dialog which should be destroyed with DestroyWindow; EndDialog is for the destruction of modal dialogs. Don't use DefWindowProc for default message handling for dialogs, return zero instead.
    Code:
    INT_PTR CALLBACK DialogProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
    	switch (uMsg)
    	{
    	case WM_COMMAND:
    		switch (LOWORD(wParam))
    		{
    		case IDCANCEL:
    			DestroyWindow(hWnd);
    			break;
    		}
    		break;
           case WM_DESTROY:
                   PostQuitMessage(0);
                   break;
    	default:
    		return 0
    	}
    
    	return 0;
    See also Dialog Boxes.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    118

    Smile

    Thanks.
    Why drink and drive when you can smoke and fly?

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Read:
    http://msdn.microsoft.com/library/de.../dialogbox.asp for DialogBox
    http://msdn.microsoft.com/library/de...dialogproc.asp for DialogProc

    On a side note, Ken forgot to convert the break; to return 1; in the above procedure. And the default branch of the case is redundant.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I don't understand the point of dialogs. I prefer windows.

    Can anyone explain?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  7. #7
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    The attractive point of dialogs is that they provide a quick way to arrange and create a GUI (Graphic User Interface) and even some default processing, cutting down on the amount of code you must write.
    ....
    One thing to remember is that dialogs are just windows. The difference between a dialog and a "normal" window is that the system does some additional default processing for dialogs, such as creating and initialising controls, and handling tab order. Nearly all APIs that are applicable to "normal" windows will work just as well on dialogs, and vice versa!
    quote of winforge

    http://www.winprog.org/

    Model and Modeless Dialog Boxes are a great way to make toolbars and file load/save requestor. Getting user info or data or a message requestor for easy gui communication. Gui controls can be a good way to control data or info.

    You like what you like. It is just another tool in the box.

    edit side note
    oh good. i am not the only one who writes redundant code or code that does the same thing as already implicitly covered by the system such as return 0 in main() which i do not do or did not did untill it was suggested that it was a good idea.
    Last edited by kryptkat; 10-27-2006 at 10:48 AM.

  8. #8
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    So it's useful
    "The Internet treats censorship as damage and routes around it." - John Gilmore

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Close an HTTPListenerResponse.OutputStreram
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 04-23-2008, 11:00 PM
  2. open() and close()
    By SoFarAway in forum C Programming
    Replies: 3
    Last Post: 04-08-2005, 01:16 PM
  3. XWindows- Close window event?
    By Exile in forum Linux Programming
    Replies: 8
    Last Post: 01-09-2005, 10:39 PM
  4. Proper way to end a dialogbox.
    By spoon_ in forum Windows Programming
    Replies: 2
    Last Post: 01-06-2003, 05:47 PM
  5. Ghost in the CD Drive
    By Natase in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 10-12-2001, 05:38 PM