0
votes

okay, so I have taken out the time to learn a but of the Win32 API to do with opening windows, and the code I came up with in the end I would think would work, but doesn't. I registered the window class, made all the things I have to, but when I run it, nothing happens... It would be a great help if someone could point out what I am doing wrong/missing.

#include <stdlib.h>
#include <iostream>
#include <Windows.h>
#pragma comment (lib, "wsock32.lib")
#define WNDCLASSNAME "wndclass"

bool quit = false;

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_CLOSE:
            DestroyWindow(hwnd);
            break;

        case WM_DESTROY:
            PostQuitMessage(0);
            break;

        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int nshowcmd)
{
    WNDCLASSEX WCE;
    WCE.cbSize = sizeof(WNDCLASSEX);
    WCE.style = CS_HREDRAW|CS_VREDRAW|CS_OWNDC|CS_DBLCLKS;
    WCE.lpfnWndProc = WndProc;
    WCE.cbClsExtra = 0;
    WCE.cbWndExtra = 0;
    WCE.hInstance = hinstance;
    WCE.hIcon = NULL;//LoadImage()
    WCE.hCursor = NULL;//LoadCursor(NULL, IDC_CROSS);
    WCE.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
    WCE.lpszMenuName = NULL;
    WCE.lpszClassName = "KyleWindow";
    WCE.hIconSm = NULL;

    RegisterClassEx(&WCE);

    HWND WindowHandle;

    WindowHandle = CreateWindowEx(WS_OVERLAPPEDWINDOW, "KyleWindow", "Xerus", WS_OVERLAPPEDWINDOW, 0, 0, 500, 500, NULL, NULL, hinstance, NULL);

    ShowWindow(WindowHandle, SW_SHOWNORMAL);
    UpdateWindow(WindowHandle);

    std::cout<<"'Opened' Window"<<std::endl;

    MSG msg;

    while(!quit)
    {
        if(PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE))
        {
            if(msg.message == WM_QUIT)
                quit = true;
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    return msg.lParam;
}
3
Um, can you at least say whether all the function calls succeed? What messages does your window procedure receive? We're not psychic.Raymond Chen
@RaymondChen Sorry, yes it does; the function call does succeed.Kyle Martin
You're passing WS_OVERLAPPEDWINDOW for the extended window styles, I think you mean WS_EX_OVERLAPPEDWINDOW.Jonathan Potter
@JonathanPotter Just tried that, and it unfortunately doesn't work. EDIT: Oops... It does work, thank you!Kyle Martin
@KyleMartin Not sure what you mean. There are at least seven function calls in your program. Are you saying that they all succeed?Raymond Chen

3 Answers

3
votes

Use WS_EX_OVERLAPPEDWINDOW as the first parameter of your CreateWindowEx function (instead of WS_OVERLAPPEDWINDOW, which is not a valid extended window style).

-1
votes

instead of using WNDCLASSEX use WNDCLASS

change:

WNDCLASSEX WCE; to WNDCLASS WCE;

remove line:

WCE.cbSize = sizeof(WNDCLASSEX);

change:

RegisterClassEx(&WCE); to RegisterClass(&WCE);

-2
votes

The function int WINAPI WinMain must be before function LRESULT CALLBACK WndProc. Compilers read in order.