gpt4 book ai didi

c++ - Win32 API 逻辑错误 : Code compiles fine but the main window doesn't show up

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

由于程序大小和性能问题,我正在重写我编写的软件;代码编译正常,但窗口未显示。

我已经调查了其他关于此的问题,但没有一个解决我的问题,这就是为什么我要问一个新问题,为了完整起见,请解释代码有什么问题以及如何解决问题。

我使用的是 Visual Studio 2013。这是代码:

WFrame.h

#pragma once

#include <Windows.h>
#include <tchar.h>
#include <wchar.h>

class WFrame
{
public:
WFrame();
WFrame(LPCWSTR szClassName, LPCWSTR szWindowTitle, HINSTANCE hInstance, int nCmdShow, int nX, int nY, int nWidth, int nHeight, DWORD dwStyle = WS_EX_OVERLAPPEDWINDOW);
~WFrame();

void show();

static LRESULT CALLBACK staticWindowProcedure(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
virtual LRESULT CALLBACK WindowProcedure(UINT uMsg, WPARAM wParam, LPARAM lParam);
protected:

private:
HWND hWnd;
int cmdShow;
};

WFrame.cpp

#include "WFrame.h"


WFrame::WFrame()
{

}

WFrame::WFrame(LPCWSTR szClassName, LPCWSTR szWindowTitle, HINSTANCE hInstance, int nCmdShow, int nX, int nY, int nWidth, int nHeight, DWORD dwStyle)
{
WNDCLASSEX wClass;
ZeroMemory(&wClass, sizeof(WNDCLASSEX));
wClass.cbClsExtra = NULL;
wClass.cbSize = sizeof(WNDCLASSEX);
wClass.cbWndExtra = NULL;
wClass.hbrBackground = (HBRUSH)COLOR_WINDOW;
wClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wClass.hIcon = NULL;
wClass.hIconSm = NULL;
wClass.hInstance = hInstance;
wClass.lpfnWndProc = (WNDPROC) WFrame::staticWindowProcedure;
wClass.lpszClassName = szClassName;
wClass.lpszMenuName = NULL;
wClass.style = CS_HREDRAW | CS_VREDRAW;

if (!RegisterClassEx(&wClass))
{
int nResult = GetLastError();
MessageBox(NULL,
L"Window class creation failed",
L"Window Class Failed",
MB_ICONERROR);
}

this->hWnd = CreateWindow(
szClassName,
szWindowTitle,
dwStyle,
nX,
nY,
nWidth,
nHeight,
NULL,
NULL,
hInstance,
NULL);

if (!hWnd)
{
int nResult = GetLastError();

MessageBox(NULL,
L"Window creation failed",
L"Window Creation Failed",
MB_ICONERROR);
}

}

WFrame::~WFrame()
{
delete this;
}

void WFrame::show()
{
ShowWindow(this->hWnd, this->cmdShow);
UpdateWindow(this->hWnd);
}

LRESULT CALLBACK WFrame::staticWindowProcedure(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
WFrame* winptr = (WFrame*)GetWindowLongPtr(hWnd, GWLP_USERDATA);

if (winptr == NULL) {
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
else {
return winptr->WindowProcedure(uMsg, wParam, lParam);
}
}

LRESULT CALLBACK WFrame::WindowProcedure(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CLOSE:
DestroyWindow(this->hWnd);
break;
case WM_DESTROY:
if (!GetParent(this->hWnd))
PostQuitMessage(0);
break;
}

return DefWindowProc(this->hWnd, uMsg, wParam, lParam);
}

main.cpp

#include <Windows.h>

#include "WFrame.h"

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WFrame * frame = new WFrame(L"Window", L"Window", hInstance, nCmdShow, 0, 0, 1024, 700);
frame->show();

MSG msg;
ZeroMemory(&msg, sizeof(MSG));

while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return msg.wParam;
}

感谢进一步的回答。

最佳答案

我没有看到你给成员变量cmdShow赋值,所以它的默认值为0,也就是SW_HIDE,所以你应该试试下面的代码,看看窗口是否可以显示或者在WFrame初始化器中赋值cmdShow。

void WFrame::show()
{
ShowWindow(this->hWnd, SW_SHOW);
UpdateWindow(this->hWnd);
}

关于c++ - Win32 API 逻辑错误 : Code compiles fine but the main window doesn't show up,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30428048/

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