Shanks Limbu
Shanks Limbu

Reputation: 147

CreateWindowEx called on button click fails

I'm trying to create a dialog window from button window click in WM_COMMAND of main window procedure.

case WM_CREATE: 
            hwndRegister = CreateWindow(TEXT("BUTTON"), TEXT("Register"), WS_VISIBLE | WS_CHILD, CW_USEDEFAULT, CW_USEDEFAULT, 100, 50, hwnd, (HMENU)REGISTER_WINDOW, NULL, NULL);
            break;

        case WM_COMMAND:
            switch (LOWORD(wParam)) 
            {
                case REGISTER_WINDOW:
                    DisplayRegisterDialog(hwndRegister);
                    break;
            }
            break;

If that hwndRegister is clicked, It should pop up new dialog window defined and registered in another cpp file.

CONST wchar_t DIALOG_WINDOW_NAME[] = L"DialogClass";
void RegisterDialog(HINSTANCE hInstance) 
{
    WNDCLASS dialogBox = { };

    dialogBox.lpfnWndProc = DialogProc;
    dialogBox.lpszClassName = DIALOG_WINDOW_NAME;
    dialogBox.hInstance = hInstance;
    dialogBox.hCursor = LoadCursor(NULL, IDC_ARROW);

    RegisterClass(&dialogBox);
}

void DisplayRegisterDialog(HWND hwnd) 
{
    if (!hwnd) 
    {
        MessageBox(NULL, L"Window Creation faile", L"WINDOW CREATION FAILED", MB_ICONERROR);
    }
    HWND hwndDialogWindow = CreateWindowEx(0, DIALOG_WINDOW_NAME, L"Register User", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 200, 200, hwnd, (HMENU)DIALOG_REGISTER_WINDOW, NULL, NULL);
}

I'm getting the instance to register dialog from winapi winmain of main application.

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) {
RegisterDialog(hInstance);
}

For now I don't get any visible error thrown and the program runs smoothly. However, The dialog window is not created nor an error is thrown. Edit - Dialog procedure implementation:

LRESULT CALLBACK DialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
        case WM_CLOSE:
            PostQuitMessage(0);
            break;
    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

I'd also like to note after following IInspectable advice on error checking manually. I did this if window is being registered

if (RegisterClass(&dialogBox) == 0) {
        MessageBox(NULL, L"Dialog window registration failed", L"Error", 0);
        throw GetLastError();
    }

This code throws the exception of Microsoft C++ exception: unsigned long at memory location 0x0078F828.

EDIT 2- Alright. I've finally found the problem with error code 1410 Class already exists. I have difference names for these windows class. Is it because of hInstance ?

Upvotes: 0

Views: 464

Answers (2)

Shanks Limbu
Shanks Limbu

Reputation: 147

The error was due to HMENU set in hwndDialogWindow. It had to be null. Also about 1410 error. I am confused about it as well. The code is

RegisterClass(&dialogBox);
if (RegisterClass(&dialogBox) == 0) {
        int nresult = GetLastError();
        MessageBox(NULL, L"Dialog window registration failed", L"Error", 0);
    }

I don't know how the above code produces 1410 error. I remove the first line to register and the error went away.

Also Thank you to RemyLebeau for pointing out the wrong approach, Inspectable for the advice on manually checking error which was tremendously helpful and Iziminza for dialog rss script which I'll use for other button.

Upvotes: 0

Iziminza
Iziminza

Reputation: 389

Your function DialogProc is actually a normal window procedure, not a dialog procedure, so you have to pass unhandled messages to DefWindowProc, like this:

LRESULT CALLBACK DialogProc /*misnomer!*/(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch (uMsg) {
    case WM_CLOSE:
        MessageBox(hwnd, L"Test", L"Test", 0);
        break;
    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam); // important!!
}

If you write return 0; instead, the window will be destroyed immediately after it returns from the call with WM_CREATE.

Upvotes: 1

Related Questions