Can I use EndDialog in WM_CLOSE

I have a main dialog box created with DialogBox in WinMain. I use EndDialog in WM_COMMAND to close the dialog box. This is the window procedure for it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
INT_PTR CALLBACK DialogProc(HWND hwndDlg, UNIT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
            case WM_COMMAND:
                    switch(LOWORD(wParam))
                    {
                    case IDCANCEL:
                            EndDialog(hwndDlg, IDCANCEL);
                            break;
                    }
                    break;

            default:
                    return FALSE;
    }
    return TRUE;
}


IDCANCEL is defined by windows to have the value 2. But I need to define something else to the value 2 to be used in WM_COMMAND. Most of the code you see online uses EndDialog in WM_COMMAND. Could I use EndDialog in WM_CLOSE instead like this?
case WM_CLOSE:
EndDialog(hwndDlg, IDCANCEL);
break;
Last edited on
If this is a custom message box then yes, you can assign any value you want to the button. But the second parameter in "EndDialog()" will be the value returned to the parent window so that's the value you want to change.
This is the main window. Its created in WinMain() like this
1
2
3
4
int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
	return DialogBox(hInstance, TEXT("MainDialog"), 0, DialogProc);
}


That's all the code there in in WinMain()
Last edited on
But I need to define something else to the value 2 to be used in WM_COMMAND.

Out of interest, what needs to have the value of 2?

If possible, it would be better to leave IDCANCEL and the like alone (for reasons of consistency if nothing else) and apply an offset to your IDs.

For example, it you were coding a caculator you could assign the numeric buttons IDs' of 2000, 2001, ... 2009 (for 0..9) and then subtract 2000 when you handle the command.

Could I use EndDialog in WM_CLOSE instead like this?

WM_CLOSE will work if you're just closing the dialog by either clicking on the [x] button or the "Close" item in the system menu.

Andy
Topic archived. No new replies allowed.