简体   繁体   中英

Win32 API window won't open

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;
}

使用WS_EX_OVERLAPPEDWINDOW作为CreateWindowEx函数的第一个参数(而不是WS_OVERLAPPEDWINDOW ,这不是有效的扩展窗口样式)。

instead of using WNDCLASSEX use WNDCLASS

change:

WNDCLASSEX WCE; to WNDCLASS WCE;

remove line:

WCE.cbSize = sizeof(WNDCLASSEX);

change:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM