Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Dialog not closing on clicking 'X' button?

159 views
Skip to first unread message

deostroll

unread,
Dec 20, 2007, 11:51:00 PM12/20/07
to
I've created a simple c++ program. It just creates a plain dialog box
with a button and a static control in it. It even has the ordinary
SYSMENU buttons - minimize, maximize and close. But when I click on
close (the "X" button) nothing happens!

How to close this dialog box?

------ main.cpp ------
#include <windows.h>
#include "resource.h"

BOOL CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM
lParam)
{
switch(message)
{
case WM_INITDIALOG:

break;
case WM_COMMAND:

switch(LOWORD(wParam))
{
case ID_QUIT:
EndDialog(hwnd, ID_QUIT);
break;
}
break;
case WM_DESTROY:
EndDialog(hwnd, ID_QUIT);
break;
default:
return FALSE;
}

return TRUE;
}

int main()
{
DialogBox(
GetModuleHandle(NULL),
MAKEINTRESOURCE(IDD_DIALOG),
NULL,
WndProc);
return 0;
}

------- myresource.rc ----------
#include <windows.h>
#include "resource.h"

IDD_DIALOG DIALOG DISCARDABLE 100, 50, 400, 300
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU |
WS_MAXIMIZEBOX | WS_MINIMIZEBOX
CAPTION "A Dialog Program"
FONT 8, "MS Sans Serif"

BEGIN
CTEXT "This is a program\r\ncreated using dialog boxes
methodology.",
IDC_STATIC, 16, 18, 144, 33
PUSHBUTTON "&Quit", ID_QUIT, 200, 250, 50, 14
END

Christian ASTOR

unread,
Dec 21, 2007, 5:28:57 AM12/21/07
to
On 21 déc, 05:51, deostroll <deostr...@gmail.com> wrote:

>                 case ID_QUIT:
>                     EndDialog(hwnd, ID_QUIT);
>                     break;


case IDCANCEL:
//...

deostroll

unread,
Dec 23, 2007, 12:59:05 AM12/23/07
to

No, even this does not work...(Merry christmas & happy new year).

- deostroll

Scott Seligman

unread,
Dec 23, 2007, 1:57:03 AM12/23/07
to
deostroll <deos...@gmail.com> wrote:
>I've created a simple c++ program. It just creates a plain dialog box
>with a button and a static control in it. It even has the ordinary
>SYSMENU buttons - minimize, maximize and close. But when I click on
>close (the "X" button) nothing happens!
>
>How to close this dialog box?

You need to handle IDOK and IDCANCEL in your WM_COMMAND handler.

