Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么调用CreateWindow失败,我如何修复它?_C++_Windows_Visual Studio_Winapi - Fatal编程技术网

C++ 为什么调用CreateWindow失败,我如何修复它?

C++ 为什么调用CreateWindow失败,我如何修复它?,c++,windows,visual-studio,winapi,C++,Windows,Visual Studio,Winapi,我正在学习directx12的教程,甚至无法理解窗口D的创建:有一个已实现的错误窗口,它告诉我在创建实际要创建的窗口时出错。 这应该意味着它“没有成功地获得一个窗口句柄”。。。 我不知道这意味着什么,但是我非常感谢你的帮助 #include "stdafx.h" //Handle to the window HWND hwnd = NULL; //Name of the window LPCTSTR WindowName = L"GameEngineApp"; //Title of the

我正在学习directx12的教程,甚至无法理解窗口D的创建:有一个已实现的错误窗口,它告诉我在创建实际要创建的窗口时出错。 这应该意味着它“没有成功地获得一个窗口句柄”。。。 我不知道这意味着什么,但是我非常感谢你的帮助

#include "stdafx.h"

//Handle to the window
HWND hwnd = NULL;

//Name of the window
LPCTSTR WindowName = L"GameEngineApp";

//Title of the window
LPCTSTR WindowTitle = L"My Game Engine";

//Width and height of the window
int Width = 800;
int Height = 600;

//Toggle for fool screen
bool FullScreen = false;

//Toggle for window creation
bool InitializeWindow(HINSTANCE hInstance,
    int ShowWnd, int width, int height, bool fullscreen);

//Main application loop
void mainLoop()
{
    //The message var hold any windows message received through the 
    //PeekMessage function
    MSG msg;
    ZeroMemory(&msg, sizeof(MSG));

    while (true)
    {
        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            //If there is a message, it is translated
            //then dispatched so Windows 
            //knows the program hasn't stopped working
            if (msg.message = WM_QUIT) {
                break;

                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
            else {
                //If there is no message the game 
                //code will run and the loop will continue
            }
        }
    }
}

//Callback function for windows messages
LRESULT CALLBACK WndProc(HWND hWnd,
    UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
    case WM_KEYDOWN:
        if (wParam == VK_ESCAPE) {
            if (MessageBox(0, L"Are you sure you want to exit?",
                L"Really?", MB_YESNO | MB_ICONQUESTION) == IDYES)
                DestroyWindow(hwnd);
        }
        return 0;

    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hwnd,
        msg, wParam, lParam);
}

//Main Windows function
int WINAPI WinMain(HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nShowCmd)
{
    //Toggle window creation
    if (!InitializeWindow(hInstance, nShowCmd, Width, Height, FullScreen))
    {
        MessageBox(0, L"Window Initialization - Failed",
            L"Error", MB_OK);
        return 0;
    }

    mainLoop(); //Call main loop to start

    return 0;
}

bool InitializeWindow(HINSTANCE hInstance,
    int ShowWnd, int width, int height, bool fullscreen)
{
    //Check if fullscreen is needed and set to fullscreen if so
    if (fullscreen)
    {
        HMONITOR hmon = MonitorFromWindow(hwnd,
            MONITOR_DEFAULTTONEAREST);
        MONITORINFO mi = { sizeof(mi) };
        GetMonitorInfo(hmon, &mi);

        width = mi.rcMonitor.right - mi.rcMonitor.left;
        height = mi.rcMonitor.bottom - mi.rcMonitor.top;
    }

    //Window class structure
    WNDCLASSEX wc;
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = NULL;
    wc.cbWndExtra = NULL;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 2);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = WindowName;
    wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

    //Registering the window class
    if (!RegisterClassEx(&wc))
    {
        MessageBox(NULL, L"Error registering class",
            L"Error", MB_OK | MB_ICONERROR);
        return false;
    }

    //Create the window with the now registered window class
    hwnd = CreateWindowEx(NULL,
        WindowName,
        WindowTitle,
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT,
        width, height,
        NULL,
        NULL,
        hInstance,
        NULL);

    //Return error msg if unsuccessful in getting a window handle
    if (!hwnd)
    {
        MessageBox(NULL, L"Error creating window",
            L"Error", MB_OK | MB_ICONERROR);
        return false;
    }

    //Removing the style from the window when fullscreen
    if (fullscreen)
    {
        SetWindowLong(hwnd, GWL_STYLE, 0);
    }

    //Showing and updating the window
    ShowWindow(hwnd, ShowWnd);
    UpdateWindow(hwnd);

    return true;
}
我尝试将CreateWIndowEx更改为CreateWindowA,这是唯一可能产生影响的事情,但我没有看到任何结果。 我已尝试打印错误,但没有错误


< >用C++代码在Windows中创建窗口,这样我就可以制作一个游戏引擎。< /p> 但不要滥用全局变量。 您正在使用
hwnd
作为句柄参数,在
WndProc
中调用
DefWndProc
。但是直到
CreateWindowEx
返回时才设置,在窗口站立期间,有大量非常重要的消息被浏览
WndProc
。这些消息需要窗口句柄,而您不能传递它。相反,您只是传递
NULL
(启动值
hwnd

WndProc中的all
hwnd
更改为
hwnd
(参数名称)


我留下的评论中提到的其余问题供您解决。

请包括您的代码,如果没有详细说明,将很难帮助您。感谢您的快速回答!很抱歉,我正在处理它,因为我知道如何在文本框中缩进它。您的消息循环已关闭。您可能打算在WM_QUIT时中断,但您只处理WM_QUIT消息。修正大括号。投票关闭你想要的
msg.message==WM\u QUIT
至少。嘿,谢谢你的帮助,这真的成功了!我真的没有发现我在if语句中有一个输入错误,这是非常糟糕的,但是现在我必须在task manager中结束该过程才能关闭它,我不知道这是否正常,因为我相信我已经实现了一个关闭窗口的函数。