【发布时间】:2020-11-18 07:33:47
【问题描述】:
您好,我是学习 DirectX 11 Api 的初学者,我在 Youtube 上看到了教程,并为 Windows 实现了这个 Wrapper 但是窗口很多 Loading 我得到的唯一调试字符串只是 INIT_WINDOw 方法中的 Register Class Failed 字符串。没有别的弹出。请帮我解决一下这个 。 DX_Wrapper.cpp
#include "Headers/Dx_Wrapper.h"
//Global One For Defining __stdcall for WindowProc
namespace
{
Wrapper* g_wrapper = nullptr;
}
//Main WINDOW Message processing Window Procedure
LRESULT CALLBACK WINDOWPROC(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
if (g_wrapper) return g_wrapper->MessageProcedure(hwnd, msg, wparam, lparam);
else return DefWindowProc(hwnd, msg, wparam, lparam);
}
//Constructor For the Instance
Wrapper::Wrapper(HINSTANCE hinstance)
{
m_hinstance = hinstance;
m_hwnd = NULL;
m_winheight = 600;
m_winwidth = 800;
m_wintitle = L"DirectX";
m_winstyles = WS_OVERLAPPEDWINDOW;
g_wrapper = this;
}
Wrapper::~Wrapper()
{
}
//Window Loop Manager Attached to the WINDOWPROC function
int32_t Wrapper::Run()
{
MSG message = { 0 };
while (WM_QUIT != message.message)
{
//Message Check If Quit
if (PeekMessage(&message, NULL, NULL, NULL, PM_REMOVE))
{
TranslateMessage(&message);
DispatchMessage(&message);
}
//If Not Quit
else
{
UPDATE_FRM(5.5f);
}
}
return static_cast<int>(message.wParam);
}
bool Wrapper::Init()
{
if (!INIT_WINDOW() == true)
{
return false;
}
return true;
}
LRESULT Wrapper::MessageProcedure(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
switch (msg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hwnd, msg, wparam, lparam);
}
}
void Wrapper::UPDATE_FRM(float delta_time)
{
}
void Wrapper::RENDER_FRM(float delta_time)
{
}
//Registering Classical Windows Class Function
bool Wrapper::INIT_WINDOW()
{
//THE CLASS REGISTERING STUFF
WNDCLASSEX win_class;
win_class.cbClsExtra = 0;
win_class.hInstance = m_hinstance;
win_class.cbWndExtra = 0;
win_class.style = CS_HREDRAW | CS_VREDRAW;
win_class.cbSize = sizeof(WNDCLASSEX);
win_class.lpfnWndProc = WINDOWPROC;
win_class.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
win_class.lpszMenuName = NULL;
win_class.lpszClassName = L"dx_main_class";
win_class.hCursor = 0;
RegisterClassEx(&win_class);
if (!RegisterClassEx(&win_class))
{
OutputDebugString(L"Registering Class Failed");
return false;
}
RECT r = {0,0,m_winwidth,m_winheight};
AdjustWindowRect(&r, m_winstyles, FALSE);
uint_fast16_t f_width = r.right - r.left;
uint_fast16_t f_height = r.bottom - r.top;
uint_fast16_t f_width_final = uint_fast16_t(GetSystemMetrics(SM_CXSCREEN) / 2 - f_width / 2);
uint_fast16_t f_height_final = uint_fast16_t(GetSystemMetrics(SM_CYSCREEN) / 2 - f_height / 2);
m_hwnd = CreateWindow(L"dx_main_class", m_wintitle, m_winstyles, f_width_final, f_height_final, f_width, f_height, NULL, NULL, m_hinstance, NULL);
if (!m_hwnd)
{
OutputDebugString(L"Window Could Not be Created ");
return false;
}
ShowWindow(m_hwnd, SW_SHOW);
return true;
}
DX_Wrapper.h
#include <Windows.h>
#include <stdint.h>
#include <string>
#define WIN32_LEAN_AND_MEAN
class Wrapper
{
public:
Wrapper(HINSTANCE hinstance);
virtual ~Wrapper();
int32_t Run();
virtual bool Init();
virtual LRESULT MessageProcedure(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);
virtual void UPDATE_FRM(float delta_time) = 0;
virtual void RENDER_FRM(float delta_time) = 0;
protected:
HWND m_hwnd = nullptr;
HINSTANCE m_hinstance = nullptr;
uint_fast16_t m_winwidth = 0;
uint_fast16_t m_winheight = 0;
LPCWSTR m_wintitle = L"";
DWORD m_winstyles = 0;
protected:
bool INIT_WINDOW();
};
这是主应用程序 CPP 文件
#include "Headers/Dx_Wrapper.h"
class Application : public Wrapper
{
public:
Application(HINSTANCE hinstance);
bool Init() override;
void UPDATE_FRM(float delta_time) override;
void RENDER_FRM(float delta_time) override;
};
Application::Application(HINSTANCE hinstance) : Wrapper(hinstance)
{
}
bool Application::Init()
{
return Wrapper::Init();
}
void Application::UPDATE_FRM(float delta_time)
{
}
void Application::RENDER_FRM(float delta_time)
{
}
int32_t WINAPI WinMain(__in HINSTANCE hinstance, __in_opt HINSTANCE hprevinstance, __in LPSTR cmdline, __in int cmdshow)
{
Application m_app(hinstance);
if (!m_app.Init()) return 1;
return m_app.Run();
}
我的问题是 OutputDebugString 仅显示 INIT_WINDOW 函数中的注册类失败字符串。我不知道为什么它是错误的。窗口没有运行。
【问题讨论】:
-
您尝试注册课程两次。如果您想了解更多信息,请阅读文档。他们会告诉您如何以及何时调用 GetLastError 以找出任何 api 调用失败的原因。
-
关注documentation:"
cbSize:此结构的大小,以字节为单位。将此成员设置为sizeof(WNDCLASSEX)。"我>
标签: c++ windows winapi visual-c++