Additionally, it's not necessary to call EndDialog when you see
WM_DESTROY (though, afaik it's effectively just a no-op).

--
--------- Scott Seligman <scott at <firstname> and michelle dot net> ---------
It is the final proof of God's omnipotence that he need not exist in
order to save us.
-- Peter De Vries

Christian ASTOR

unread,
Dec 23, 2007, 2:22:47 AM12/23/07
to
deostroll wrote:

>>On 21 déc, 05:51, deostroll <deostr...@gmail.com> wrote:
>>
>>
>>> case ID_QUIT:
>>> EndDialog(hwnd, ID_QUIT);
>>> break;
>>
>>case IDCANCEL:
>>//...
>
>
> No, even this does not work...(Merry christmas & happy new year).


It must work if you haven't done any mistake.
It's the default behaviour of the Dialog Manager on WM_CLOSE
(and I've tested (XP SP2)...)

Grzegorz Wróbel

unread,
Dec 23, 2007, 5:39:25 AM12/23/07
to
deostroll wrote:
> case WM_DESTROY:
> EndDialog(hwnd, ID_QUIT);
> break;

This should be:

case WM_CLOSE:
EndDialog(hdial,FALSE);
break;

On WM_DESTROY window is already being destroyed and you might want only
to do some cleanup there (like freeing memory you have allocated on
WM_INITDIALOG).


--
Grzegorz Wróbel
http://www.4neurons.com/
677265676F727940346E6575726F6E732E636F6D

Norman Bullen

unread,
Dec 23, 2007, 9:17:05 AM12/23/07
to

Perhaps the OP failed to understand your excessively brief response.
He/she may have placed the case statement AFTER the break statement as
it appears in your message.

To the OP:
Clicking the "X" button causes a WM_CLOSE message to be sent to the
dialog. The default action of the dialog is to send a WM_COMMAND message
with wParam set to IDCANCEL to you dialog function. Christian intended
to suggest that your code should look like this:
switch (message) {
case WM_COMMAND:
switch (LOWORD(wParam)) {
case ID_QUIT:
case IDCANCEL:
EndDialog(...);

--
Norm

To reply, change domain to an adult feline.

deostroll

unread,
Dec 23, 2007, 5:39:59 PM12/23/07
to
On Dec 23, 7:17 pm, Norman Bullen <n...@BlackKittenAssociates.com>
wrote:

WM_CLOSE worked!!! But if I write it like this...

------dialog.h-------


BOOL CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM
lParam)
{
switch(message)
{

case WM_COMMAND:
switch(LOWORD(wParam))
{
case WM_CLOSE:


case ID_QUIT:
EndDialog(hwnd, ID_QUIT);
break;
}

break;
default:
return FALSE;
}
return TRUE;
}

Seems a little weird. I initially put WM_CLOSE in the first switch and
found out that such messages were not actually being passed...I wonder
why? Anyway thanx all for the support.

-- deostroll

deostroll

unread,
Dec 24, 2007, 2:09:12 PM12/24/07
to
Can someone tell me what constant has the value "0x2"?
-deostroll

Scott Seligman

unread,
Dec 24, 2007, 3:30:01 PM12/24/07
to
deostroll <deos...@gmail.com> wrote:
>Can someone tell me what constant has the value "0x2"?

Tons. Probably the on of interest to you is IDCANCEL, which is what
you should be looking for in the first place.


--
--------- Scott Seligman <scott at <firstname> and michelle dot net> ---------

He had to get up to run some more. He told his muscles to do so. They
told him they had the night off.
-- Deadly Relations: Bester Ascendant by J. Gregory Keyes

r_z_...@pen_fact.com

unread,
Dec 24, 2007, 3:34:14 PM12/24/07
to
On Mon, 24 Dec 2007 11:09:12 -0800 (PST), deostroll
<deos...@gmail.com> wrote:

>Can someone tell me what constant has the value "0x2"?

That is not nearly enough info. My copy of winuser.h lists the
following:
SB_CTL
SB_PAGEUP
SB_PAGELEFT
ALERT_SYSTEM_WARNING
IDCANCEL
and a whole lot more.


>-deostroll

-----------------------------------------
To reply to me, remove the underscores (_) from my email address (and please indicate which newsgroup and message).

Robert E. Zaret, eMVP
PenFact, Inc.
20 Park Plaza, Suite 400
Boston, MA 02116
www.penfact.com

deostroll

unread,
Dec 25, 2007, 9:44:10 PM12/25/07
to
On Dec 25, 1:34 am, r_z_aret@pen_fact.com wrote:
> On Mon, 24 Dec 2007 11:09:12 -0800 (PST), deostroll
>

Even WM_DESTROY has the same value. Putting WM_CLOSE in the switch
didn't actually do the work. (But I could have sworn I thought it
did). But instead WM_DESTROY does the trick gracefully. Anyway once
again thanx everyone for your support.

-deostroll

KIENI

unread,
Dec 30, 2007, 11:48:32 AM12/30/07
to
> I've created a simple c++ program. It just creates a plain dialog box
> with a button and a static control in it. It even has the ordinary
> SYSMENU buttons - minimize, maximize and close. But when I click on
> close (the "X" button) nothing happens!
>
> How to close this dialog box?
>
> ------ main.cpp ------
> #include <windows.h>
> #include "resource.h"
>
> BOOL CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM
> lParam)
> {
> switch(message)
> {
> case WM_INITDIALOG:
>
> break;
> case WM_COMMAND:
>
> switch(LOWORD(wParam))
> {
> case ID_QUIT:
> EndDialog(hwnd, ID_QUIT);
> break;
> }
> break;

case WM_SYSCOMMAND:
{
switch( LOWORD( wParam ) )
{
case SC_CLOSE:
DestroyWindow( hwnd );
break;

default:

0 new messages