gpt4 book ai didi

c++ - 在 OO 中创建一个窗口

转载 作者:行者123 更新时间:2023-11-30 03:59:40 33 4
gpt4 key购买 nike

这是我的标题

#pragma once
#ifndef BASE_H
#define BASE_H

#include <Windows.h>
#include <windowsx.h>

class Base
{
HWND hWnd;
WNDCLASSEX WndCls;
HRESULT Hr;
public:
Base();
static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
void RegisterWnd(HINSTANCE hInstance);
void ShowWnd(int nCmdShow);
~Base();
};

#endif

这是我的base.cpp

#include "Base.h"


Base::Base()
{
}

static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
// sort through and find what code to run for the message given
switch (message)
{
// this message is read when the window is closed
case WM_DESTROY:
{
// close the application entirely
PostQuitMessage(0);
return 0;
} break;
}

// Handle any messages the switch statement didn't
return DefWindowProc(hWnd, message, wParam, lParam);
}

void Base::RegisterWnd(HINSTANCE hInstance)
{
ZeroMemory(&WndCls, sizeof(WNDCLASSEX));
WndCls.cbSize = sizeof(WNDCLASSEX);
WndCls.hbrBackground = (HBRUSH)COLOR_WINDOW;
WndCls.hCursor = LoadCursor(NULL, IDC_ARROW);
WndCls.hIcon = LoadIcon(hInstance, NULL);
WndCls.hIconSm = LoadIcon(hInstance, NULL);
WndCls.hInstance = hInstance;
WndCls.lpfnWndProc = WndProc;
WndCls.lpszClassName = "ClsName";
WndCls.style = CS_HREDRAW | CS_VREDRAW;

Hr = RegisterClassEx(&WndCls);
if (FAILED(Hr))
MessageBox(NULL, "Window Class failed to register.", "ERROR", MB_OK);

hWnd = CreateWindowEx(
NULL,
"WndClassName",
"WndName",
WS_OVERLAPPEDWINDOW,
100, 100,
480, 640,
NULL,
NULL,
hInstance,
NULL);
if (FAILED(hWnd))
MessageBox(NULL, "Window Class failed to create", "ERROR", MB_OK);
}

void Base::ShowWnd(int nCmdShow)
{
Hr = ShowWindow(hWnd, nCmdShow);
if (FAILED(Hr))
MessageBox(NULL, "Failed to display Window", "ERROR", MB_OK);
}

Base::~Base()
{
}

这是我的 main.cpp

#include "Base.h"
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
Base CreateWnd;

CreateWnd.RegisterWnd(hInstance);
CreateWnd.ShowWnd(nCmdShow);

MSG Msg;
while (GetMessage(&Msg, NULL, 0, 0))
{
// translate keystroke messages into the right format
TranslateMessage(&Msg);

// send the message to the WindowProc function
DispatchMessage(&Msg);
}

// return this part of the WM_QUIT message to Windows
return Msg.wParam;
}

问题是,我不断收到我不理解的错误消息。对不起,不好的解释..仍然是编程的学生...... Here is the image of the error

更新:上面的错误已通过替换得到更正

static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

LRESULT CALLBACK Base::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

感谢 ravi 和 IInspectable 的快速帮助。

现在我遇到了另一个错误 D:当我点击调试时,一切运行正常,但没有任何显示。没有窗口显示。 Visual Studio 以“就绪”状态完美运行。 (抱歉,我不想提出另一个新问题,因为它仍然与在 oo 中创建窗口有关

第二次更新:我在 CreateWindowEx 中的类名与 RegisterWnd 中的类名不同..我的错。再次感谢 IInspectable 的帮助。

最佳答案

static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

你必须用类范围定义它,或者编译器如何知道它是全局静态还是类的静态成员。所以应该是

LRESULT CALLBACK Base::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

关于c++ - 在 OO 中创建一个窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26816320/

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