我想創建一個演示窗口框架在它自己的類中處理並在WinMain文件中調用,我有main.cpp和window.h和window.cpp,代碼編譯成功,但窗口不會盡管在系統托盤窗口標題我在main.cpp中通過彈出顯示,這裏是整個代碼:WinApi的c + +窗口不會出現oop
main.cpp中:
#include <Windows.h>
#include "window.h"
window* WinapiInit = new window;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE PrevhInstance, LPSTR
lpCmdLine, int nCmdShow)
{
WinapiInit->initWindow(hInstance, 800, 600, L"DirectX 12 Demo", nCmdShow);
delete WinapiInit;
return 0;
}
window.h
#pragma once
#include <Windows.h>
class window
{
private:
LPCWSTR m_WindowTitle;
const LPCTSTR m_WindowClassName = L"WindowClass1";
int m_WindowWidth;
int m_WindowHeight;
const int m_ScreenPosX = GetSystemMetrics(SM_CXSCREEN);
const int m_ScreenPosY = GetSystemMetrics(SM_CYSCREEN);
HWND hWnd;
WNDCLASSEX wcex;
MSG msg;
public:
void initWindow(HINSTANCE hInstance, int WindowWidth, int WindowHeight,
LPCWSTR WindowTitle, int nCmdShow);
static LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM
lParam);
void mainLoop();
};
window.cpp
#include "window.h"
LRESULT window::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
ExitProcess(0);
break;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
break;
}
return 0;
}
void window::mainLoop()
{
msg = { 0 };
while (WM_QUIT != msg.message)
{
if (PeekMessage(&msg, hWnd, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
//GAME CODE HERE
//...
}
}
void window::initWindow(const HINSTANCE hInstance, int WindowWidth, int
WindowHeight, LPCWSTR WindowTitle, const int nCmdShow)
{
m_WindowWidth = WindowWidth;
m_WindowHeight = WindowHeight;
m_WindowTitle = WindowTitle;
wcex = { 0 };
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 3);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
wcex.hIconSm = LoadIcon(hInstance, IDI_APPLICATION);
wcex.hInstance = hInstance;
wcex.lpfnWndProc = window::WndProc;
wcex.lpszMenuName = nullptr;
wcex.lpszClassName = m_WindowClassName;
wcex.style = CS_HREDRAW | CS_VREDRAW;
if (!RegisterClassEx(&wcex))
{
MessageBox(NULL, L"RegisterClassEx Call Error!", L"ERROR" , MB_OK |
MB_ICONERROR);
}
hWnd = CreateWindowEx(NULL, m_WindowClassName, WindowTitle,
WS_OVERLAPPEDWINDOW, m_ScreenPosX, m_ScreenPosY,
WindowWidth,
WindowHeight, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
MessageBox(NULL, L"CreateWindowEx Call Error!", L"ERROR", MB_OK |
MB_ICONERROR);
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
mainLoop();
}
是什麼導致了窗口不顯示?我是面向對象編程的初學者,所以我認爲這是一個問題,但我無法弄清楚,任何幫助將不勝感激。
如果你正在做C++的Win32開發,你可以看看MFC。 –
@Dan ok然後,謝謝你的領導 –
@Cody Gray很好,WinapiInit是一個指向窗口類的指針(希望),我發佈了頭文件和cpp文件,對於任何幫助將不勝感激 –