我现在正尝试使用win32和directX创建应用程序,但我坚持我的项目的第一步,即制作窗口。我尝试了很多东西来修复,但它一直说,未能创建窗口,这意味着hWnd为null。
这是我的代码。 app.h
#include <windows.h>
#include <string>
class App
{
public:
App (void);
~App (void);
HRESULT InitApp (HINSTANCE hInstance, int nCmdShow);
//void UpdateApp (void);
HWND window(void) { return m_hWnd; }
private:
static LRESULT CALLBACK WndProc(HWND hwnd,
UINT msg,
WPARAM wParam,
LPARAM lParam);
private:
HINSTANCE m_hInstance;
HWND m_hWnd;
RECT m_rect;
static bool s_running;
std::string m_className;
}
app.cpp
#include "app.h"
// std::string, c.str()
#include <cstring>
#include <iostream>
bool App::s_running = true;
App::App(void)
{
}
App::~App(void)
{
}
HRESULT App::InitApp(HINSTANCE hInstance, int nCmdShow)
{
m_hInstance = hInstance;
WNDCLASSEX wc = {};
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = App::WndProc;
wc.hInstance = m_hInstance;
wc.lpszClassName = "Hello" ;
RECT rc = {0, 0, 800, 600};
AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE);
int width = rc.right - rc.left;
int height = rc.bottom - rc.top;
if(!RegisterClassEx(&wc))
{
OutputDebugString("Failed to register win class\n");
return E_FAIL;
}
m_hWnd = CreateWindow("Hello", "Hello",
WS_OVERLAPPED | WS_CAPTION | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, width, height,
nullptr, nullptr, m_hInstance,
nullptr);
if(!m_hWnd)
{
OutputDebugString("Failed to create window\n");
return E_FAIL;
}
ShowWindow(m_hWnd, nCmdShow);
return S_OK;
}
LRESULT CALLBACK App::WndProc(
HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
switch(msg)
{
case WM_CREATE:
{
} break;
case WM_PAINT:
{
hdc = BeginPaint(hwnd, &ps);
EndPaint(hwnd, &ps);
} break;
case WM_DESTROY:
{
PostQuitMessage(0);
} break;
default:
{
DefWindowProc(hwnd, msg, wParam, lParam);
}
}
return 0;
}
以及在main.cpp中的用法
#include "app.h"
#include "graphics.h"
// test comment
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPSTR /*lpCmdLine*/, int nCmdShow)
{
App app;
Graphics graphics(app.window());
if(FAILED(app.InitApp(hInstance, nCmdShow)));
return 0;
/*if(FAILED(graphics.InitGraphics()))
{
graphics.CleanUpDevice();
return 0;
}i*/
MSG msg = {0};
while(WM_QUIT != msg.message)
{
if(PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
//graphics.Render();
}
}
//graphics.CleanUpDevice();
return 0;
}
我研究过很多这样的事情,但没有任何效果。我想我错过了很少的东西......但我找不到任何东西。请帮助这个可惜的noob编码器......
答案 0 :(得分:3)
WndProc
不返回DefWindowProc
返回的值,因此当窗口收到WM_NCCREATE
时,它仍将返回0,表示不应创建窗口。请注意,从WM_CREATE
返回0很好。窗口过程应如下所示:
LRESULT CALLBACK App::WndProc(
HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
LRESULT result{};
switch(msg)
{
case WM_CREATE:
{
result = 0;
} break;
case WM_PAINT:
{
PAINTSTRUCT ps;
hdc = BeginPaint(hwnd, &ps);
EndPaint(hwnd, &ps);
result = 0;
} break;
case WM_DESTROY:
{
PostQuitMessage(0);
result = 0;
} break;
default:
{
result = DefWindowProc(hwnd, msg, wParam, lParam);
}
}
return result;
}