x0x Rain x0x
x0x Rain x0x

Reputation: 11

Creating a Window in WinAPI after pressing a button

I'm making an auto clicker for a game in WinAPI and I have 4 simple buttons on the main window. When the user presses the 'start' button I want another window to open asking them for settings such as number of times to click and time between clicks. When I try to create a new window, nothing is happening, but everything else works perfectly.

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
    case WM_DESTROY:
        {
            PostQuitMessage(0);
            return 0;
        }

    case WM_COMMAND:
        {
            switch (wParam)
            {
            case ID_START:
                {
                    HINSTANCE hInstance = GetModuleHandle(CLASS_NAME);

                    HWND settings = CreateWindowEx(
                        0,
                        L"Settings",
                        L"Settings",
                        WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_CHILD,
                        100, 100, 600, 200, 
                        NULL,
                        (HMENU) ID_SETTINGS,
                        hInstance,
                        NULL
                        );

                    MSG msg = { };

                    while (GetMessage (&msg, NULL, 0, 0))
                    {
                        TranslateMessage(&msg);
                        DispatchMessage(&msg);
                    }

                    return 0;
                }

            case ID_QUIT:
                {
                    PostQuitMessage(0);
                    return 0;
                }

            case ID_CALIB:
                {
                    if (MessageBox(hwnd, L"You pressed Calibrate", L"Calibrate", MB_OK))
                    {
                        return 0;
                    }
                }

            case ID_INFO:
                {
                    if (MessageBox(hwnd, L"You pressed about", L"About", MB_OK))
                    {
                        return 0;
                    }
                }
            }
        }

    case WM_PAINT:
        {
            PAINTSTRUCT ps;

            HDC hdc = BeginPaint(hwnd, &ps);

            FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW+1));
            EndPaint(hwnd, &ps);

            return 0;
        }       
    }

    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

I just started WinAPI today, so I am extremely new. Thanks for any help in advance!

Upvotes: 0

Views: 2969

Answers (1)

ScottMcP-MVP
ScottMcP-MVP

Reputation: 10415

The second parameter to CreateWindowEx must be a class name that you registered earlier by calling RegisterClass.

You are specifying WS_CHILD. But a child must have a parent. Pass the parent HWND into the hwndParent parameter.

Upvotes: 0

Related Questions