gpt4 book ai didi

c++ - 为什么对 CreateWindow 的调用会失败,我该如何解决?

转载 作者:太空宇宙 更新时间:2023-11-04 15:30:33 25 4
gpt4 key购买 nike

我正在学习 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 中创建一个窗口,以便制作游戏引擎。

最佳答案

虽然不得滥用全局变量。

您正在使用 hwnd 作为句柄参数在 WndProc 中调用 DefWndProc。但这直到 CreateWindowEx 返回才设置,并且在窗口站立期间通过 WndProc 搜集了大量非常重要的消息.这些消息需要窗口句柄,而您并没有传递它。相反,您只是传递 NULL(hwnd 的启动值)。

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

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

关于c++ - 为什么对 CreateWindow 的调用会失败,我该如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54497997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com